在Objective-C中是否有类似于LINQ的东西?[英] Is there something similar to LINQ in Objective-C?

本文是小编为大家收集整理的关于在Objective-C中是否有类似于LINQ的东西?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

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

的类别
Person.Select(@"Name").OrderAsc(@"Name").Where(@"Id").EqualTo(1).And(@"Id").NotEqualTo(2).Load<Array>

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

我喜欢Django&subsonic中存在的这种编码方式.

推荐答案

简短的答案是,对于Objective-C而言,Linq并不等同,但是您可以将其混合使用SQLITE,NSPREDICATE和COREDATA CALLE,在包装阶段中呼叫,形状为您的喜好.您可能会对核心数据指南 predicate noreferrer"> predicate norefore <>/a>,and 示例典范a>.

从上面的谓词指南:

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];

其他推荐答案

我为目标C创建了自己的Linq-Style API,它是 github上可用 .您的特定示例看起来像这样:

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

其他推荐答案

有一个比较窗户和可可的方法的acticle.可可使用关键路径和nspredicate ....

可可是我的女朋友

本文地址:https://www.itbaoku.cn/post/597537.html

问题描述

I wonder if it is possible (and how) to provide a class in Objective-C with something like:

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

That could be very handy for a project I'm doing.

I like this way of coding present in Django & SubSonic.

推荐答案

The short answer is that there is not an equivalent to Linq for Objective-C but you can fake it with a mix of SQLite, NSPredicate and CoreData calls in a wrapper class shaped to your liking. You'd probably be interested in the core data guide, the predicate guide, and this example code.

From the above predicate guide:

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];

其他推荐答案

I created my own Linq-style API for Objective C, which is available on github. Your specific example would look something like this:

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

其他推荐答案

There is an acticle comparing the Windows and Cocoa ways of doing this. Cocoa uses Key Paths And NSPredicate....

Cocoa is my Girlfriend