0

I am new in iOS and I am facing problem regarding to add NSArray in Core Data as an NSString.

I am using code like this:

NSManagedObjectContext *context = [self managedObjectContext];
//Converting array as an string...
AuditIDCoreData=[NSString stringWithFormat:@"%@",idAuditarray];
AuditnameCoreData=[NSString stringWithFormat:@"%@",nameAuditarray];

NSError *error;

NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"Get_Auditnames_User" inManagedObjectContext:context]];

//  NSError *error = nil;
NSArray *results = [context executeFetchRequest:request error:&error];
NSLog(@"Result =%@",results);

NSString *Stringaudit =[NSString stringWithFormat:@"%@",idAuditarray];

ComplareArray=[devices valueForKey:@"auditname"];
ComplareArray2=[devices valueForKey:@"auditid"];

BOOL contains = [ComplareArray2 containsObject:Stringaudit];

if(contains == YES)
{
}
else
{
    if (self.device) {
        // Update existing device
        [self.device setValue:AuditIDCoreData forKey:@"auditid"];
        [self.device setValue:AuditnameCoreData forKey:@"auditname"];
    } else {
        // Create a new device
        NSManagedObject *newDevice = [NSEntityDescription insertNewObjectForEntityForName:@"Get_Auditnames_User" inManagedObjectContext:context];
        //  NSLog(@"context",newDevice);
        [newDevice setValue:AuditIDCoreData forKey:@"auditid"];
        [newDevice setValue:AuditnameCoreData forKey:@"auditname"];
    }

    //NSError *error = nil;
    // Save the object to persistent store
    if (![context save:&error]) {
        NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
    }
}

Code I have Updated:

NSString *FailString =@"";
NSString *WarningString =@"";

NSManagedObjectContext *context = [self managedObjectContext];

for (int i = 0; i < idarray.count; i++){
    if (self.device) {
        // Update existing device
        [device setValue:Audit forKey:@"auditnameId"];
        [device setValue:Passarray[i] forKey:@"checklistid"];
        [device setValue:CheckpointNameIDArray[i] forKey:@"checkpointid"];
        [device setValue:FailString forKey:@"failreason"];
        [device setValue:WarningString forKey:@"warningreason"];
        [device setValue:AuditStartDate forKey:@"starttimedate"];
        [device setValue:userid forKey:@"userid"];
        [device setValue:imageArray[i] forKey:@"auditimage"];

        NSError *error = nil;
        // Save the object to persistent store
        if (![context save:&error]) {
            NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
        } 
    } else {
        // Create a new device
        NSManagedObject *newDevice = [NSEntityDescription insertNewObjectForEntityForName:@"AuditPost" inManagedObjectContext:context];
        [newDevice setValue:Audit forKey:@"auditnameId"];
        [newDevice setValue:Passarray[i] forKey:@"checklistid"];
        [newDevice setValue:CheckpointNameIDArray[i] forKey:@"checkpointid"];
        [newDevice setValue:FailString forKey:@"failreason"];
        [newDevice setValue:WarningString forKey:@"warningreason"];
        [newDevice setValue:AuditStartDate forKey:@"starttimedate"];
        [newDevice setValue:userid forKey:@"userid"];
        [newDevice setValue:imageArray[i] forKey:@"auditimage"];

        NSError *error = nil;
        // Save the object to persistent store
        if (![context save:&error]) {
            NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
        }           
    }
}

But it saves whole array in just a single string. I need to store array element one by one in Core Data as String.

How is it possible?

0

3 Answers 3

1

Can do in just simple steps.

AppDelegate *appDelegate =
[[UIApplication sharedApplication] delegate];

NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSManagedObject *newData;
for (int i = 0; i < array1.count; i++){
    newData = [NSEntityDescription insertNewObjectForEntityForName:@"yourEntityName" inManagedObjectContext:context];
    [newData setValue:array1[i] forKey:@"sub-entities"];    
    [newData setValue:array2[i] forKey:@"sub-entities"];        
}

NSError *error = nil;
// Save the object to persistent store
if (![context save:&error]) {
    NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
}else {
    NSLog(@"Data saved successfully ..");
}

Saving multiple array array1 & array2 to the sub-entities of an entity.

Sign up to request clarification or add additional context in comments.

4 Comments

crash showing error 'Unacceptable type of value for attribute: property = "checklistid"; desired type = NSString; given type = __NSCFNumber; value = 97.
Please see the updated Question I have try your code but it get crash.
you are passing number whereas you database needs string there. Just convert you array to string using code [newData setValue:[NSString stringWithFormat:@"%@", array[i]] forKey:@"sub-entities"];
welcome, keep in remind that explicit conversion is not a prefered way just use original types.
0

You can convert it as follow

NSArray *arrayToBeConvertedInToString = @[@"This", @"is", @"an", @"array"];
NSString * result = [[arrayToBeConvertedInToString valueForKey:@"description"] componentsJoinedByString:@" "];
NSLog(@"Array in string: %@", result);

1 Comment

I am already doing this it sore whole array in just single string. I need to add array element one by one in core data.
0

You can use method explained by Richard G

I ll suggest you to convert it into NSData and store it as transferable in coreData

Converting into NSData

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:arrayName];

Storing into CoreData

[newManagedObject setValue:nsData forKey:@"auditid"];

Be sure you have changed datatype to transformable in coredata

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.