Fouandation(NSString ,NSArray,NSDictionary,NSSet) 中常见的理解错误区
Fouandation 中常见的理解错误区
1.NSString
//快速创建(实例和类方法) 存放的地址是 常量区
NSString * string1 = [NSString alloc]initWithString:@“123”];
NSString * string3 = [NSString stringWithString:@“123”];
//格式化方法创建 存放地址是堆区
NSString * string2 = [NSString alloc]initWithFormat:@“%d”,123];
NSString * string4 = [NSString stringWithFormat:@“%d”,123];
所以当判断两个字符串内容是否相同时
if(string1 isEqualToString:string3) //string2与string4也一样
{
}//结果内容是相同的
但是
if(string? == string ? )
{
}//当是1 与 3 时 是是同一个对象,因为内存的指向同一个地址(常量区)
//当是 2 与 4 时 不是同一个对象 ,因为内容相同,但是指向的内存地址不一样(堆区)
注意点:NSMutableString 用快速创建的一样是 存放在堆区
2.比较 字符串的大小
NSComparisonResult result =【string1 caseInsensitiveCompare:string2】;//返回的是基本数据类型:-1 降序 0 相等 1 升序
3.字符串长度 : 【string1 length】;
4.字符串转换:
4.1 字符串首字母大写(有空格的单词首个字母都大写):[ string capitalizedString]
4.2字符串所有字母大写:[ string uppercaseString]
4.3.字符串所有字母小写:[ string lowercaseString]
5 字符串转换成基本数据类型
float i = 【string floatValue】;(boolValue , intValue….)
6.字符串转换成数组(有规律的字符串)
NSString * MD = [NSString stringWithString:@“abc 123 xyz”];
NSArray *array = [MD componentsSeparatedByString:@“ “];//空格作为分割
7.字符串的截取:
NSString *subString1 = [string subStringToIndex:2];//从开头到 第X位 不包括X位
NSString *subString2 = [string subStringFromIndex:2];//从第X位 到最后 包括第X位
NSRange range ;
range.location = 4;
range.length = 2;
第二种定义range
NSRange *range = {4,2};
NSString *subString2 = [string subStringWithRange : range];
8.字符串的拼加:
1.通过格式化字符串
NSString *string1 = [NSString stringWithString:@“123”];
NSString *string1 = [NSString stringWthString:@“abc”];
NSSting *appengString = [NSString alloc]]initWithFormat:@“%@%@“,string1,string2];
2.NSString *appString = [string1 stringByAppendingString:string2];
3.NSString *appString 3 = [string1 stringByAppendingFormat:@“%@“,string2];
9.查询字符串
NSString *string1 = [NSString stringWithString:@“123abc_maxxyz”];
NSRange range = [string1 rangeOfString:@“abc_max”];
if(range.location != NSNotFound)
{
} NSNotFound = NSIntergeMax; 查询range 这段范围 的起始点是否找得到
//注意点:要查看range的location and length 可以通过 NSStringFormrange(range);
9.NSMutableString继承 Nstring
常用的插入 ,替换,删除
NSMutableString *mutableString = [NSMutableString alloc]]initWithString:@“abc”];
9.1 插入
[mutableString insertString:@“…xyz” atIndex:3];
9.2 替换
[mutableString replaceCharactersInRange:NSMakeRange:(0,3) withString:@“efg”];
9.3删除
[mutableString deleteCharactersInRange:NSMakeRange:(0,3)];
///////////////////////////////////////////////
NSArray 的知识点
1.数组的创建(类方法和实例方法)
NSArray * array1 = [NSArray arrayWithObject:@“one”];//单个
NSArray * array2 = [NSArray arrayWithObjects:@“one”,@“two”,nil];//创建多个对象
NSArray * array3 = [NSArray arrayWithArray:array1]; //通过数组直接创建
2.数组中元数的个数
[array1 count];
3,增加数组中的元素
NSArray *array4 = [array2 arrayByAddingObject:@“end”];
4.将数组转换成字符串
NSString *string = [array4 componentsJoinsByString:@“," ];
5.根据对象返回索引下标,根据索引下标返回对象
NSString *string = [array2 objectAtIndex:2];//根据索引下标返回对象
NSInteger a = [array2 indexOfObject:@“one”];//根据对象返回索引下标
NSString string = [array2 lastObject];//返回数组最后一个元素
6.数组中是否包含某个元素
BOOL isContains = [array2 containsObject:@“one”];
7.NSMutableArray 是继承NSArray
NSMutableArray *mutableArray = [NSMutableArray array];
NSMutableArray *mutableArray2 = [NSMutaleArray arrayWithCapacity:5];//这个仅仅起到标志内存,可以超出和少于
NSMutabelArray *mutableArray3 = [NSMutableArray arrayWithObjects:@“aaa”,@“bbb”,nil];
//添加
[mutableArray3 addObject:@“ccc”];//在数组最后添加一个元素
//插入
[mutableArray3 insertObject:@“” atIndex: 0]; //在指定的位置插入一个元素
//删除
[mutableArray3 removeLastObject];//移除最后一个元素
[mutableArray3 removeObject:@“bbb”];//指定元素移除
[mutableArray3 removeObjectAtIndex:@“0”];//根据下标移除元素
[mutableArray3 removeObjectsInArray:array2];//根据数组移除元素
[mutableArray3 removeAllObjects];//移除所有元素
//替换
[mutableArray3 replaceObjectAtIndex:0 withObject:@“”replace];
//遍历数组
常规遍历
NSArray *array =[NSArray arrayWithObjects:@“One”,@“Two”,nil ];
for(int index = 0; index < [array count];index++)
{
NSString *string = [array objectAtIndex:index];
NSLog();
}
快速枚举
for(NSString *string in array)
{
NSLog();
}
for(id string in array)
{
NSLog();
}//id 包含了* ,当不知道类型的时候使用id
/////////////////////////////////////////////////////
NSDictionary
1.NSDictionary 的创建(实例和类方法)
NSDictionary *dic1 = [NSDictionary dictionaryWithObject:@“value" forKey:@“k1”];
NSDictionary *dic2 = [NSDictionary dictionaryWithObjectAndKeys:@“v1" ,@“k1”,@“v2" ,@“k2”,@“v3" ,@“k3”,nil];//创建多个
NSDictionary *dic3 = [NSDictionary dictionaryWithDictionary:dic1];//通过字典创建
//获取字典的Value
NSString *string =【dic2 objectForKey:@“key”];
//获取字典的所有Key
[dic2 allKeys];
//获取字典的所有Value
[dic2 allValues];
NSMutableDictionary 继承 NSDictionary
//增加整个字典
[dic2 addEntriesFromDictionary:dic4];
//增加一个键值对
[dic2 setValue:@“Object” forKey:@“key”];
//创建一个空的字典
NSMutableDictionary *dicc = [NSMutableDictionary dictionary];
[dicc setDictionary:dic2];//特别注意的是:如果dicc 如果有值则会被覆盖
//移除
[dic2 removeObjectForKey:@“k1”];//移除一个对应键值对
[dic2 removeObjectForKeys:arrayKey];//移除多个键值对
遍历字典
NSArray *key = [dic allKeys];
for(int index = 0; index < [key count];index ++)
{
id k = [key objectAtIndex:index];
id V = [dic objectForKey:k];
NSLog();
}
//快速遍历
for(id key in dic1)
{
NSString *string = [dic1 objectForKey:key];
NSLog();
}
//枚举遍历
NSEnumerator *enumerator = [dic1 keyEnumerator];
id key = [enumerator nextObject];
while(key)
{
id Value = [dic1 objectForKey:key];
NSLog();
id key = [enumerator nextObject];
}
/////////////////////////////////////
NSSet
创建(实例和类方法)初始化
NSSet *set1 = [NSSet alloc]]initWitObjects:@“1”,@“2”,nil];
NSSet * set2 = [NSSet setWithObjects:@“one”,@“two”,nil];
NSArray *array = [NSArray arrayWithObjects:@“x”,@“y”,nil];
NSSet *set3 = [NSSet setWithArray:array];//通过数组创建集合
NSSet *set4 = [NSSet setWithSet:set1];//通过集合创建集合
//计算集合有几个元素
int a = [set1 count];
//获取所有的集合元素
NSArray *objects = [set allObjects];
//获取集合中任意一个元素
id object = [set anyObjects];
//集合中 是否包含某个元素
BOOL isContains = [set1 containsObject:@“2”];
//集合与集合间是否存在相同的元素
BOOL isIntersect = [set1 intersectsSet:set2];
//集合与集合是否完全匹配
BOOL isEqual = [set1 isEqual set2];
//集合是否是另一个集合的子集
BOOL isSubset = [set1 isSubsetOfSET:set5];
//对已有集合增加一元素创建新的集合
NSSet *set = [set1 setByAddingObject:@“12”];
//对已有集合增加集合创建新的集合
NSSet *set = [set1 setByAddingObjectsFromSet:set2];
//对已有集合增加数组创建新的集合
NSSet *set = [set1 setByAddingObjectsFromArray:array];
/////////////////
NSMutableSet((类方法和实例)初始化
NSMutableSet *set1 = [NSMutableSet setWithObjects:@“1”,@“2”,nil];
NSMutableSet *set2 = [NSMutableSet setWithObjects:@“d”,@“2”,nil];
//“减去”相同的
[set1 minusSet:set2];
//集合中相同的(交集)
[set1 intersectSet:set2];//记得与intersectsSet 的差别
//并集
[set1 unionSet: set2];
//根据元素移除
[set1 removeObjects:@“1”];
//移除集合所有的元素
[set1 removeAllObjects];
//根据数组增加集合元素
[set1 addObjectsFromArray:array];
Fouandation(NSString ,NSArray,NSDictionary,NSSet) 中常见的理解错误区的更多相关文章
- [转]一些NSArray,NSDictionary,NSSet相关的算法知识
iOS编程当中的几个集合类:NSArray,NSDictionary,NSSet以及对应的Mutable版本,应该所有人都用过.只是简单使用的话,相信没人会用错,但要做到高效(时间复杂度)精确(业务准 ...
- NSData NSDate NSString NSArray NSDictionary 相互转换
// NSData NSDate NSString NSArray NSDictionary json NSString *string = @"hello word"; NSDa ...
- NSData NSDate NSString NSArray NSDictionary 相互转化
// NSData NSDate NSString NSArray NSDictionary json NSString *string = @"hello word"; ...
- python中常见的报错信息
python中常见的报错信息 在运行程序时常会遇到报错提示,报错的信息会提示是哪个方向错的,从而帮助你定位问题: 搜集了一些python最重要的内建异常类名: AttributeError:属性错误, ...
- Python中常见的报错名称
Python中常见的报错名称 1.SyntaxError 语法错误.看看是否用Python关键字命名变量,有没有使用中文符号,运算符.逻辑运算符等符号是不是使用不规范. 2.IndentationEr ...
- [翻译] 用 ObjectiveSugar 扩展NSArray NSDictionary NSSet NSNumber
source - https://github.com/supermarin/ObjectiveSugar Look like a girl, act like a lady, think like ...
- 一些NSArray,NSDictionary,NSSet相关的算法知识
iOS编程当中的几个集合类:NSArray,NSDictionary,NSSet以及对应的Mutable版本,应该所有人都用过.只是简单使用的话,相信没人会用错,但要做到高效(时间复杂度)精确(业务准 ...
- 遍历NSArray, NSDictionary, NSSet的方法总结
1,for循环读取 NSArray: NSArray *array = /*…*/ ; i<array.count; i++) { id object = array[i]; // do sth ...
- JS 调试中常见的报错的解决办法
报错:Uncaught SyntaxError: Unexpected token o in JSON at position 1 at JSON.parse (<anonymous>) ...
随机推荐
- can't open a connection to site 'syb_backup'
sp_configure "allow update",1 go update sysservers set srvname='SYB_BACKUP', srvnetname=' ...
- FireFox每次访问页面时检查最新版本
FireFox每次访问页面时检查最新版本 浏览器都有自己的缓存机制,作为开发人员,每次js的修改都要清空缓存,显然很不方便.而firefox并没有提供ie那样的设置. 下面的方法就可以非常方便的设置f ...
- [Nhibernate]SchemaExport工具的使用(二)——创建表及其约束、存储过程、视图
目录 写在前面 文档与系列文章 表及其约束 存储过程 视图 总结 写在前面 由于一直在山西出差,有几天没更新博客了.昨晚回到家,将博客园最近三天更新的文章搜集了一下,花费了半天的时间,看了看,有些文章 ...
- sqilite学习
1,用代码插入数据 for (int i = 0; i < 100; i++) { NSString *nameStr = [NSString stringWithFormat:@ ...
- ASP.NET 导出数据表格
功能:可以实现导出整个数据表格或整个页面 public bool ExportGv(string fileType, string fileName) { bool ...
- Maven 入门 (1)—— 安装
Maven 入门 (1)—— 安装 http://blog.csdn.net/kakashi8841/article/details/17371837 1.下载maven安装包 http://mave ...
- Python的垃圾回收机制
Python的GC模块主要运用了“引用计数”(reference counting)来跟踪和回收垃圾.在引用计数的基础上,还可以通过“标记-清除”(mark and sweep)解决容器对象可能产生的 ...
- Ext 修改Store初始化加载完后修改record属性。
/** * Created by huangbaidong on 2016/9/18. * 产品组件通用Store, */ Ext.define('app.component.ebs.itemdata ...
- request \response 总结
request&response request 1.获得信息的方法 1> 获得请求首行信息的方法 *getMethod *getContextP ...
- spring jpa 实体互相引用返回restful数据循环引用报错的问题
spring jpa 实体互相引用返回restful数据循环引用报错的问题 Java实体里两个对象有关联关系,互相引用,比如,在一对多的关联关系里 Problem对象,引用了标签列表ProblemLa ...