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. Time Intersection

    Description Give two users' ordered online time series, and each section records the user's login ti ...

  2. 02_Tutorial 2: Requests and Responses 请求和响应

    1.请求和响应 1.文档 https://www.django-rest-framework.org/tutorial/2-requests-and-responses/ https://q1mi.g ...

  3. Inellij idea创建javaWeb以及Servlet简单实现

    版权声明:本文为博主原创文章,未经博主允许不得转载. 准备:1. 安装jdk1.7 2. 安装tomcat1.8 一.创建并设置javaweb工程 1.创建javaweb工程File --> N ...

  4. html与HTML5的区别

    文档的类型声明不同 html:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.or ...

  5. LibreOJ #6165. 一道水题

    二次联通门 : LibreOJ #6165. 一道水题 /* LibreOJ #6165. 一道水题 欧拉线性筛 其实题意就是求区间[1, n]所有数的最小公倍数 那么答案就是所有质因子最大幂次的乘积 ...

  6. qt 读取xml文件中文问题

    1.保存文件格式为UTF-8 2.文件流打开时设置 QFile file(fileName); if (!file.open(QIODevice::ReadOnly)) { qDebug() < ...

  7. python qq发消息

    # 原理是先将需要发送的文本放到剪贴板中,然后将剪贴板内容发送到qq窗口 # 之后模拟按键发送enter键发送消息 import win32gui import win32con import win ...

  8. Pythton3实例

    计算1-100之和 #add.py n = 0 sum = 0 for n in range(0,101): sum += n print(sum) 实现99乘法法则 #mul.py i = 1 wh ...

  9. P3144 [USACO16OPEN]关闭农场——离线,并查集

    https://www.luogu.org/problem/P3144 每次关闭一个农场,农场之间有边相连,问每次关闭后开着的农场是否是一个连通块: 数据小,离线搞: 我们先记录删的顺序,然后倒着来, ...

  10. 修改rabbitmq web-UI端口号

    一.拷贝配置文件 [root@test03 ~]# cd /usr/share/doc/rabbitmq-server-3.6.15/ [root@test03 rabbitmq-server-3.6 ...