简体   繁体   English

iPhone SDK:核心数据

[英]iPhone SDK: Core Data

Probably a silly question but I cannot find a way to do it. 可能是一个愚蠢的问题,但我找不到解决方法。

I am developing an iPhone application that uses Core Data for it's storage. 我正在开发一个使用Core Data进行存储的iPhone应用程序。 At one point I want to loop around all the objects in the store and perform and action on them. 一方面,我想循环存储中的所有对象,并对它们执行操作。 Is there an easy way to do this? 是否有捷径可寻? I've tried all manner of for and while loops but can't seem to get anything working. 我已经尝试过for和while循环的所有方式,但似乎无法使任何工作正常。

If you perform a fetch request on your managed object context it returns an array that you can then loop through. 如果在托管对象上下文上执行获取请求,它将返回一个数组,然后可以循环遍历该数组。

NSFetchRequest * request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"SomeEntity"
                               inManagedObjectContext:context]];
NSError * error = nil;
NSArray * objects = [context executeFetchRequest:request error:&error];

if (error) {
    // an error occured
}

for (SomeEntity * object in objects) {
    // perform action
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM