NSDictionary,NSArray,NSPredicate,JSON

1.NSDictionary 是一种hash表, 是乱序的, 如果想要顺序就用NSArray

   当需要保持原来的顺序时,可考虑用两个NSArray代替NSDictionary。

   NSDictionary,NSArray不可以将自定义的对象writeToFile。


2. for(NSString*  key in dic)

   对字典直接循环,得到的是它的键

3.不可以存在nil的object,若要插入空值 ,使用[NSNull null]

    NSDictionary* dic=[NSDictionary dictionaryWithObject:[NSNull null] forKey:@"aa"];

    NSArray,NSDictionary中的值都不可以是nil。

   [NSDictionary dictionaryWithObjectsAndKeys]参数中的nil值可能截断扔掉了后面的参数。


4.NSArray's containsObject: methods calls isEqual: on its objects, hence even a dynamically created string such as[NSString stringWithFormat:@"%d", 2]would returnYES in your sample snippet.
This is because NSString's isEqual: (or more precisely its isEqualToString:) method is implemented to becontent aware (vs. comparing pointer identities) and thus returnYES for any pair of strings containing the very same sequence of characters (at time of comparison), no matter how and when they were created.

   containsObject做内容对比,isEqual,isEqualToString都是内容对比。地址对比使用==。

5.深度解析iPhone内省机制  http://www.61ic.com/Mobile/iPhone/201108/36492.html

内省(Introspection)是对象揭示自己作为一个运行时对象的详细信息的一种能力。这些详细信息包括对象在继承树上的位置,对象是否遵循特定的协议,以及是否可以响应特定的消息。

NSObject协议和类定义了很多内省方法,用于查询运行时信息,以便根据对象的特征进行识别。明智地使用内省可以使面向对象的程序更加高效和强壮。它有助于避免错误地进行消息派发、错误地假设对象相等、以及类似的问题。


7.从NSDictionary中copy出来的某个值,要注意它的类型是否和接收的类型一致,否则若在NSArray的containsObject函数中不会返回true。

8.JSON (JavaScript Object Notation)

   不要使用SBJSON(json-framework)

     http://blog.devtang.com/blog/2012/05/05/do-not-use-sbjson/


JSONKit  https://github.com/johnezang/JSONKit,JSONKit不支持arc

#if __has_feature(objc_arc)
#error JSONKit does not support Objective-C Automatic Reference Counting (ARC)
#endif


20.

love is a circle.coming back to you .when your love is given,with a heart that's true.


21.谓词类 NSPredicate

   使用谓词虽然便捷,但是它的运行速度不会比自己缩写全部代码快。

   (1)对单个对象操作

    NSPredicate* predicate = [NSPredicate predicateWithFormat:@"name==%@",@“jim”];//注意这里%@没有被单引号引用

    BOOL match=[predicate evaluateWithObject:obj];//obj使用valueForKeyPath取得name属性的值,与jim对比。

    predicate = [NSPredicate predicateWithFormat:@"innerObj.name=='%@'", “jim”];

    match=[predicate evaluateWithObject:obj];//obj使用成员innerObj的name的值与jim对比。

  (2)对数组操作

    返回新的数组

    NSArray* filteredArray=[objArray filteredArrayUsingPredicate:predicate];//filteredArrayUsingPredicate是NSArray的一种类别方法,它循环过滤数组内容,将匹配的对象放到新数组中并返回。

    也可以在可变数组中剔除不符合条件的项:

   NSMutableArray* arrCopy=[array mutableCopy];

   [arrCopy filterUsingPredicate:predicate];//剔除了不符合条件的项

  补充: KVC 键值编码  将valueForKey:发送给数组时,键将作用于数组中的所有元素,得到的是值的数组。

  (3)格式说明符

   可以使用格式说明符%k指定键名,格式说明符%@指定值,例如:NSPredicate* predicate = [NSPredicate predicateWithFormat:@"%k==%@",@"name",@“jim”];

  (4)使用变量名表示值

   NSPredicate* predicate = [NSPredicate predicateWithFormat:@"name==$NAME_VAR"];

   NSDictionary* varDic=[NSDictionary dictionaryWithObjectsAndKeys:@“jim”,@“NAME_VAR”,nil];

   predicate=[predicate predicateWithSubStitutionVariables:varDic];

   注意:键路径上不能应用变量名;变量的值可以是NSString,NSNumber,NSNull或数组。

   (5)运算符

   支持c语言中一些常用的运算符,

   运算符不区分大小写,如and,or,not

   不等号既适用于数字值又适用于字符串值。

   使用between:

   NSPredicate* predicate = [NSPredicate predicateWithFormat:@"age between {20,30}"];   //大括号表示数组

   可以使用%@格式说明符插入数组对象

   NSArray* betweenArray=[NSArray arrayWithObjects:[NSNumber numberWithInt:20],[NSNumber numberWithInt:30], nil];

   NSPredicate* predicate = [NSPredicate predicateWithFormat:@"age between %@",betweenArray];  

   使用IN:

   NSPredicate* predicate = [NSPredicate predicateWithFormat:@"name in {'tom','jim'’}"];   //大括号表示数组,也可以插入数组表示IN的条件值。

   (6)SELF

   SELF引用用于谓词计算的对象,特别对于一些简单值对象有用,如NSString。

   取两个数组的交集:

   NSArray* nameArray1=[NSArray arrayWithObjects:@"a",@"b",@"c" nil];

   NSArray* nameArray2=[NSArray arrayWithObjects:@"d",@"b",@"c" nil];

   NSPredicate* predicate = [NSPredicate predicateWithFormat:@"SELF in %@",nameArray1];  

   NSArray* retArray=[nameArray2 filteredArrayUsingPredicate:predicate];

   (7)专门针对字符串的关系运算符

    beginWith

    endWith

    contains

    它们是区分大小写和重音符的

    为了减少匹配规则名称,可以在这些运算符后面添加修饰符:[c]表示不区分大小写,[d]表示不区分发音符号 ,[cd]表示两者都不区分。
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains[cd] %@", searchBar.text];
    filteredArray = [array filteredArrayUsingPredicate:predicate];

  (8)like运算符

   like同样接受[cd]修改符

   “name like '*er*'”,等效于 "name contains 'er'"

   “name like '??er*'”,

  (9)matches运算符

   正则表达式的功能强大,但是它们的计算开销非常大,在执行matches前先进行一些简单的运算排除,这有助于提高运算速度。


 

 


 




resign  辞职,放弃.

relinquish 放弃,撤回,停止.

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值