美文网首页
Foundation 框架总结

Foundation 框架总结

作者: Phaemlo | 来源:发表于2017-08-16 22:56 被阅读0次

    一、NSString用法

    1.字符串写入文件:

    [str writeToFile:@"/Users/zhaoxiaohu/Desktop/"atomically:YESencoding:NSUTF8StringEncoding error:&err];

    2.字符串从文件读取:

    NSString *str = [NSString stringWithContentsOfFile:@"/Users/zhaoxiaohu/Desktop/str.txt"encoding:NSUTF8StringEncoding error:&err];

    3.字符串比较函数:

    NSComparisonResult result = [str1 compare:str2 options:NSCaseInsensitiveSearch|NSNumericSearch];

    返回值:NSOrderedAscending(str1

    NSOrderedDescending(str1>str2)

    NSOrderedSame(str1 = str2)

    4.判读字符串是否相等:

    [str1 isEqualToString:str3]

    5.检测字符串前后缀:

    [url hasPrefix:@"http://"];字符串是否以http://开头

    [imgName hasSuffix:@".jpg"];检测字符串是否以.jpg结尾

    6.查找字符串的位置

    NSRange range = [str1 rangeofString:str2];//str1中找str2

    7.字符串截取

    NSString *str1 = [str substringFromIndex:5];//从xx位置开始到搜字符结束

    NSString *str2 = [str substringToIndex:5];//从开始位置到xx位置结束

    NSRange r1 = {3,4};

    NSString *str3 = [str substringWithRange:r1];//截取一个range范围

    8.字符串替换://用*替换a

    NSString *newStr = [str stringByReplacingOccurrencesOfString:@"a"withString:@"*"];

    9.将字符串转成int类型

    intb = [str intValue];//前提是字符串是数值类型

    10.c字符串与oc字符串相互替换

    NSString *str = [NSString stringWithUTF8String:s];// c -> oc

    constchar*s1 = [str2 UTF8String];// oc -> c

    二、NSRange

    1.NSRange的结构体初始化

    NSRange r4 = NSMakeRange(3,3)

    2.打印NSSrange:

    NSStringFromRange(r4)

    三、NSURL

    8.通过urlwithstring创建NSURL

    NSURL *url = [NSURL URLWithString:@"sms://10086"];

    NSURL *url = [NSURL URLWithString:@"file:///Users/zhaoxiaohu/Desktop/3.txt"];

    9.获取本地路径:

    NSURL *url = [NSURL fileURLWithPath:@"/Users/zhaoxiaohu/Desktop/4.txt"];

    10.通过url创建字符串

    NSString *str2 = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];

    三、NSMutableString

    1.不可变字符串的字符串创建:

    NSMutableString *str2 = [NSMutableString stringWithFormat:@"Jack"];

    NSMutableString *str3 = [NSMutableString string];

    2.格式化拼接字符串:

    NSMutableString *str = [NSMutableString string];

    [str appendFormat:@"http://www.baidu.com/%d",100];

    3.删除一部分内容:

    [str deleteCharactersInRange:NSMakeRange(3,4)];

    4.插入一个字符串:

    [str insertString:@"p://"atIndex:3];

    5.将某个字符串内容替换成指定内容:

    [str replaceCharactersInRange:NSMakeRange(11,5) withString:@"itcast"];

    6.字符串拼接:

    [str appendString:@"xxxxx"];

    四、NSArray基本使用

    NSArray:

    特点:一旦创建内容不可改变,只能存储oc对象

    1.直接初始化

    NSArray *arr1 = [NSArray array];

    NSArray *arr2 = [[NSArray alloc]init];

    2.创建数组,只有一个元素

    NSArray *arr2 = [NSArray arrayWithObject:@"1"];

    3.创建数组,有多个元素

    // nil表示数组赋值结束

    NSArray *arr3 = [NSArray arrayWithObjects:@"one",@"two",@1,nil];

    4.调用对象方法,创建数组

    //nil Nil NULL  NSNULL

    NSArray *arr4 = [[NSArray alloc] initWithObjects:@"three",[NSNull null],@"four",nil];

    5.用一个数组可以创建另外一个数组

    NSArray *arr5 = [NSArray arrayWithArray:arr3];

    6.简化数组元素的创建:

    NSArray *arr =@[@"1",@"one",@"3",@4,@"ONE"];

    7.获取数组的某个元素:

    NSString *str =[arr objectAtIndex:2];

    str = arr[1]//简化方式访问

    8.通过下标访问数组元素:

    NSArray *arr = [@"one",@"two",@"three"];

    arr[下标];

    9.数组的遍历方式:

    普通for循环,取角标

    快速枚举法:

    for(NSString *strinarr){

    }

    block:

    [arr enumerateObjectsUsingBlock:^(idobj, NSUinteger index,BOOL*shop){

    }];

    10.NSArray读写文件:

    创建数组:

    NSArray *array = [NSArray arrayWithObjects:@"one",@"zbz",@"cgx",@"sb",@"cjk",@"senni",nil];

    将数组写入到文件中:

    BOOLisWrite =  [array writeToFile:@"/Users/zhaoxiaohu/Desktop/arr.xml"atomically:YES];

    从文件中,读取一个数组信息

    NSArray *readArr = [NSArray arrayWithContentsOfFile:@"/Users/zhaoxiaohu/Desktop/arr.xml"];

    11.快速包装数组:

    NSArray *arr =@[@1,@2,@3,@4];

    将数组连在一起变成NSString

    NSString *str = [arr componentsJoinedByString:@"-"];

    12.将字符串分割成数组

    str2 =@"400#800#12580#400#888#11200";

    NSArray *arr3 = [str2 componentsSeparatedByString:@"#"];

    13.取数组的元素方法:

    [arr firstobject]//取出第一个元素

    [arr lastobject]//取出最后一个元素

    五、NSMutableArray的基本使用

    1.可变数组:初始化的时候,直接放置某个元素

    NSMutableArray *arr2 = [NSMutableArray arrayWithObject:@"one"];

    NSMutableArray *arr3 = [NSMutableArray arrayWithObjects:@"one",@"two",@3,nil];

    2.创建数组的时候指定放置多少个元素:

    NSMutableArray *arr4 = [NSMutableArray arrayWithCapacity:5];

    [arr4 addobject:@"fengjie"]

    3.插入某个元素到数组当中

    [arr1 insertObject:@"fengjie"atIndex:0];

    [arr1 removeAllobjects];//移除数组中所有的元素

    4.修改数组当中的某个元素

    [arr3 replaceObjectAtIndex:1withObject:@"four"];

    5.判断数组中是否包含某个元素

    BOOLisSearch = [arr3 containsObject:@"four"];

    6.交换数组当中的元素:

    NSMutableArray *arr5 =[NSMutableArray arrayWithObjects:@1,@2,@3,@4,@5,nil];

    //可以交换数组元素

    [arr5 exchangeObjectAtIndex:0withObjectAtIndex:4];

    六,NSDictionary的基本使用

    1.普通初始化:

    NSDictionary *dictionary = [NSDictionary dictionary]

    2.初始化的时候指定字典中的键值对

    一组键值对:

    NSDictionary *dict2 = [NSDictionary dictionaryWithObject:@"zhangsan"forKey:@"zs"];

    多组键值对:

    NSDictionary *dict3 = [NSDictionary dictionaryWithObjectsAndKeys:@"value1",@"k1",@"value2",@"k2",nil];

    3.快速创建键值对的方法:

    NSDictionary *dict4 =@{@"zs":@"zhaosi",@"zs":@"zhangsan",@"ls":@"lisi",@"bz":@"banzhang"};

    dict4.count//计算字典中键值对的个数

    4.使用枚举遍历字典:

    [dict4 enumerateKeysAndObjectsUsingBlock:^(idkey,idobj,BOOL*stop) {

    NSLog(@"%@ --> %@",key,obj);

    }];

    5.获取字典中的某个元素:

    dict[@"zbz"]

    6.把字典写入到文件中:

    BOOLisWrite = [dict writeToFile:@"/Users/zhaoxiaohu/Desktop/dict.plist"atomically:YES];

    7.从文件中读取字典

    NSDictionary *readDict = [NSDictionary dictionaryWithContentsOfFile:@"/Users/zhaoxiaohu/Desktop/dict.plist"];

    8.读取citys.plist中的内容

    NSDictionary *citysDict = [NSDictionary dictionaryWithContentsOfFile:@"/Users/zhaoxiaohu/Desktop/citys.plist"];

    9.使用block遍历字典中的内容:

    [citysDict enumerateKeysAndObjectsUsingBlock:^(idkey,idobj,BOOL*stop) {

    for(NSString *strinobj) {

    NSLog(@"city = %@",str);

    }

    }];

    七、NSMutableDictionary的基本使用

    1.可变字典的创建

    NSMutableDictionary *dict1 = [NSMutableDictionary dictionary];//创建空字典

    NSMutableDictionary *dict2 = [NSMutableDictionary dictionaryWithCapacity:3];

    2.给可变字典添加键值对

    [dict1 setValue:@"zhaosi"forKey:@"ls"];//因为key值重复了,所以添加不上

    [dict1 setValue:@"lisi"forKey:@"ls"];// 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.判断文件是否可读

    Bool isRead = [manger isReadableFileAtPath:filePath];

    5.判断文件是否可写

    Bool isWrite = [manger isWriteableFileAtPath:filePath];

    6.判断文件是否可以删除

    Bool isDel = [manger isDeletableFileAtPath:filePath];

    用法:

    7.获取文件的属性(信息)

    NSDictionary *dict = [fm attributesOfItemAtPath:filePath error:nil];

    8.使用递归方式获取文件及子目录

    NSArray *subPaths = [fm subpathsAtPath:dirPath];

    9.直接获取文件及子目录

    subPaths = [fm subpathsOfDirectoryAtPath:dirPath error:nil];

    10.获取指定目录下的文件及目录信息(不在获取后代路径)

    subPaths = [fm contentsOfDirectoryAtPath:dirPath error:nil];

    11.根据路径创建文件

    BOOLisYES = [fm createDirectoryAtPath:createDirPath withIntermediateDirectories:YESattributes:nilerror:nil];

    12.将字符串转换成二进制数据

    NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];

    13.移动文件

    [fm moveItemAtPath:createDirPath toPath:targetPath error:nil];

    14.删除文件

    [fm removeItemAtPath:targetPath error:nil];

    九、沙盒

    1.沙盒:

    存放数据的文件夹

    特点:每个应用程序都有自己的沙盒(iOS只能访问自己的沙盒),iOS8开始,开放了几个固定区域

    应用程序包(文件夹):Documents持久化数据

    tem临时目录

    library

    cache缓存

    preference (系统偏好)// SQlite,coreData

    2.沙盒路径获取方法:

    NSString *sandBoxPath = NSHomeDirectory();

    //参数1:要查找的目录参数2:是否是用户主目录YES/NO是否获取全路径

    NSArray *paths= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);

    NSString *documentPath = [paths lastObject];

    十、常见结构体

    结构体:

    CGPoint

    CGPoint c4 = CGPointMake(10,10);

    CGSize

    CGSize s2 = CGSizeMake(100,100);

    CGRect

    CGRect r2 = {{0,1},{20,34}};

    CGRect 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]

    十三.NSDate

    1.设置日期的显示格式

    NSDateFormatter *formatter = [NSDateFormatter new];

    formatter.dateFormat =@"yyyy年MM月dd日HH:mm:ss";

    formatter.dateFormat =@"yyyy-MM-dd HH:mm:ss";

    2.显示出时间

    NSTimeInterval t =60*60*24;

    NSDate *tom = [NSDate dateWithTimeIntervalSinceNow:t];

    NSDate *zuotian = [NSDate dateWithTimeIntervalSinceNow:-t];

    //格式化显示时间

    NSString *timeStr = [formatter stringFromDate:zuotian];

    3.计算昨天的时间:

    NSDate *now = [NSDate date];

    NSDate *zt = [now addTimeInterval:-t];

    timeStr = [formatter stringFromDate:zt];

    4.计算日历对象:

    NSDate *d = [NSDate date];

    //创建日期的对象

    NSCalendar *cal = [NSCalendar currentCalendar];

    //cal components:获取日期的哪些部分fromDate:日期对象

    NSDateComponents *coms =  [cal components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:d];

    相关文章

      网友评论

          本文标题:Foundation 框架总结

          本文链接:https://www.haomeiwen.com/subject/sfiwrxtx.html