Foundation 框架总结

一、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];

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,015评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,262评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,727评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,986评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,363评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,610评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,871评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,582评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,297评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,551评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,053评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,385评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,035评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,079评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,841评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,648评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,550评论 2 270

推荐阅读更多精彩内容