您的位置:首页 > 其它

OC_变量类型

2019-03-14 17:00 29 查看

——————————————————————————
字符串的替代符为:@,且每个字符串前面都要加@,而且字符串的变量为指针形式

//字符串 对象类型
NSLog(@"hello,world");
NSString * mystring = @"dajiahao";
NSLog(@"myfirst:%@", mystring);
//字符串拼接
NSString * mystring1 = @"hello";
NSString * mystring2 = @"world";
NSString * mystring3 = [NSString stringWithFormat:@"%@ %@",mystring1,mystring2];
NSLog(@"mystring3:%@",mystring3);
//字符串截取
NSString * mystring4 = [mystring3 substringFromIndex:5];
NSLog(@"substring3:%@",mystring4);
//字符串替换
NSString * mystring5 = [mystring3 stringByReplacingOccurrencesOfString:@"world" withString:@"haha"];
NSLog(@"%@", mystring5);
//字符
char mychar = 'c';
NSLog(@"char:%c",mychar);

注意整数的替代符是 %ld

//整数
NSUInteger myint1 = 10;
NSLog(@"myint1:%ld",myint1);

float小数点后7位,double小数点后15位

//小数
CGFloat myfloat = 3.13132123;
double mydouble = 3.13132123;
NSLog(@"myfloat:%f,%f",myfloat,mydouble);

数组不可变数组,长度不能改变,只能存放对象类型的值,例如字符串,不能保存整型这种,需要转换成对象类型才行。

//数组 不能存放inter这种非对象类型,不可改变
NSArray *arr = [[NSArray alloc] initWithObjects:@"a",@"b",@"c", nil];
NSLog(@"%@,%@,%@",arr[0],arr[1],arr[2]);
//可变数组 不能存放inter这种非对象类型,不可改变
NSMutableArray *muarr = [NSMutableArray array];
[muarr addObject:@"a"];//添加
[muarr addObject:@"b"];
[muarr addObject:@"c"];
NSLog(@"%@,%@,%@",muarr[0],muarr[1],muarr[2]);
[muarr removeObject:0];
NSLog(@"%@,%@",muarr[0],muarr[1]);
//字典
NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:@"value1",@"key1",@"value2",@"key2", nil];
NSLog(@"%@",[dict objectForKey:@"key1"]);
NSLog(@"%@",[dict objectForKey:@"key2"]);

NSDictionary *dict1 = [[NSDictionary alloc] initWithObjects:@[@"value1",@"value2"] forKeys:@[@"key1",@"key2"]];
NSLog(@"%@",[dict objectForKey:@"key1"]);
NSLog(@"%@",[dict objectForKey:@"key2"]);
//可变字典
NSMutableDictionary *mudict = [[NSMutableDictionary alloc] init];
[mudict setObject:@"value1" forKey:@"key1"];//添加
[mudict setObject:@"value2" forKey:@"key2"];
NSLog(@"%@",[dict objectForKey:@"key1"]);
NSLog(@"%@",[dict objectForKey:@"key2"]);
[mudict removeObjectForKey:@"key2"];//删除

再加个句式,OC和python等一样,循环语句也可以用for in结构

//循环语句
NSArray *arr = [[NSArray alloc] initWithObjects:@"a",@"b",@"c", nil];
for (NSString *value in arr){
NSLog(@"%@", value);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: