糖果誰(shuí)不愛(ài)呢
oc 為了簡(jiǎn)化一些寫(xiě)法,會(huì)有所謂的語(yǔ)法糖,讓你事半功倍!
變量聲明
NSString *str = @"123"; //字符串
NSArray *arr = @[@"123",@"123"];//數(shù)組
//訪問(wèn)數(shù)組
id a = arr[0]
//字典
NSDictionary *dictionary = @{
@"key0" : @"value0",
@"key1" : @"value1",
@"key2" : @"value2"
};
//訪問(wèn)字典
id a = dictionary[@"key0"]
//nsnumber
NSNumber *a = @123;
NSNumber *b = @11.2;
NSNumber *c = @('a');
注 id 為泛類(lèi)型修飾符,表明此變量可能是任何類(lèi)型
UI方面
//({ })
self.imageView = ({
UIImageView *imageView = [[UIImageView alloc] init];
imageView.backgroundColor = [UIColor redColor];
imageView.image = [UIImage imageNamed:@
"12345"];
imageView.frame = CGRectMake(0, 0, 100, 100);
[self.view addSubview:imageView];
imageView; //結(jié)尾必須要有此返回
});
注:1 強(qiáng)烈建議用這樣的寫(xiě)法加入xcode 的snippet, 提高工作效率!
2 xcode 內(nèi)<#描述文字#> 是IDE 專(zhuān)用占位符,方便你使用snippet時(shí)填入需要的內(nèi)容.