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?