【Objective-CとSwift比較】※今後追加予定

  • 225
    いいね
  • 0
    コメント
この記事は最終更新日から1年以上が経過しています。

■UIView

@Obj-C

UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
v.backgroundColor = [UIColor colorWithRed:1.0f green:1.0 blue:0.0 alpha:1.0f];
[self.view addSubview:v];

@Swift

let v:UIView = UIView(frame:CGRectMake(0, 0, 100, 100));
v.backgroundColor = UIColor(red: 1.0, green: 1.0, blue: 0.0, alpha: 1.0);
self.view.addSubview(v);

■UIImage

@Obj-C

UIImage *img = [UIImage imageNamed:@"img.png"];

@Swift

let img:UIImage = UIImage(named:"img.png");

■UIImageView

@Obj-C

UIImageView *iv = [[UIImageView alloc] initWithImage:img];
iv.frame = CGRectMake(0, 0, 100, 100);
[self.view addSubview:iv];

@Swift

let iv:UIImageView = UIImageView(image:img);
iv.frame = CGRectMake(0, 0, 100, 100);
self.view.addSubview(iv);

■アニメーション

@Obj-C

[UIView animateWithDuration:1.0f
    delay:0.0f
    options:UIViewAnimationOptionCurveEaseIn
animations:^(void){
    iv.frame = CGRectMake(200, 0, 100, 100);
    iv.transform = CGAffineTransformScale(iv.transform, 0.5, 0.5);
    iv.alpha = 0.5;
}
completion:^(BOOL finished){
    NSLog(@"Animation End");
}];

@Swift

UIView.animateWithDuration(
    1.0,
    delay: 0.0,
    options: UIViewAnimationOptions.CurveEaseIn,
    animations: {
        iv.frame = CGRectMake(200, 0, 100, 100);
        iv.transform = CGAffineTransformScale(iv.transform, 0.5, 0.5);
        iv.alpha = 0.5;
    },
    completion:{
        (value: Bool) in
        println("Animation End");
    }
);

■UIButton

@Obj-C

UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(100, 0, 100, 100);
[btn setImage:img forState:UIControlStateNormal];
[btn addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];

@Swift

let btn:UIButton? = UIButton.buttonWithType(UIButtonType.Custom) as? UIButton;
btn!.frame = CGRectMake(100, 0, 100, 100);
btn!.setImage(img, forState: UIControlState.Normal);
btn!.addTarget(self, action: "onClick:", forControlEvents: UIControlEvents.TouchUpInside);
self.view.addSubview(btn);

■NSString

@Obj-C

NSString *str0 = @"文字列";
NSString *str1 = [NSString stringWithFormat:@"文字列%d", 1];

@Swift

let str0:String! = "文字列";
let str1:String! = NSString(format:"文字列%d", 1)

■NSLog

@Obj-C

NSLog(@"%@ : %@", str0, str1);

@Swift

NSLog("%@ : %@", str0, str1);
println("\(str0) : \(str1)");

■NSArray

@Obj-C

NSArray *arr0 = [NSArray arrayWithObjects:@"0", @"1", @"2", nil];
for(NSString *v in arr0){
    NSLog(@"%@", v);
}

@Swift

let arr0:String[] = ["0", "1", "2"];
arr0[0] = "00"; // 差し替えは可能
// arr0 += ["3", "4"]; // 追加は不可
// arr0.insert("insert", atIndex:1); // 差し込みは不可
for v in arr0{
    println("\(v)");
}

■NSMutableArray

@Obj-C

NSMutableArray *arr1 = [NSMutableArray arrayWithObjects:@"0", @"1", @"2", nil];
[arr1 replaceObjectAtIndex:0 withObject:@"00"]; // 差し替え
[arr1 addObject:@"3"]; // 追加
[arr1 addObject:@"4"]; // 追加
[arr1 addObject:@"5"]; // 追加
[arr1 insertObject:@"insert" atIndex:1]; // 差し込み
for(NSString *v in arr1){
    NSLog(@"%@", v);
}

@Swift

var arr1:String[] = ["0", "1", "2"];
arr1[0] = "00"; // 差し替え
arr1 += ["3", "4"]; // 追加
arr1.append("5"); // 追加
arr1.insert("insert", atIndex:1); // 差し込み
for v in arr1{
    println("\(v)");
}

■NSDictionary

@Obj-C

NSDictionary *dict0 = [NSDictionary dictionaryWithObjectsAndKeys:@"value0", @"key0", @"value1", @"key1", nil];
for(NSString *key in [dict0 allKeys]){
    NSLog(@"%@, %@", key, [dict0 objectForKey:key]);
}

@Swift

let dict0:Dictionary<String, String> = ["key0" : "value0", "key1" : "value1"];
//dict0["key0"] = "mod_value0"; // 差し替えは不可
//dict0["key2"] = "value2"; // 追加は不可
//dict0.removeValueForKey("key1"); 削除は不可
for key in dict0.keys{
    println("\(key) : \(dict0[key])");
}

■NSMutableDictionary

@Obj-C

NSMutableDictionary *dict1 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"value0", @"key0", @"value1", @"key1", nil];
[dict1 setObject:@"mod_value0" forKey:@"key0"]; // 差し替え
[dict1 setObject:@"value2" forKey:@"key2"]; // 追加
[dict1 removeObjectForKey:@"key1"]; // 削除
for(NSString *key in [dict1 allKeys]){
    NSLog(@"%@, %@", key, [dict1 objectForKey:key]);
}

@Swift

var dict1:Dictionary<String, String> = ["key0" : "value0", "key1" : "value1"];
dict1["key0"] = "mod_value0"; // 差し替え
dict1["key2"] = "value2"; // 追加
dict1.removeValueForKey("key1"); // 削除
for key in dict1.keys{
    println("\(key) : \(dict1[key])");
}