Foundation框架精选

来源:互联网 发布:windows 纯净 下载 编辑:程序博客网 时间:2024/05/17 02:12
一、NSString用法1.字符串比较函数:NSComparisonResult result = [str1 compare:str2 options:NSCaseInsensitiveSearch|NSNumericSearch];返回值:NSOrderedAscending(str1<str2)NSOrderedDescending(str1>str2)NSOrderedSame(str1 = str2)2. 判读字符串是否相等:[str1 isEqualToString:str3]3. 检测字符串前后缀:[url hasPrefix:@"http://"]; 字符串是否以http://开头[imgName hasSuffix:@".jpg"]; 检测字符串是否以.jpg结尾4.查找字符串的位置NSRange range = [str1 rangeofString:str2]; //str1 中找str25.字符串截取NSString *str1 = [str substringFromIndex:5];//从xx位置开始到搜字符结束NSString *str2 = [str substringToIndex:5];// 从开始位置到xx位置结束NSRange r1 = {3,4};NSString *str3 = [str substringWithRange:r1];// 截取一个range范围6. 将字符串转成int类型int b = [str intValue]; // 前提是字符串是数值类型二、NSRange1. NSRange的结构体初始化NSRange r4 = NSMakeRange(3,3)2.打印NSSrange:NSStringFromRange(r4)三、NSURL1. 通过urlwithstring创建NSURLNSURL *url = [NSURL URLWithString:@"sms://10086"];三、NSMutableString1. 格式化和拼接字符串:NSMutableString *str = [NSMutableString string];[str appendFormat:@"http://www.baidu.com/%d",100];2. 删除一部分内容:[str deleteCharactersInRange:NSMakeRange(3, 4)];3. 插入一个字符串:[str insertString:@"p://" atIndex:3];4. 将某个字符串内容替换成指定内容:[str replaceCharactersInRange:NSMakeRange(11, 5) withString:@"itcast"];四、NSArray基本使用1. 直接初始化NSArray *arr1 = [NSArray array];2. 用一个数组可以创建另外一个数组NSArray *arr5 = [NSArray arrayWithArray:arr3];3. 简化数组元素的创建:NSArray *arr = @[@"1",@"one",@"3",@4,@"ONE"];4. 通过下标访问数组元素:NSArray *arr = [@"one",@"two",@"three"];arr[下标];5. 数组的遍历方式:普通for循环,取角标快速枚举法:for(NSString *str in arr){}6. NSArray读写文件:将数组写入到文件中:BOOL isWrite = [array writeToFile:@"/Users/zhaoxiaohu/Desktop/arr.xml" atomically:YES];从文件中,读取一个数组信息NSArray *readArr = [NSArray arrayWithContentsOfFile:@"/Users/zhaoxiaohu/Desktop/arr.xml"];7. 快速包装数组:NSArray *arr = @[@1,@2,@3,@4];将数组连在一起变成NSStringNSString *str = [arr componentsJoinedByString:@"-"];8. 将字符串分割成数组str2 = @"400#800#12580#400#888#11200";NSArray *arr3 = [str2 componentsSeparatedByString:@"#"];9. 取数组的元素方法:[arr firstobject] //取出第一个元素[arr lastobject] // 取出最后一个元素五、NSMutableArray的基本使用1. 创建数组的时候指定放置多少个元素:NSMutableArray *arr4 = [NSMutableArray arrayWithCapacity:5];[arr4 addobject:@"fengjie"]2. 插入某个元素到数组当中[arr1 insertObject:@"fengjie" atIndex:0];[arr1 removeAllobjects]; // 移除数组中所有的元素3. 判断数组中是否包含某个元素BOOL isSearch = [arr3 containsObject:@"four"];六,NSDictionary的基本使用1. 普通初始化:NSDictionary *dictionary = [NSDictionary dictionary]2. 快速创建键值对的方法:NSDictionary *dict4 = @{@"zs":@"zhaosi",@"zs":@"zhangsan",@"ls":@"lisi",@"bz":@"banzhang"};dict4.count// 计算字典中键值对的个数3. 获取字典中的某个元素:dict[@"zbz"]4. 把字典写入到文件中:BOOL isWrite = [dict writeToFile:@"/Users/zhaoxiaohu/Desktop/dict.plist" atomically:YES];5. 从文件中读取字典NSDictionary *readDict = [NSDictionary dictionaryWithContentsOfFile:@"/Users/zhaoxiaohu/Desktop/dict.plist"];七、NSMutableDictionary的基本使用1. 可变字典的创建NSMutableDictionary *dict1 = [NSMutableDictionary dictionary]; //创建空字典2. 给可变字典添加键值对[dict1 setValue:@"zhaosi" forKey:@"ls"];3.删除可变字典的键值对[dict1 removeObjectForKey:@"ls"];[dict1 removeAllObjects];4.修改字典中的数据[dict1 setObject:@"zhaosi" forKey:@"ls"];5.获取所有的key值NSArray *arr = [dict1 allkeys][arr containsObject:@"ls"]; // 数组中是否包含@“ls”这个元素八、NSFilemanger 文件管理对象1.单例对象:在程序运行期间,只有一个对象存在NSFileManger *manger = [NSFileManger defaultManger];2.判断某个路劲文件是否存在Bool isExist = [manger fileExistsAtPath:filePath];3.判断是否是一个目录Bool isDir;[manger fileExistsAtPath:filePath diDirectory:&isDir];用法:4.获取文件的属性(信息)NSDictionary *dict = [fm attributesOfItemAtPath:filePath error:nil];5. 根据路径创建文件BOOL isYES = [fm createDirectoryAtPath:createDirPath withIntermediateDirectories:YES attributes:nil error:nil];6.将字符串转换成二进制数据NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];7.移动文件[fm moveItemAtPath:createDirPath toPath:targetPath error:nil];8.删除文件[fm removeItemAtPath:targetPath error:nil];九、沙盒Documents 持久化数据tem临时目录librarycache 缓存preference (系统偏好)// SQlite,coreData2. 沙盒路径获取方法:NSString *sandBoxPath = NSHomeDirectory();// 参数1:要查找的目录 参数2:是否是用户主目录 YES/NO是否获取全路径NSArray *paths= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);NSString *documentPath = [paths lastObject];十、常见结构体结构体:CGPointCGPoint c4 = CGPointMake(10, 10);CGSizeCGSize s2 = CGSizeMake(100, 100);CGRectCGRect r3 = CGRectMake(10, 10, 100, 30);十一、NSNumber的包装1. NSNumber普通包装:NSNumber *intObj = [NSNumber numberWithInt:a];2. NSNumber 快速包装:NSNumber *number = @(18)十二、NSValue的包装NSValue对象的包装:1. 通过CGPoint包装:CGPoint p1 = CGPointMake(20, 50);NSValue *pointVal = [NSValue valueWithPoint:p1];2. 通过CGRect包装NSRect r1 = NSMakeRect(0, 0, 200, 100);NSValue *rectVal[NSValue valueWithRect:r1]十三.NSDate1.设置日期的显示格式NSDateFormatter *formatter = [NSDateFormatter new];formatter.dateFormat = @"yyyy年MM月dd日 HH:mm:ss";formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";4. 计算日历对象:NSDate *d = [NSDate date];//创建日期的对象NSCalendar *cal = [NSCalendar currentCalendar];//cal components:获取日期的哪些部分 fromDate:日期对象NSDateComponents *coms = [cal components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:d];


1 0