无标题文章

OC基础语法

//整型

NSIntegera =10;

//NSLog是OC里面的打印函数

NSLog(@"a = %ld",a);

//浮点型

CGFloatb =2.3;

NSLog(@"b = %.2f",b);

//布尔类型

BOOLflag =YES;

//字符串NSString(只要是对象类型,占符全部都是%@)

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");

}

//格式化创建字符串

NSString*str1 = [NSString stringWithFormat:@"im"];

NSLog(@"str = %@",str1);

//数组(NSArry/NSMutableArry)

//不可变数组

NSArray*array1 =@[@"a",@"b",@"c",@"d"];

NSLog(@"array1 = %@",array1);

//数组元素个数.count

NSLog(@"count = %ld",array1.count);

//通过下标访问数组里面的元素

NSString*str = array1[0];

NSLog(@"str = %@",str);

//可变数组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);

//调用方法用空格,方法调用结束用括号表示

[self func1];

NSIntegernum = [self func2];

NSLog(@"num = %ld",num);

NSIntegerlength = [selflengOfstring:@"12345"];

NSLog(@"length = %ld",length);

//+表示类方法,只能用类来调用,-表示实例方法,用对象调用

//无参数输入的方法格式:+/-(方法的返回值)方法名

- (void)func1

{

NSLog(@"%s",__func__);

}

-(NSInteger)func2{

NSLog(@"%s",__func__);

return20;

}

//有参数输入的方法格式:=+/-(方法的返回值)方法名:(参数1类型)参数1名方法名:(参数2类型)参数2名

//输入字符串,返回字符串长度

-(NSInteger)lengOfstring:(NSString*)string

{

returnstring.length;

}

//内存溢出的时候调用

- (void)didReceiveMemoryWarning {

[superdidReceiveMemoryWarning];

}

//按钮UIButton (UIButton *button =[UIButton buttonWithType:UIButtonTypeInfoDark];

)

//frame表明了控件的坐标和宽高(CGRect类型)

UIButton*button = [[UIButtonalloc]initWithFrame:CGRectMake(20(x),50(y),80(宽),80(高))];

[button setTitle:@"火影" forState:UIControlStateNormal];

//根据名字加载图片

UIImage*image = [UIImageimageNamed:@"left_normal"];

//给按钮设置背景图片(forState表明按钮状态)

[buttonsetBackgroundImage:ImageforState:(UIControlStateNormal)];

//给按钮设置背景图片颜色

button.backgroundColor = [UIColor redColor];

//按钮的监听

[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"];

//设置imageView显示的图片

imageview.image= image1;

[self.viewaddSubview:imageview];

//标签UILabel

UILabel*label = [[UILabelalloc]initWithFrame:CGRectMake(150(x),270(y),150(宽),30(高))];

//设置标签文本

label.text=@"XUANG";

//设置居中方式

label.textAlignment=NSTextAlignmentCenter;

//设置标签文本颜色

label.textColor= [UIColorredColor];

//添加标签到视图上面

[self.viewaddSubview:label];}

你可能感兴趣的:(无标题文章)