天天看點

objective-c中使用cocoa的NSPredicate,謂詞(十四)

在語言上,謂語,謂詞是用來判斷的,比如“我是程式猿”中的是,就是表判斷的謂語,“是”就是一個謂詞,在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. @property (copy)NSString *name;  
  9. @property int age;  
  10. @end  
  11. @implementation Human  
  12. @synthesize name;  
  13. @synthesize age;  
  14. @end  
  15. int main(int argc, const char * argv[])  
  16. {  
  17.     @autoreleasepool {  
  18.         //利用kvc進行對象初始化  
  19.         Human *human = [[Human alloc]init];  
  20.         Human *child = [[Human alloc]init];  
  21.         [human setValue:@"holydancer" forKey:@"name"];  
  22.         [human setValue:[NSNumber numberWithInt:20] forKey:@"age"];  
  23.         [human setValue:child forKey:@"child"];  
  24.         [human setValue:[NSNumber numberWithInt:5] forKeyPath:@"child.age"];  
  25.         NSPredicate *predicate1=[NSPredicate predicateWithFormat:@"name=='holydancer'"];//建立謂詞判斷屬性  
  26.         NSPredicate *predicate2=[NSPredicate predicateWithFormat:@"child.age==5"];//建立謂詞判斷屬性的屬性  
  27.         //此處在建立謂詞時可以有好多種條件寫法,比如大小比較,範圍驗證,甚至像資料庫操作那樣的like運算符,這裡就不一一列舉了  
  28.         BOOL tmp1=[predicate1 evaluateWithObject:human];//驗證謂詞是否成立,得到布爾傳回值  
  29.         BOOL tmp2=[predicate2 evaluateWithObject:human];  
  30.         if (tmp1) {  
  31.             NSLog(@"human對象的name屬性為'holydancer'");  
  32.         }  
  33.         if (tmp2) {  
  34.             NSLog(@"human對象的child屬性的age為5");  
  35.         }  
  36.   }  
  37.     return 0;  
  38. }  

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屬性的age為5

關鍵字: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