现在的位置: 首页 > 综合 > 正文

objective-c中使用cocoa的NSPredicate,谓词(十四)

2013年08月19日 ⁄ 综合 ⁄ 共 3661字 ⁄ 字号 评论关闭

在语言上,谓语,谓词是用来判断的,比如“我是程序猿”中的是,就是表判断的谓语,“是”就是一个谓词,在objective-c中,应该说在COCOA中的NSPredicate表示的就是一种判断。一种条件的构建。我们可以先通过NSPredicate中的predicateWithFormat方法来生成一个NSPredicate对象表示一个条件,然后在别的对象中通过evaluateWithObject方法来进行判断,返回一个布尔值。还是看代码简单明了:

[plain] view
plain
copy

  1. #import <Foundation/Foundation.h>  
  2. @interface Human :NSObject  
  3. {  
  4.     NSString *name;  
  5.     int age;  
  6.     Human *child;  
  7.       
  8. }  
  9. @property (copy)NSString *name;  
  10. @property int age;  
  11. @end  
  12. @implementation Human  
  13. @synthesize name;  
  14. @synthesize age;  
  15.   
  16.   
  17. @end  
  18. int main(int argc, const char * argv[])  
  19. {  
  20.   
  21.     @autoreleasepool {  
  22.         //利用kvc进行对象初始化  
  23.         Human *human = [[Human alloc]init];  
  24.         Human *child = [[Human alloc]init];  
  25.         [human setValue:@"holydancer" forKey:@"name"];  
  26.         [human setValue:[NSNumber numberWithInt:20] forKey:@"age"];  
  27.         [human setValue:child forKey:@"child"];  
  28.         [human setValue:[NSNumber numberWithInt:5] forKeyPath:@"child.age"];  
  29.           
  30.         NSPredicate *predicate1=[NSPredicate predicateWithFormat:@"name=='holydancer'"];//创建谓词判断属性  
  31.         NSPredicate *predicate2=[NSPredicate predicateWithFormat:@"child.age==5"];//创建谓词判断属性的属性  
  32.         //此处在创建谓词时可以有好多种条件写法,比如大小比较,范围验证,甚至像数据库操作那样的like运算符,这里就不一一列举了  
  33.           
  34.         BOOL tmp1=[predicate1 evaluateWithObject:human];//验证谓词是否成立,得到布尔返回值  
  35.         BOOL tmp2=[predicate2 evaluateWithObject:human];  
  36.         if (tmp1) {  
  37.             NSLog(@"human对象的name属性为'holydancer'");  
  38.         }  
  39.         if (tmp2) {  
  40.             NSLog(@"human对象的child属性的age为5");  
  41.   
  42.         }  
  43.           
  44.   }  
  45.     return 0;  
  46. }  


2012-03-21 19:59:42.668 predicate[2246:403] human对象的name属性为'holydancer'

2012-03-21 19:59:42.670 predicate[2246:403] human对象的child属性的age5


关键字:objective-c ,objective c, oc ,谓词 ,NSPredicate  ,IPhone开发基础 .


好了,写到这里大家就会发现谓词的用法其实很简单,语法结构有点儿类似之前我们介绍的KVC,灵活多变,我们暂且掌握到这里便足够了。另外,到今天为止,我们的objective-c基础就告一段落了,马上我要推出IPhone开发的教学博客,希望大家继续关注。


网上找的IOS开发面试题,暂无答案

1.Difference between shallow copy and
deep copy?


2.What is advantage of categories? What is difference between implementing a category and inheritance?

3.Difference between categories and extensions?

4.Difference between protocol in objective c and interfaces in java?

5.What are KVO and KVC?

6.What is purpose of delegates?

7.What are mutable and immutable types in Objective C?

8.When we call objective c is runtime language what does it mean?

9.what is difference between NSNotification and protocol?

10.What is push notification?

11.Polymorphism?

12.Singleton?

13.What is responder chain?

14.Difference between frame and bounds?

15.Difference between method and selector?

16.Is there any garbage collection mechanism in Objective C.?

17.NSOperation queue?

18.What is lazy loading?

19.Can we use two tableview controllers on one viewcontroller?

20.Can we use one tableview with two different datasources? How you will achieve this?

21.What is advantage of using RESTful webservices?

22.When to use NSMutableArray and when to use NSArray?

23.What is the difference between REST and SOAP?

24.Give us example of what are delegate methods and what are data source methods of uitableview.

25.How many autorelease you can create in your application? Is there any limit?

26.If we don’t create any autorelease pool in our application then is there any autorelease pool already provided to us?

27.When you will create an autorelease pool in your application?

28.When retain count increase?

29.Difference between copy and assign in objective c?

30.What are commonly used NSObject class methods?

31.What is convenience constructor?

32.How to design universal application in Xcode?

33.What is keyword atomic in Objective C?

34.What are UIView animations?

35.How can you store data in iPhone applications?

36.What is coredata?

37.What is NSManagedObject model?

38.What is NSManagedobjectContext?

39.What is predicate?

40.What kind of persistence store we can use with coredata?


:来自cocoaChian社区



转自holydancer的CSDN专栏,原文地址:http://blog.csdn.net/holydancer/article/details/7380799

抱歉!评论已关闭.