1.1 NSRange

NSRange range = NSMakeRange(2, 4);//location=2,len=4

   NSString *str = @"i love oc";
//查找对应的字符串的位置location,length
range = [str rangeOfString:@"love"];
4.2 NSMutableString

// NSMutableString可变字符串

NSMutableString *s1 = [NSMutableString stringWithFormat:@"age is 10"];

[s1 appendString:@"love"];

 NSRange range = [s1 rangeOfString:@"love"];

[s1 deleteCharactersInRange:range];

4.3 NSString

//类方法构造字符串

NSString *str = [NSString stringWithFormat:@"age is %d",10];

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

 //对象方法构造字符串

NSString *str1 = [[NSString alloc]initWithContentsOfFile:@"/Users/zhangct/Desktop/1.txt"encoding:NSUTF8StringEncoding error:nil];

 NSString *url = @"file:///Users/zhangct/Desktop/1.txt";

NSString *str2 = [[NSString alloc]initWithContentsOfURL:urlencoding:NSUTF8StringEncoding error:nil];

 //讲字符串写入文件

[str2 writeToFile]

 //常用属性
[s length]
[s characterAtIndex:i]
 //结构体类型转换成字符串
NSStringFromCGRect(<#CGRect rect#>)
NSStringFromCGPoint(<#CGPoint point#>)
4.4 NSString (NSExtendedStringDrawing)
//根据字体计算字符串的显示宽高
NSString *nikeName = @"内涵段子";
NSDictionary *attri = @{NSFontAttributeName: [UIFont systemFontOfSize:13]};
CGSize nikeSize = [nikeName boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) 
options:NSStringDrawingUsesLineFragmentOrigin attributes:attri context:nil].size;
4.5 NSBundle
//获取文件绝对路径
NSString *path = [[NSBundle mainBundle] pathForResource:@"app.plist" ofType:nil];
//加载xib试图文件,返回对象数组
NSArray *objs = [[NSBundle mainBundle] loadNibNamed:@"HMAppView" owner:nil options:nil];
// infoDictionary获取info.plist的字典信息
NSDictionary *dict = [[NSBundle mainBundle] infoDictionary];
// objectForInfoDictionaryKey获取info.plist字典中某一个键值
NSString *versonKey = (__bridge NSString *)kCFBundleVersionKey;
NSString *verson = [[NSBundle mainBundle] objectForInfoDictionaryKey:versonKey];
1.2 NSArray和NSMutableArray
//读取文件内容填充NSArray集合
NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];
 //向数组中每个元素发送SEL消息
array makeObjectsPerformSelector:@selector(<#selector#>)
[self.answerView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
1.3 NSSet
//获取集合个数
NSLog(@"set1 count :%d", set1.count);
//以数组的形式获取集合中的所有对象
NSArray *allObj = [set2 allObjects];
NSLog(@"allObj :%@", allObj);
//获取任意一对象
NSLog(@"anyObj :%@", [set3 anyObject]);
//是否包含某个对象
NSLog(@"contains :%d", [set3 containsObject:@"obj2"]);
//是否有交集
NSLog(@"intersect obj :%d", [set1 intersectsSet:set3]);
//是否完全匹配
NSLog(@"isEqual :%d", [set2 isEqualToSet:set3]);
//set2是否是set1的子集合
NSLog(@"isSubSet :%d", [set2 isSubsetOfSet:set1]);
1.4 NSMutableSet
//集合元素相减
[mutableSet1 minusSet:mutableSet2];
//只留下相等元素
[mutableSet1 intersectSet:mutableSet3];
//合并集合
[mutableSet2 unionSet:mutableSet3]
//删除指定元素
[mutableSet2 removeObject:@"a"];
//删除所有数据
[mutableSet2 removeAllObjects];
1.5 NSDictionary和NSMutableDictionary

NSDictionary *dict = @{@"name": @"zhangct", @"age": @20};

 NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setValue:@"zhangct" forKey:@"name"];
1.6 NSNumber

NSNumber *num1 = [NSNumber numberWithInt:10];

[num1 intValue];

 NSNumber *num2 = [NSNumber numberWithFloat:1.0];

[num2 floatValue];

 NSNumber *num3;

int age = 20;

num3 = @(age);

num3 = @100;

1.7 NSValue

CGPoint p = CGPointMake(10, 20);

NSValue *value = [NSValue valueWithPoint:p];

 NSLog(@"%@", NSStringFromPoint([value pointValue]));
1.8 NSDate

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

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

 NSDate *date = [NSDate date];
NSLog(@"%@", [formatter stringFromDate:date]);
4.6 NSObject
4.6.1 NSObject

[self performSelector:@selector(onClick:value1:) withObject:nil withObject:nil];

performSelector是执行self对象中的onClick:value1:方法,最多支持传递2个参数.

4.6.2 NSObject(NSDelayedPerforming)

//播放完毕后延迟1秒执行释放内存

[self.tom performSelector:@selector(setAnimationImages:) 
withObject:nil afterDelay:self.tom.animationDuration + 1.0f];

4.6.3 NSObject(NSKeyValueCoding)
//单个赋值
[self setValue:dict[@"answer"] forKey:@"answer"];

[self setValue:dict[@"title"] forKey:@"title"];

 //ForKeyPath取值,返回Person的car name
[p valueForKeyPath:@"car.name"]
 //ForKeyPath取值返回title数组
carsGroups valueForKeyPath:@"title"
 
//字典转模型,使用此方法要保证属性名称和字典的key同名
[self setValuesForKeysWithDictionary:dict];
 //模型转字典
NSDictionary *dict = [p dictionaryWithValuesForKeys:@[@"name", @"age"]];
4.6.4 NSObject(UINibLoadingAdditions)

//当xib文件加载完毕后执行

- (void)awakeFromNib;
 
4.8 NSIndexPath

//根据分组和行号初始化对象

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:10 inSection:0];

 //返回分组

