1. NSString 常用方法
//字符串末尾拼接字符串
NSString *newStr = [string1 stringByAppendingString:string2];
//字符串成员的引用
[string characterAtIndex:1]
//比较两个字符串是否完全相同
BOOL bl = [string1 isEqualToString:string2];
//比较两个字符串的大小
NSComparisonResult result = [string1 compare:string2];
NSOrderedAscending: string1 < string2
NSOrderedSame: string1 == string2
NSOrderedDescending: string1 > string2
//忽略大小写的比较
NSComparisonResult result = [string1 caseInsensitiveCompare:string2];
//字符串中查找子串
NSRange range = [string rangeOfString:@"substring"];
//判断是否包含某个前缀
BOOL bl = [str hasPrefix:@"txt"];
//判断是否包含某个后缀
BOOL bl = [str hasSuffix:@"txt"];
//字符串转数字
[string intValue]
//将字符串中的小写字母转换成大写字母
[string uppercaseString]
//将字符串中的字母转换成小写字母
[string lowercaseString]
//首字母转大写
[string capitalizedString]
//字符串的提取:从某个位置开始提取,一直到字符串的末尾
NSString *subFrom = [string substringFromIndex:0];
//从字符串的开始提取到某个位置结束
NSString *subTo = [string substringToIndex:[string length]];
#pragma mark -NSMutableString
//字符串末尾拼接字符串
[mutableString1 appendString:@"string"];
//插入字符串
[mutableString insertString:@"string" atIndex:3];
//删除字符串
NSRange range = {19, 3};
[mutableString deleteCharactersInRange:range];
//修改整个字符串
[mutableString setString:@"modified”];
2. NSArray 常用方法
//数组成员的引用
[array objectAtIndex:0]
//根据对象找索引
[array indexOfObject:@"object"]
//判断某个元素是否在数组中
[array containsObject:@"object"]
//获取数组中最后一个元素
[array lastObject]
//将字符串进行切割
NSArray *array = [string componentsSeparatedByString:@"$"];
//按照字符集进行切割
NSArray *array = [string componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"$#@&"]];
//将数组每个元素用空格拼接成字符串
NSString *stringFromArray = [arrayFromString componentsJoinedByString:@" "];
#pragma mark -NSMutableArray
//数组末尾增加一个元素
[mutableArray addObject:@"object"];
//数组的末尾增加一个数组
[mutableArray addObjectsFromArray:array];
//数组中插入一个元素
[mutableArray insertObject:@"object" atIndex:2];
//删除数组中的指定元素,有多少删多少
[mutableArray removeObject:@"object"];
//删除指定下标的元素
[mutableArray removeObjectAtIndex:2];
//删除指定范围内的元素
NSRange range = {0, 2};
[mutableArray removeObjectsInRange:range];
//删除最后一个元素
[mutableArray removeLastObject];
//删除整个数组
[mutableArray removeAllObjects];
//数组成员的替换
[mutableArray replaceObjectAtIndex:0 withObject:@"object"];
//数组成员的交换,参数是交换的两个成员下标
[mutableArray exchangeObjectAtIndex:1 withObjectAtIndex:2];
3. NSDictionary 常用方法
//字典的创建
NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:
@"value1", @"key1",
@"value2", @"key2", nil];
//字典键值对的个数
[dictionary count]
//根据key找value
[dictionary objectForKey:@"key"]
//获取字典中所有的key值
NSArray *allKeys = [dictionary allKeys];
#pragma mark -NSMutableDictionary
//添加整个字典到可变字典中
[mutableDictionary addEntriesFromDictionary:dic];
/* 添加或修改键值对:
如果字典中存在key,那就就修该对应的value值,
如果字典中不存在key, 那就进行添加键值对 */
[mutableDictionary setObject:@"value" forKey:@"key"]
//根据key 删除键值对
[mutableDictionary removeObjectForKey:@"key"];
//删除所有的键值对
[mutableDic removeAllObjects];
//修改整个字典
[mutabelDic setDictionary:dic];
4. 简单的数据持久化
//NSData可以存储图片、声音等二进制数据
//dataToString
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
NSString *strFormData = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
//plist文件的创建与读取
//将该字典写入到list文件中,并读取plist文件中内容
void dictionaryToPlist(void) {
NSString *pathDesktop = @"/Users/mac/Desktop";
NSArray *array1 = [NSArray arrayWithObjects:@"one", @"two", nil];
NSArray *array2 = [NSArray arrayWithObjects:@"beijing", @"shanghai", nil];
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
array1, @"key1",
array2, @"key2", nil];
[dictionary writeToFile:[pathDesktop stringByAppendingString:@"/1.plist"] atomically:YES];//持久化字典对象 (写入到plist文件中)
NSDictionary *dicFromPlist = [NSDictionary dictionaryWithContentsOfFile:[pathDesktop stringByAppendingString:@"/1.plist"]];// 读取plist内容
NSLog(@"%@", dicFromPlist);
}