获取核心数据并将其保存到nsarray

时间:2014-01-28 08:52:47

标签: ios objective-c

我认为这不是应该但我面临着这些问题。 我正在研究核心日期,在获取它之后我正试图将其保存到nsarray中。 但每次数据库中的最后数据都保存到数组中。我希望每个数据都插入到数据库中。 假设我有一个属性名称account_name 我添加了A,B,C,X,Y,Z。在这段代码中,我只是在我的数组中得到最后一个值Z. :( 但是当我打印它然后我得到所有数据.. 任何解决方案?

NSArray *fetchedObjects; 
AppDelegate *delegate = [UIApplication sharedApplication].delegate;
NSManagedObjectContext *context = [delegate managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Chart_of_Accounting" inManagedObjectContext:context];
[fetchRequest setEntity:entity];

NSError *error;
fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];

for (NSManagedObject *obj in fetchedObjects) {
    debitArray = [NSMutableArray arrayWithObject:[obj valueForKey:@"account_name"]];
    creditArray = [NSMutableArray arrayWithObject:[obj valueForKey:@"account_name"]];
    NSLog(@"Account Name: %@", [obj valueForKey:@"account_name"]);
}

4 个答案:

答案 0 :(得分:2)

试试这个

NSArray *fetchedObjects; 
AppDelegate *delegate = [UIApplication sharedApplication].delegate;
NSManagedObjectContext *context = [delegate managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Chart_of_Accounting" inManagedObjectContext:context];
[fetchRequest setEntity:entity];

NSError *error;
fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
NSMutableArray *debitArray = [[NSMutableArray alloc] init];
NSMutableArray *creditArray = [[NSMutableArray alloc] init];
for (NSManagedObject *obj in fetchedObjects) {
    [debitArray addObject:[obj valueForKey:@"account_name"]];
    [creditArray addObject:[obj valueForKey:@"account_name"]];
    NSLog(@"Account Name: %@", [obj valueForKey:@"account_name"]);
}

答案 1 :(得分:0)

因为,每次初始化。使用addObject

debitArray = creditArray = [[NSMutableArray alloc] init];
for (NSManagedObject *obj in fetchedObjects) {
    [debitArray addObject:[obj valueForKey:@"account_name"]];
    [creditArray addObject:[obj valueForKey:@"account_name"]];

}NSLog(@"Account Name: %@", [obj valueForKey:@"account_name"]);

答案 2 :(得分:0)

在你的循环中:

for (NSManagedObject *obj in fetchedObjects) {
   debitArray = [NSMutableArray arrayWithObject:[obj valueForKey:@"account_name"]];
   creditArray = [NSMutableArray arrayWithObject:[obj valueForKey:@"account_name"]];
   NSLog(@"Account Name: %@", [obj valueForKey:@"account_name"]);
}

每次都要实例化一个新数组(并覆盖前一个数组)。这解释了发生了什么。

你应该在循环之外实例化数组,然后在循环中将对象添加到它。

debitArray = [NSMutableArray arrayWithCapacity:[fetchedObjects count]];
creditArray = [NSMutableArray arrayWithCapacity:[fetchedObjects count]];

for (NSManagedObject *obj in fetchedObjects) {

   [debitArray addObject:[obj valueForKey:@"account_name"]];
   [creditArray addObject:[obj valueForKey:@"account_name"]];
   NSLog(@"Account Name: %@", [obj valueForKey:@"account_name"]);

}

答案 3 :(得分:0)

不要在不同的数组中创建重复值。

将您的fetchedObjects数据作为NSArray Type返回,然后从coredata返回。

fetchedObjects=[self yourRetriveMethod];

    for (NSManagedObject *obj in fetchedObjects) {
        debitArray = [NSMutableArray arrayWithObject:[obj valueForKey:@"account_name"]];
        creditArray = [NSMutableArray arrayWithObject:[obj valueForKey:@"account_name"]];
        NSLog(@"Account Name: %@", [obj valueForKey:@"account_name"]);
    }