objective-c中使用cocoa的NSPredicate,谓词(十四)
2012-12-20 09:25
316 查看
在语言上,谓语,谓词是用来判断的,比如“我是程序猿”中的是,就是表判断的谓语,“是”就是一个谓词,在objective-c中,应该说在COCOA中的NSPredicate表示的就是一种判断。一种条件的构建。我们可以先通过NSPredicate中的predicateWithFormat方法来生成一个NSPredicate对象表示一个条件,然后在别的对象中通过evaluateWithObject方法来进行判断,返回一个布尔值。还是看代码简单明了:
[plain] view
plaincopy
#import <Foundation/Foundation.h>
@interface Human :NSObject
{
NSString *name;
int age;
Human *child;
}
@property (copy)NSString *name;
@property int age;
@end
@implementation Human
@synthesize name;
@synthesize age;
@end
int main(int argc, const char * argv[])
{
@autoreleasepool {
//利用kvc进行对象初始化
Human *human = [[Human alloc]init];
Human *child = [[Human alloc]init];
[human setValue:@"holydancer" forKey:@"name"];
[human setValue:[NSNumber numberWithInt:20] forKey:@"age"];
[human setValue:child forKey:@"child"];
[human setValue:[NSNumber numberWithInt:5] forKeyPath:@"child.age"];
NSPredicate *predicate1=[NSPredicate predicateWithFormat:@"name=='holydancer'"];//创建谓词判断属性
NSPredicate *predicate2=[NSPredicate predicateWithFormat:@"child.age==5"];//创建谓词判断属性的属性
//此处在创建谓词时可以有好多种条件写法,比如大小比较,范围验证,甚至像数据库操作那样的like运算符,这里就不一一列举了
BOOL tmp1=[predicate1 evaluateWithObject:human];//验证谓词是否成立,得到布尔返回值
BOOL tmp2=[predicate2 evaluateWithObject:human];
if (tmp1) {
NSLog(@"human对象的name属性为'holydancer'");
}
if (tmp2) {
NSLog(@"human对象的child属性的age为5");
}
}
return 0;
}
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
[plain] view
plaincopy
#import <Foundation/Foundation.h>
@interface Human :NSObject
{
NSString *name;
int age;
Human *child;
}
@property (copy)NSString *name;
@property int age;
@end
@implementation Human
@synthesize name;
@synthesize age;
@end
int main(int argc, const char * argv[])
{
@autoreleasepool {
//利用kvc进行对象初始化
Human *human = [[Human alloc]init];
Human *child = [[Human alloc]init];
[human setValue:@"holydancer" forKey:@"name"];
[human setValue:[NSNumber numberWithInt:20] forKey:@"age"];
[human setValue:child forKey:@"child"];
[human setValue:[NSNumber numberWithInt:5] forKeyPath:@"child.age"];
NSPredicate *predicate1=[NSPredicate predicateWithFormat:@"name=='holydancer'"];//创建谓词判断属性
NSPredicate *predicate2=[NSPredicate predicateWithFormat:@"child.age==5"];//创建谓词判断属性的属性
//此处在创建谓词时可以有好多种条件写法,比如大小比较,范围验证,甚至像数据库操作那样的like运算符,这里就不一一列举了
BOOL tmp1=[predicate1 evaluateWithObject:human];//验证谓词是否成立,得到布尔返回值
BOOL tmp2=[predicate2 evaluateWithObject:human];
if (tmp1) {
NSLog(@"human对象的name属性为'holydancer'");
}
if (tmp2) {
NSLog(@"human对象的child属性的age为5");
}
}
return 0;
}
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
相关文章推荐
- objective-c中使用cocoa的NSPredicate,谓词(十四)
- objective-c中使用cocoa的NSPredicate,谓词(十四)
- objective-c中使用cocoa的NSPredicate,谓词(十四)
- objective-c中使用cocoa的NSPredicate,谓词
- Cocoa入门 - 使用Objective-c读书笔记1
- Iphone开发基础篇(十四)-ObjectC之谓词NSPredicate
- 谓词(NSPredicate)的使用
- Objective-C之谓词NSPredicate
- oc中谓词的使用(NSPredicate)
- 谓词的使用(NSPredicate)
- Windows上使用Objective-c和Cocoa
- 实用知识:NSPredicate(谓词)的使用
- [iOS学习] 谓词NSPredicate使用
- Cocoa入门-使用OBjective-c的读书笔记2
- iOS 中谓词的使用 NSPredicate
- iOS中的谓词(NSPredicate)使用
- iOS谓词的使用 (NSPredicate)
- iOS中的谓词(NSPredicate)使用
- 使用谓词(NSPredicate)来提高集合遍历与过滤查找的效率
- objective-C之 NSPredicate(谓词)