Foundation框架精选

一、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 中找str2

5.字符串截取
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]; // 前提是字符串是数值类型




二、NSRange

1. NSRange的结构体初始化
NSRange r4 = NSMakeRange(3,3)

2.打印NSSrange:
NSStringFromRange(r4)



三、NSURL

1. 通过urlwithstring创建NSURL
NSURL *url = [NSURL URLWithString:@"sms://10086"];



三、NSMutableString


1. 格式化和拼接字符串:
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];

将数组连在一起变成NSString
NSString *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临时目录
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 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";



4. 计算日历对象:
NSDate *d = [NSDate date];
//创建日期的对象
NSCalendar *cal = [NSCalendar currentCalendar];
//cal components:获取日期的哪些部分 fromDate:日期对象
NSDateComponents *coms = [cal components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:d];








评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值