gpt4 book ai didi

ios - 如何在 Objective c 的 Core Data 中添加数组作为字符串

转载 作者:行者123 更新时间:2023-11-29 00:29:58 24 4
gpt4 key购买 nike

我是 iOS 的新手,我面临着将 NSArray 作为 NSString 添加到 Core Data 中的问题。

我正在使用这样的代码:

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]);
}
}

我更新的代码:

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]);
}
}
}

但它将整个数组保存在一个字符串中。我需要将数组元素作为字符串一个一个地存储在核心数据中。

这怎么可能?

最佳答案

可以通过简单的步骤完成。

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 ..");
}

保存多个数组 array1 & array2 到一个实体的子实体。

关于ios - 如何在 Objective c 的 Core Data 中添加数组作为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42152496/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com