【问题标题】:iPhone Core Data objects added to NSMutableArrayiPhone Core Data 对象添加到 NSMutableArray
【发布时间】:2010-02-13 23:45:18
【问题描述】:

我正在尝试将一堆核心数据对象 (NSStrings) 添加到 NSMutableArray 中,以便我的 pickerView 可以将字符串用作其数据值。

目前,我可以在 Core Data 中将 textField 中的字符串一次保存到 sqlite 文件中。

我无法将这些对象从 Core Data sqlite 文件中拉回 NSMutable 数组中。

这是将对象添加到 sqlite 文件的代码...

-(IBAction)addToPicker
{
 CoreDataPickerAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
 NSManagedObjectContext *context = [appDelegate managedObjectContext]; 
 NSManagedObject *thePerson = [NSEntityDescription insertNewObjectForEntityForName:@"Event" inManagedObjectContext:context];

 [thePerson setValue:textField.text forKey:@"eventName"];

 [context save:&error];

 textField.text=@"";
 self.viewDidLoad;

 [textField resignFirstResponder];
}

现在我有...

- (void)viewDidLoad {

 NSMutableArray *array = [[NSMutableArray alloc] init];

        //code needed to send all the objects in the sqlite Core Data store into array to be read by the pickerView

        self.pickerData = array;   // the picker uses NSMutableArray pickerData for its data

 [array release];

 [super viewDidLoad];

}

任何帮助将不胜感激。 克里斯

【问题讨论】:

    标签: iphone-sdk-3.0 core-data nsmutablearray uipickerview


    【解决方案1】:

    您可以使用 NSFetchRequest 将数据提取到数组中。看看Core Data Programming guide - Fetching Managed Object。您只需阅读链接的第一部分。

    执行 fetch 请求会返回一个数组

    NSArray *array = [moc executeFetchRequest:request error:&error];
    

    【讨论】:

    • 感谢您的回复。我已经能够将对象加载到数组中,但我没有问正确的问题。我正在寻找将对象属性“eventName”加载到数组中。所以我会有一个字符串数组,而不是一个 NSManagedObjects 数组。
    • 一旦你有了 NSManagedObjects 数组,你就可以对每个项目做一个循环,只把 eventName 放到另一个数组中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-22
    • 1970-01-01
    相关资源
    最近更新 更多