简体   繁体   中英

How to save NSArray data in coredata?

I want to save my data in coredata from NSArray . How can I achieve this ?

NSArray *fetchdata = [Lockbox arrayForKey:@"fetch"];   

Here, I am getting data in below form:

在此处输入图片说明

How can I save this data in NSManageobject ?

Here is my try:

NSFetchRequest *fetchrequestforside=[NSFetchRequest fetchRequestWithEntityName:@"demo"];
NSManagedObjectContext *context = [self managedObjectContext];
NSManagedObject *newDevice1 = [NSEntityDescription
    insertNewObjectForEntityForName:@"demo"
             inManagedObjectContext:context];

NSArray *fetchdata = [Lockbox arrayForKey:@"fetch"]; // 

for (int i =0;i<[fetchdata count];i++){
    NSString *Name = [[fetchdata objectAtIndex:i] objectForKey:@"name"];
    NSString *Websitename = [[fetchdata objectAtIndex:i] objectForKey:@"websitename"];
    NSString *Feedlink = [[fetchdata objectAtIndex:i] objectForKey:@"feedurl"];

    NSLog(@"value:%@,%@,%@",Name,Websitename,Feedlink);
    [newDevice1 setValue:Name  forKey:@"name"];

    [newDevice1 setValue:Websitename  forKey:@"websitename"];
    [newDevice1 setValue:Feedlink forKey:@"feedurl"];
    NSLog(@"value:%@,%@,%@",Name,Websitename,Feedlink);

    // Save the context.
    NSError *error = nil;
    if (![context save:&error]) {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }
}

But with this technique I am getting an error. How can I resolve this problem?

EDIT:-

I am getting this error:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x7ff262520660'

Your code treats the contents of the fetchdata array as instances of NSDictionary , but, LockBox only appears to support string contents:

-(NSArray *)arrayForKey:(NSString *)key
{
    NSArray *array = nil;
    NSString *components = [self objectForKey:key];
    if (components)
        array = [NSArray arrayWithArray:[components componentsSeparatedByString:kDelimiter]];

    return array;
}

So, you need to deal with the conversion. You're probably part dealing with it already to store the original array...

But, that's why you get the error, the code expects a dictionary but actually gets a string

Do you have an object that looks like this?

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>

@interface demo : NSManagedObject

@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSString * websitename;
@property (nonatomic, retain) NSString * feedurl;

@end

After creating an object like this (using insertNewObjectForEntityForName) try to fetch it like that:

NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:self.managedObjectContext];
[request setEntity:entity];
NSArray *results = [self.managedObjectContext executeFetchRequest:request error:&error];

Btw, I would recommended you to stick to the naming conventions and call it Demo. Cheers

我建议您使用很棒的MagicalRecord库,它可以节省很多时间,而且非常简单,这里的教程MagicalRecord Tutorial

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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