indexPath.section

 //返回行号
indexPath.row

iOS-Foundation各种NS的更多相关文章

  1. iOS Foundation 框架概述文档:常量、数据类型、框架、函数、公布声明

    iOS Foundation 框架概述文档:常量.数据类型.框架.函数.公布声明 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业 ...

  2. iOS Foundation 框架基类

    iOS Foundation 框架基类 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协议 转 ...

  3. iOS Foundation框架 -2.常用集合类简单总结

    Foundation框架中常用的类有:NSString.NSArray.NSSet.NSDictionary 以及它们对应的子类 NSMutableString.NSMutableArray.NSMu ...

  4. iOS Foundation框架简介 -1.常用结构体的用法和输出

    1.安装Xcode工具后会自带开发中常用的框架,存放的地址路径是: /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.plat ...

  5. iOS Foundation框架 -1.常用结构体的用法和输出

    1.安装Xcode工具后会自带开发中常用的框架,存放的地址路径是: /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.plat ...

  6. IOS - Foundation和Core Foundation掺杂使用桥接

    Foundation和Core Foundation掺杂使用桥接 Toll-Free Bridging 在cocoa application的应用中,我们有时会使用Core Foundation(CF ...

  7. iOS Foundation框架 -3.利用NSNumber和NSValue将非OC对象类型数据存放到集合

    1.Foundation框架中提供了很多的集合类如:NSArray,NSMutableArray,NSSet,NSMutableSet,NSDictionary,NSMutableDictionary ...

  8. iOS - Foundation相关

    1.NSString         A.创建的方式:            stringWithFormat:格式化字符串  ,创建字符串对象在堆区域            @"jack& ...

  9. iOS Foundation框架 -4.NSDate类的简单用法

    NSDate为日期时间类对象,简单操作: 注意:直接NSLog输出NSDate对象,默认是以0时区为标准,因此会比北京时间少8小时 1.将Date格式转换为自定义格式的字符串格式 // 自定义Date ...

  10. IOS Note - Core NS Data Types

    NSString (Immutable)NSMutableString (rarely used)NSNumberNSValueNSData (bits)NSDateNSArray (Immutabl ...

随机推荐

  1. 多任务5-协程(IO密集型适用)--gevent完成多任务及monkey补丁

    代码: import gevent def f1(n): for i in range(n): print(gevent.getcurrent(),i) gevent.sleep(1) def f2( ...

  2. 生成器调试---send方式

    调试 def creat_num(all_num): a, b = 0, 1 current_num = 0 while current_num < all_num: ret = yield a ...

  3. 密码加密与微服务鉴权JWT详细使用

    [TOC] 1.1.了解微服务状态 微服务集群中的每个服务,对外提供的都是Rest风格的接口,而Rest风格的一个最重要的规范就是:服务的无状态性. 什么是无状态? 1.服务端不保存任何客户端请求者信 ...

  4. [NOI2019]序列(模拟费用流)

    题意: 有两个长度为n的序列,要求从每个序列中选k个,并且满足至少有l个位置都被选,问总和最大是多少. \(1\leq l\leq k\leq n\leq 2*10^5\). 首先,记录当前考虑到的位 ...

  5. Hive的自定义函数

    功能: 通过人的生日,算出人的生肖和星座. 先在hive中创建一个表: 往这表中导入数据: 导入的数据为: 可以成功查询: 编写自定义函数代码:如下 package cn.tendency.wenzh ...

  6. web+文件夹上传

    一. 大文件上传基础描述: 各种WEB框架中,对于浏览器上传文件的请求,都有自己的处理对象负责对Http MultiPart协议内容进行解析,并供开发人员调用请求的表单内容. 比如: Spring 框 ...

  7. 10分钟教你用eclipse上传代码到GitHub

    关注我们的公众号哦!获取更多精彩消息! 好久没有更新了,这两天小编在整理以前的代码,上传到GitHub做备份. 加上现在GitHub的私有仓库不是免费了嘛,所以今天顺便给大家讲讲怎么用eclipse上 ...

  8. Xmind8安装

    现在新版安装极其简单.是deb安装包Xmind8安装小书匠 kindle 参照官网安装方法,在此记录下来,方便自己查找. 流程: 55ccaad0655d256ac5fb9fea8aa8569d.pn ...

  9. java.lang.ClassNotFoundException: org.springframework.kafka.core.KafkaAdmin

    <dependency> <groupId>org.springframework.kafka</groupId> <artifactId>spring ...

  10. tsar安装和使用

    Tsar简介 Tsar是淘宝自己开发的一个采集工具,主要用来收集服务器的系统信息(如cpu,io,mem,tcp等),以及应用数据(如squid haproxy nginx等). 收集到的数据存储在磁 ...