虽然简单,但是好多东西用到的时候再要去网上搜,觉得挺麻烦的。所以就自己整理了一份,以便不时之需。
1.NSString
// ------------------NSString常见用法------------------
NSString *str1 = @"beiJING";
NSString *str2 = @"SHANGhai";
//全部转化为大写
NSLog(@"%@",[str1 uppercaseString]);
//全部转化为小写
NSLog(@"%@",[str2 lowercaseString]);
//首字母大写
NSLog(@"%@",[str1 capitalizedString]);
//比较两个字符串内容是否相同
BOOL b = [str1 isEqualToString:str2];
if (b) {
NSLog(@"str1跟str2相同");
}else{
NSLog(@"str1跟str2不相同");
}
//两个字符串内容比较
NSComparisonResult result = [str1 compare:str2];
if (result == NSOrderedAscending)
{
NSLog(@"str1<str2");
}else if (result == NSOrderedSame)
{
NSLog(@"str1=str2");
}else
{
NSLog(@"str1>str2");
}
//忽略大小写进行比较,返回值跟compare一样
result = [str1 caseInsensitiveCompare:str2];
if (result == NSOrderedAscending)
{
NSLog(@"str1<str2");
}else if (result == NSOrderedSame)
{
NSLog(@"str1=str2");
}else
{
NSLog(@"str1>str2");
}
//判断字符串是否以指定字符串开头和结尾
if ([str1 hasPrefix:@"bei"]) {
NSLog(@"str1是以“bei”开头的字符串");
}else
{
NSLog(@"str1不是以“bei”开头的字符串");
}
if ([str2 hasSuffix:@"shang"]) {
NSLog(@"str2是以“HAI”结尾的字符串");
}
else
{
NSLog(@"str2不是以“HAI”结尾的字符串");
}
//判断字符串是否包含指定字符串,返回位置和长度
NSRange range = [str1 rangeOfString:@"bei"];
NSLog(@"%@",NSStringFromRange(range));
//反向搜索
range = [str2 rangeOfString:@"Gh" options:NSBackwardsSearch];
NSLog(@"%@",NSStringFromRange(range));
//指定范围进行搜索
range = NSMakeRange(0, 7);
range = [str1 rangeOfString:@"iJ" options:NSBackwardsSearch range:range];
NSLog(@"%@",NSStringFromRange(range));
//字符串的截取
NSRange range1 = NSMakeRange(6, 2);
NSString *str3 = @"guangZHOU";
NSLog(@"%@",[str3 substringFromIndex:3]);
NSLog(@"%@",[str3 substringToIndex:4]);
NSLog(@"%@",[str3 substringWithRange:range1]);
//用指定字符串分割字符串,返回一个数组
NSArray *arr = [NSArray arrayWithObjects:@"1",@
"2",@"3",@"4",nil];
[arr componentsJoinedByString:@","];
NSLog(@"%@",arr);
//将数组中的字符串组成一个文件路径
NSMutableArray *components = [NSMutableArray array]; // ???
[components addObject:@"Users"];
[components addObject:@"CentralPerk"];
[components addObject:@"DeskTop"];
NSString *path = [NSString pathWithComponents:components];
NSLog(@"%@",path);
//将一个路径分割成一个数组
NSArray *arr1 = [path pathComponents];
NSLog(@"%@",arr1);
//判断是否为绝对路径(依据:是否以“/”开始)
path = @"/Users/CentralPerk/DeskTop";
NSLog(@"%i",[path isAbsolutePath]);
//获取最后一个目录
NSLog(@"%@",[path lastPathComponent]);
//删除最后一个目录
NSLog(@"%@",[path stringByDeletingLastPathComponent]);
//拼接一个目录
NSLog(@"%@",[path stringByAppendingString:@"zpf"]);
NSLog(@"%@",[path stringByAppendingPathComponent:@"zpf"]);
NSLog(@"%@",[path stringByAppendingFormat:@"%@%@",@"B",@"a"]);
//获取拓展名
NSString *str4 = @"Users/CentralPerk/Desktop/test.txt";
NSLog(@"%@",[str4 pathExtension]);
//添加拓展名
NSLog(@"%@",[str4 stringByAppendingPathExtension:@"mp4"]);
//删除拓展名
NSLog(@"%@",[str4 stringByDeletingPathExtension]);
//字符串转化为int double folat
NSString *str5 = @"123";
NSLog(@"%i",[str5 intValue]);
NSLog(@"%zi",[str5 length]); // ???-> zi
//取出指定位置的字符
unichar c = [str5 characterAtIndex:0];
NSLog(@"%c",c);
//转化为C语言的字符串
const char *a = [str5 UTF8String];
NSLog(@"%s",a);
2.NSArray和NSMutableArray
// ------------------NSArray常见用法------------------
// NSMutableArray;
//类方法数组创建
NSArray *array1 = [NSArray arrayWithObject:@"obj"];
NSLog(@"%@",array1);
NSArray *array2 = [NSArray arrayWithObjects:@"obj1",@"obj2",@"obj3", nil];
NSLog(@"%@",array2);
NSArray *array3 = [NSArray arrayWithArray:array1];
NSLog(@"%@",array3);
//实例方法创建数组
NSArray *array4 = [[NSArray alloc]initWithObjects:@"obj4",@"obj5",@"obj6", nil];
NSLog(@"%@",array4);
//数组个数
NSLog(@"array4 count is %ld",[array4 count]);
//访问元素
NSLog(@"obj at index :%@",[array4 objectAtIndex:2]);
//追加字符串,返回新的array对象
NSArray *array5 = [array4 arrayByAddingObject:@"abc"];
NSLog(@"array5 is %@",array5);
//追加数组,返回新的array对象
array5 = [array2 arrayByAddingObjectsFromArray:array4];
NSLog(@"array5 = array2 + array4 = %@",array5);
//根据指定的字符串连接数组元素
NSString *ConnectString = [array5 componentsJoinedByString:@"|"];
NSLog(@"ConnectString is %@",ConnectString);
//是否包含指定对象
BOOL isContain = [array5 containsObject:@"abc"];
if (isContain) {
NSLog(@"array5 里面包含 字符串“abc”");
}else
{
NSLog(@"array5 里面不包含 字符串“abc”");
}
//查找某个对象所在索引
NSLog(@"字符串“abc”在array5的第%ld的位置",[array5 indexOfObject:@"obj2"]+1);
//最后一个元素
NSLog(@"最后一个元素:%@",[array5 lastObject]);
//快速遍历数组
for (id element in array5) {
NSLog(@"element is %@",element);
}
// ------------------NSMutableArray常见用法------------------
//可变数组(期望容量值为3)
NSMutableArray *mutableArray = [NSMutableArray arrayWithCapacity:3];
//直接添加
[mutableArray addObject:@"aaa"];
[mutableArray addObject:@"bbb"];
[mutableArray addObject:@"ccc"];
NSLog(@"mutableArray is %@",mutableArray);
//插入元素
[mutableArray insertObject:@"eee" atIndex:0];
NSLog(@"mutableArray is %@",mutableArray);
//移除指定元素
[mutableArray removeObject:@"bbb"];
NSLog(@"mutableArray is %@",mutableArray);
//移除指定下标元素
[mutableArray removeObjectAtIndex:2];
NSLog(@"mutableArray is %@",mutableArray);
//移除最后一个元素
[mutableArray removeLastObject];
NSLog(@"mutableArray is %@",mutableArray);
//添加数组
[mutableArray addObjectsFromArray:array5];
NSLog(@"mutableArray is %@",mutableArray);
//移除指定数组中的内容
[mutableArray removeObjectsInArray:array2];
NSLog(@"mutableArray is %@",mutableArray);
//指定索引替换对象
[mutableArray replaceObjectAtIndex:0 withObject:@"hello"];
NSLog(@"mutableArray is %@",mutableArray);
//删除全部对象
[mutableArray removeAllObjects];
NSLog(@"mutableArray is %@",mutableArray);
3.NSDictionary和NSMutableDictionary
// ------------------NSDictionary常见用法------------------
//创建字典
NSDictionary *dic = [NSDictionary dictionaryWithObject:@"value" forKey:@"key"];
NSLog(@"dic is %@",dic);
//创建多个字典
NSDictionary *dic2 = [NSDictionary dictionaryWithObjectsAndKeys:@"value1",@"key1",@"value2",@"key2",@"value3",@"key3", nil<span style="white-space:pre"> </span>];
NSLog(@"dic2 is %@",dic2);
//根据现有的字典创建字典
NSDictionary *dic3 = [NSDictionary dictionaryWithDictionary:dic];
NSLog(@"dic3 is %@",dic3);
//根据key值获取value值
NSLog(@"key3 value is %@",[dic2 objectForKey:@"key3"]);
//获取字典数量
NSLog(@"dic2 count is %ld",[dic2 count]);
//所有的键集合
NSArray *allkey = [dic2 allKeys];
NSLog(@"dic2 allKeys is %@",allkey);
//所有值的集合
NSArray *allValue = [dic2 allValues];
NSLog(@"dic2 allValue is %@",allValue);
// ------------------NSDictionary常见用法------------------
//添加现有的字典数据
NSMutableDictionary *mutableDic = [[NSMutableDictionary alloc]initWithObjectsAndKeys:@"mvalue1",@"mkey1",@"mvalue2",@"mkey<span style="white-space:pre"> </span>2", nil];
//添加现有的字典数据
[mutableDic addEntriesFromDictionary:dic2];
NSLog(@"mutableDic is %@",mutableDic);
//添加新的键值对象
[mutableDic setValue:@"mvalue3" forKey:@"mkey3"];
NSLog(@"mutableDic is %@",mutableDic);
//以新的字典数据覆盖旧的字典数据
[mutableDic setDictionary:dic2];
NSLog(@"mutableDic is %@",mutableDic);
//根据key值删除value值
[mutableDic removeObjectForKey:@"key2"];
NSLog(@"mutableDic is %@",mutableDic);
//快速遍历字典
for (id key in mutableDic) {
NSLog(@"key = %@,value = %@",key,[mutableDic objectForKey:key]);
}
//根据key数组删除元素
NSArray *allKeys = [mutableDic allKeys];
[mutableDic removeObjectsForKeys:allKeys];
NSLog(@"this is nil %@",mutableDic);
//删除字典中所有元素
[mutableDic removeAllObjects];
NSLog(@"this is nil %@",mutableDic);
还有NSData 没有整理出来,有时间整理出来再更新。。。。