在Objective-C中有类似于LINQ的东西吗?

mam*_*mcx 20 database objective-c

我想知道是否有可能(以及如何)在Objective-C中提供类似于以下内容的类:

Person.Select(@"Name").OrderAsc(@"Name").Where(@"Id").EqualTo(1).And(@"Id").NotEqualTo(2).Load<Array>
Run Code Online (Sandbox Code Playgroud)

对于我正在做的项目来说,这可能非常方便.

我喜欢Django和SubSonic中的这种编码方式.

Col*_*inE 15

我为Objective C创建了自己的Linq风格的API,可以在github上找到.您的具体示例如下所示:

NSArray* results = [[[people where:^BOOL(id person) {
                                return [person id] == 1 && [person id] != 2;
                             }]
                             select:^id(id person) {
                                return [person name];
                             }]
                             sort]; 
Run Code Online (Sandbox Code Playgroud)


slf*_*slf 6

简短的回答是没有与Objectiveq C相同的Linq,但你可以在一个根据自己喜好的包装类中混合使用SQLite,NSPredicate和CoreData调用来伪造它.您可能对核心数据指南,谓词指南此示例代码感兴趣.

从上面的谓词指南:

NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Employee"
        inManagedObjectContext:managedObjectContext];
[request setEntity:entity];
// assume salaryLimit defined as an NSNumber variable
NSPredicate *predicate = [NSPredicate predicateWithFormat:
        @"salary > %@", salaryLimit];
[request setPredicate:predicate];
NSError *error = nil;
NSArray *array = [managedObjectContext executeFetchRequest:request error:&error];
Run Code Online (Sandbox Code Playgroud)


Ant*_*ert 5

有一个比较Windows和Cocoa这样做的方法.Cocoa使用Key Paths和NSPredicate ....

可可是我的女朋友