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. pygame游戏图像绘制及精灵用法

    1精灵文件 plane_sprites.py import pygame class GameSprite(pygame.sprite.Sprite): """飞机大战游 ...

  2. javascript权威指南第21章 Ajax和Comet

    function createXHR(){ if(typeof XMLHttpRequest !='undefined'){ return new XMLHttpRequest(); }else if ...

  3. 转,sql 50道练习题

    SQL语句50题   -- 一.创建教学系统的数据库,表,以及数据 --student(sno,sname,sage,ssex) 学生表--course(cno,cname,tno) 课程表--sc( ...

  4. 用免费的webservice查询天气

    亲测能用URL地址:https://blog.csdn.net/qq_37171353/article/details/79415960 wsimport -s . file:///D:weath.w ...

  5. Cogs 12. 运输问题2(有上下界的有源汇最大流)

    运输问题2 ★★☆ 输入文件:maxflowb.in 输出文件:maxflowb.out 简单对比 时间限制:1 s 内存限制:128 MB 运输问题 [问题描述] 一个工厂每天生产若干商品,需运输到 ...

  6. linux系列(十二):more命令

    1.命令格式: more [-dlfpcsu ] [-num ] [+/ pattern] [+ linenum] [file] 2.命令功能: more命令和cat的功能一样都是查看文件里的内容,但 ...

  7. linux环境下固定ip操作

    背景: 使用虚拟机管理软件VMvare workstation 安装好liunx虚拟机(centos)成功,下面为了固定linux的ip进行一系列设置 参考的文件有部分不是很详细,在借鉴它的基础上进行 ...

  8. scrapy框架自定制命令

    写好自己的爬虫项目之后,可以自己定制爬虫运行的命令. 一.单爬虫 在项目的根目录下新建一个py文件,如命名为start.py,写入如下代码: from scrapy.cmdline import ex ...

  9. Ubuntu 源 (ros)

    deb http://archive.ubuntu.com/ubuntu/ trusty main restricted universe multiverse deb http://archive. ...

  10. HDU 4609 3-idiots ——(FFT)

    这是我接触的第一个关于FFT的题目,留个模板. 这题的题解见:http://www.cnblogs.com/kuangbin/archive/2013/07/24/3210565.html. FFT的 ...