1、字符串简单总结
NSString *str1 = @”hello”;
NSString *str2 = @”helloWorld”;
NSString *str3 = @”123″;
BOOL flag = 0;
//比较两个字符串的内容是否相同
flag = [str1 isEqualToString:str2];
NSLog(@”两个字符串的内容是否相同:%i”, flag);
//比较两个字符串的地址是否相同
flag = (str1 == str2);
NSLog(@”str1地址是%p”, str1);
NSLog(@”str2地址是%p”, str2);
NSLog(@”两个字符串的地址是否相同:%i”, flag);
//比较两个字符串的大小
long newFlag = [str1 compare:str2];
NSLog(@”%ld”, [str1 length]);//求字符串的长度
NSLog(@”%ld”, [str2 length]);
NSLog(@”两个字符串的大小是否相同:%ld”, newFlag);
NSLog(@”%c”,[str1 characterAtIndex:1]);//截取第几个字符
int a = [str3 intValue];//将字符串转化为整形数据
NSLog(@”%i”, a);
NSRange range = [str2 rangeOfString:@”oW”];//查找子串【找不到返回NSNotFound,找到返回位置和长度】
if (range.location != NSNotFound) {
NSLog(@”%ld %ld”, range.location, range.length);
}
BOOL flag1 = [str2 hasPrefix:@”he”];//判断字符串是否以指定字符串开头
BOOL flag2 = [str2 hasSuffix:@”uu”];//判断字符串是否以指定字符串结尾
NSLog(@”字符串是否以he开头:%i”, flag1);
NSLog(@”字符串是否以uu结尾:%i”, flag2);
//拼接字符串
// 1 NSString 每次新建一个对象,效率低
NSString *string1 = [str1 stringByAppendingString:str2];
NSLog(@”%@”, string1);
NSString *content = [NSString stringWithFormat:@”productId = %@,isSuccess = %i”,productId,isSuccess];
// 2 NSMutableString 同一个对象,效率高
NSMutableString *content = [NSMutableString stringWithFormat:@”productId = %@,isSuccess = %i”,productId,isSuccess];
2数组简单总结
//NSArray只能存放任意的OC对象,不能存储非OC对象
//NSArray是不可变的,一旦初始化完毕后,它里面的内容就永远是固定的,不能删除里面的元素,也不能再往里面添加元素
//初始化数组
NSArray *arr1 = [NSArray arrayWithObjects:@”hello”, @”world”, @”good”, @”morning”, nil nil];
NSArray *arr2 = @[@”good”, @”morning”, @”hello”, @”world”];
NSLog(@”%@”, [arr1 objectAtIndex:0]);//取出数组的某个元素
NSLog(@”%@”, arr2[0]);
//遍历NASrray数组
for (NSString *obj in arr1) {
NSLog(@”obj = %@”, obj);
}
//对数组进行排序【元素不能是自定义对象】
NSArray *arr3 = [NSArray arrayWithObjects:@(1), @(34), @(56), @(13), @(7), nil nil];
NSArray *newArr2 = [arr3 sortedArrayUsingSelector:@selector(compare:)];
NSLog(@”排序之后的数组是:%@”, newArr2);//输出的格式带有小括号
//将数组写入文件中
BOOL isSuccess = [arr2 writeToFile:@”/Users/coolzhen/Desktop/arr2.plist” atomically:YES];
NSLog(@”isSuccess = %i”, isSuccess);
//从文件中读取一个数组
NSArray *arr4 = [NSArray arrayWithContentsOfFile:@”/Users/coolzhen/Desktop/arr2.plist”];
NSLog(@”%@”, arr4);
//*****************************可变数组*****************************//
//不能通过@[]来创建一个可变数组,因为@[]创建出来的是不可变数组
NSMutableArray *arr11 = [[NSMutableArray alloc] init];
NSMutableArray *arr12 = [NSMutableArray array];
//给可变数组增加内容
[arr11 addObject:@”hello”];
[arr12 addObjectsFromArray:@[@”hello”, @”world”, @”good”, @”afternoon”]];
NSLog(@”%@”, arr11);
NSLog(@”%@”, arr12);
3 字典的简单总结
//创建字典【字典里一个Key对应一个value,存储的都是键值对】
NSDictionary *dict1 = [NSDictionary dictionaryWithObject:@”dog” forKey:@”name”];
NSDictionary *dict2 = [NSDictionary dictionaryWithObjects:@[@”cat”,@”21″] forKeys:@[@”name”, @”age”]];
NSDictionary *dict3 = @{@”name”:@”fish”, @”age”:@”32″};//简写
NSString *name = [dict1 objectForKey:@”name”];//取出字典的某个值
NSLog(@”dict1的名字是%@”,name);
NSLog(@”dict2的名字是%@, 年龄是%@”, [dict2 objectForKey:@”name”], [dict2 objectForKey:@”age”]);
NSLog(@”dict3的名字是%@, 年龄是%@”, dict3[@”name”], dict3[@”age”]);
NSLog(@”dict3的个数:%lu”, [dict3 count]);//获取字典的个数
//遍历字典第一种方法
for (int i = 0; i < dict3.count; i++) {
NSArray *keys = [dict3 allKeys];
NSString *key = keys[i];
NSString *value = dict3[key];
NSLog(@”key = %@, value = %@”, key, value);
}
//遍历字典第二种方法
for (NSString *key in dict2) {
NSString *value = dict2[key];
NSLog(@”key = %@, value = %@”, key, value);
}
//将字典数据写入文件之中
BOOL isSuccess = [dict2 writeToFile:@”/Users/coolzhen/Desktop/dict2.plist” atomically:YES];
NSLog(@”isSuccess = %i”, isSuccess);
//从文件中读取字典数据
NSDictionary *newDict = [NSDictionary dictionaryWithContentsOfFile:@”/Users/coolzhen/Desktop/dict2.plist”];
NSLog(@”%@”, newDict);//字典中保存的数据是无序的,和数组不一样
NSMutableDictionary========
//创建可改变的词典 它是动态的
//需要我们初始化一个空间大小,用dictionaryWithCapacity
NSMutableDictionary * mutableDictionary = [NSMutableDictionary dictionaryWithCapacity:5];
//这里我们给的空间大小是5,当添加的数据超过的时候,它的空间大小会自动扩大
//添加数据,注意:id key 是成对出现的
[mutableDictionary setObject:@”good lucky” forKey:@”why”];
[mutableDictionary setObject:@”bye bye” forKey:@”how”];
输出类型
Char : %s
NSString : %@
long : %ld
bool : %i
类型转换
1、 Int 转NSNumber
int iValue;
NSNumber *number = [NSNumber numberWithInt:iValue];
2、 NSNumber 转Int
int myInt = [number intValue];
3、NSString转int
NSString *stringInt = @”120″;
int ivalue = [stringInt intValue];
4、int转NSString
NSString *string = [NSString stringWithFormat:@”%d”,ivalue];
5、NSString转NSNumber
NSString *stringInt = @”120″;
NSNumber *shareType = @([stringInt integerValue]);
6、NSNumber转NSString
NSNumber* number = @1;
NSString* str = [number stringValue];
原生Json
参考资料:http://blog.sina.com.cn/s/blog_7b9d64af0101ce92.html
// 将字典或者数组转化为JSON串 NSData
– (NSData *)toJSONData:(id)theData{
NSError *error = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:theData options:NSJSONWritingPrettyPrinted error:&error];
if ([jsonData length] > 0 && error == nil){
return jsonData;
}else{
return nil;
}
}
// 将字典或者数组转化为JSON string
– (NSString *)toJSONString:(id)theData{
NSData *data = [self toJSONData: theData];
NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
return jsonString;
}
———————
作者:Leovany
来源:CSDN
原文:https://blog.csdn.net/leovnay/article/details/79948012?utm_source=copy
版权声明:本文为博主原创文章,转载请附上博文链接!