OC基礎(chǔ)語法
//整型
NSIntegera =10;
//NSLog是OC里面的打印函數(shù)
NSLog(@"a = %ld",a);
//浮點(diǎn)型
CGFloatb =2.3;
NSLog(@"b = %.2f",b);
//布爾類型
BOOLflag =YES;
//字符串NSString(只要是對(duì)象類型,占符全部都是%@)
NSString*str =@"abcde";
NSLog(@"str = %@",str);
//length字符串的長度
NSLog(@"str的長度= %ld",str.length);
//字符串相等(全等,前綴,后綴)
//1,全等
if([strisEqualToString:@"abcde"]){
NSLog(@"是的");
}
//2,前綴相等
if([strhasPrefix:@"a"]){
NSLog(@"前綴等于a");
}
//3,后綴相等
if([strhasSuffix:@"de"]){
NSLog(@"后綴等于de");
}
//格式化創(chuàng)建字符串
NSString*str1 = [NSString stringWithFormat:@"im"];
NSLog(@"str = %@",str1);
//數(shù)組(NSArry/NSMutableArry)
//不可變數(shù)組
NSArray*array1 =@[@"a",@"b",@"c",@"d"];
NSLog(@"array1 = %@",array1);
//數(shù)組元素個(gè)數(shù).count
NSLog(@"count = %ld",array1.count);
//通過下標(biāo)訪問數(shù)組里面的元素
NSString*str = array1[0];
NSLog(@"str = %@",str);
//可變數(shù)組NSMutableArray
NSMutableArray*mutableArray = [NSMutableArrayarrayWithObjects:@"1",@"2",@"3",@"4",nil];
NSLog(@"mutableArray = %@",mutableArray);
//添加
[mutableArrayaddObject:@"5"];
NSLog(@"已添加-----%@",mutableArray);
//移除
[mutableArrayremoveObject:@"3"];
NSLog(@"已移除-----%@",mutableArray);
//字典(存放)(NSDictionary,NSMutableDictionary)
//不可變字典NSDictionary
NSDictionary*dict =@{@"key1":@"value1",@"key2":@"value2",@"key3":@"value3"};
NSLog(@"dict = %@",dict);
NSString*string =[dictobjectForKey:@"key1"];
NSLog(@"string = %@",string);
//所有的key值,所有的value值
NSLog(@"allValue = %@,allValue = %@",dict.allKeys,dict.allValues);
//調(diào)用方法用空格,方法調(diào)用結(jié)束用括號(hào)表示
[self func1];
NSIntegernum = [self func2];
NSLog(@"num = %ld",num);
NSIntegerlength = [selflengOfstring:@"12345"];
NSLog(@"length = %ld",length);
//+表示類方法,只能用類來調(diào)用,-表示實(shí)例方法,用對(duì)象調(diào)用
//無參數(shù)輸入的方法格式:+/-(方法的返回值)方法名
- (void)func1
{
NSLog(@"%s",__func__);
}
-(NSInteger)func2{
NSLog(@"%s",__func__);
return20;
}
//有參數(shù)輸入的方法格式:=+/-(方法的返回值)方法名:(參數(shù)1類型)參數(shù)1名方法名:(參數(shù)2類型)參數(shù)2名
//輸入字符串,返回字符串長度
-(NSInteger)lengOfstring:(NSString*)string
{
returnstring.length;
}
//內(nèi)存溢出的時(shí)候調(diào)用
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
}
//按鈕UIButton (UIButton *button =[UIButton buttonWithType:UIButtonTypeInfoDark];
)
//frame表明了控件的坐標(biāo)和寬高(CGRect類型)
UIButton*button = [[UIButtonalloc]initWithFrame:CGRectMake(20(x),50(y),80(寬),80(高))];
[button setTitle:@"火影" forState:UIControlStateNormal];
//根據(jù)名字加載圖片
UIImage*image = [UIImageimageNamed:@"left_normal"];
//給按鈕設(shè)置背景圖片(forState表明按鈕狀態(tài))
[buttonsetBackgroundImage:ImageforState:(UIControlStateNormal)];
//給按鈕設(shè)置背景圖片顏色
button.backgroundColor = [UIColor redColor];
//按鈕的監(jiān)聽
[buttonaddTarget:selfaction:@selector(btnClickLister)forControlEvents:UIControlEventTouchUpInside];
//添加按鈕到視圖上面
[self.viewaddSubview:button];
//相框UIImageView
UIImageView*imageview = [[UIImageViewalloc]initWithFrame:CGRectMake(150(x),50(y),200(寬),200(高))];
UIImage*image1 = [UIImageimageNamed:@"biaoqingdi"];
//設(shè)置imageView顯示的圖片
imageview.image= image1;
[self.viewaddSubview:imageview];
//標(biāo)簽UILabel
UILabel*label = [[UILabelalloc]initWithFrame:CGRectMake(150(x),270(y),150(寬),30(高))];
//設(shè)置標(biāo)簽文本
label.text=@"XUANG";
//設(shè)置居中方式
label.textAlignment=NSTextAlignmentCenter;
//設(shè)置標(biāo)簽文本顏色
label.textColor= [UIColorredColor];
//添加標(biāo)簽到視圖上面
[self.viewaddSubview:label];}