Objective-CとSwift比較について

WWDC2014は徹頭徹尾“Write the code. Change the world”だった。
WWDC2014で発表された新しいプログラミング言語SwiftとObjective-C違いが知りたいので、教えてください。
2014-06-10 13:41 コメント
已邀请:

ベストアンサー

0

iQi - 面白いアプリを開発中

{ 賛成 }:

UIView

@Objective-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

@Objective-c

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


@Swift

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


UIImageView

@Objective-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);


アニメーション

@Objective-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

@Objective-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

@Objective-c

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


@Swift

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


NSLog

@Objective-c

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


@Swift

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


NSArray

@Objective-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

@Objective-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

@Objective-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

@Objective-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])");
}
2014-06-10 13:48 コメント

ログインまたは登録してから回答してください

EXIT フルスクリーンモード 回答