gpt4 book ai didi

ios - iOS:对象数(0)与键数不同

转载 作者:行者123 更新时间:2023-12-01 16:36:12 25 4
gpt4 key购买 nike

我遇到了一个问题,我不知道出了什么问题。我有一种将信息保存到NSUserDefaults中的方法,然后还有一种用于检索它的方法。但是,当我检索到该错误消息时:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSDictionary initWithObjects:forKeys:]: count of objects (0) differs from count of keys (19)'

我的保存方法是:
- (BOOL) grabAndSaveDataFromServerForPageCount: (int) pageCount
{
NSData * serverResponse = [NSData dataWithContentsOfURL:[NSURL URLWithString:[self getFetchURLStringForPageType:FindLocate_Pet withInfo:[NSString stringWithFormat:@"%i",pageCount]]]];

if (![[NSJSONSerialization JSONObjectWithData:serverResponse options:kNilOptions error:nil] objectForKey:@"Count"])
{
//Server Error. Jump over updating process.
serverResponse = nil;
}


if (serverResponse)
{
//We'll re-organize the data to be display-friendly
NSMutableDictionary * petsData = [NSJSONSerialization JSONObjectWithData:serverResponse options:kNilOptions error:nil];

NSMutableDictionary * newDataToBeStored = [NSMutableDictionary dictionary];
NSMutableArray * newGroupsOfIndividualPost = [NSMutableArray array];

for (NSDictionary * individualPost in [petsData objectForKey:@"Content"])
{
NSMutableDictionary * newIndividualPost = [individualPost mutableCopy];

//"ID"
[newIndividualPost setObject:(NSString *)[individualPost objectForKey:@"ID"] forKey:@"ID"];

//"TypeID"
[newIndividualPost setObject:(NSString *)[individualPost objectForKey:@"TypeID"] forKey:@"TypeID"];

//"Name"
[newIndividualPost setObject:(NSString *)[individualPost objectForKey:@"Name"] forKey:@"Name"];

//"GenderID"
[newIndividualPost setObject:(NSString *)[individualPost objectForKey:@"GenderID"] forKey:@"GenderID"];

//"Description"
[newIndividualPost setObject:(NSString *)[individualPost objectForKey:@"Description"] forKey:@"Description"];

//"PetsterTag"
[newIndividualPost setObject:(NSString *)[individualPost objectForKey:@"PetsterTag"] forKey:@"PetsterTag"];

//"Color"
[newIndividualPost setObject:(NSString *)[individualPost objectForKey:@"Color"] forKey:@"Color"];

//"Breed"
[newIndividualPost setObject:(NSString *)[individualPost objectForKey:@"Breed"] forKey:@"Breed"];

//"BreedName"
[newIndividualPost setObject:(NSString *)[individualPost objectForKey:@"BreedName"] forKey:@"BreedName"];

//"DOB"
[newIndividualPost setObject:(NSString *)[individualPost objectForKey:@"DOB"] forKey:@"DOB"];

//"DateCreated"
[newIndividualPost setObject:(NSString *)[individualPost objectForKey:@"DateCreated"] forKey:@"DateCreated"];

//"Lost"
[newIndividualPost setObject:(NSString *)[individualPost objectForKey:@"Lost"] forKey:@"Lost"];

//"LostMessage"
[newIndividualPost setObject:(NSString *)[individualPost objectForKey:@"LostMessage"] forKey:@"LostMessage"];

//"DateLost"
[newIndividualPost setObject:(NSString *)[individualPost objectForKey:@"DateLost"] forKey:@"DateLost"];

//"ImageURLCover"
if ([(NSString *)[individualPost objectForKey:@"ImageURLCover"] length] > 0)
{
NSData * coverImageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:(NSString *)[individualPost objectForKey:@"ImageURLCover"]]];
UIImage * coverImageConverted = [UIImage imageWithData:coverImageData];
if (coverImageConverted)
{
[newIndividualPost setObject:coverImageConverted forKey:@"ImageURLCover"];
}
else
{
[newIndividualPost setObject:[UIImage imageNamed:@"no_image.png"] forKey:@"ImageURLCover"];
}
}


//"Die"
[newIndividualPost setObject:(NSString *)[individualPost objectForKey:@"Die"] forKey:@"Die"];

//"DateDie"
[newIndividualPost setObject:(NSString *)[individualPost objectForKey:@"DateDie"] forKey:@"DateDie"];

//"Enabled"
[newIndividualPost setObject:(NSString *)[individualPost objectForKey:@"Enabled"] forKey:@"Enabled"];



//"MemberID"
NSDictionary * dataToWrite = [individualPost objectForKey:@"MemberID"];

NSMutableDictionary * newIndividualMemberDetail = [NSMutableDictionary dictionary];


//"ID"
[newIndividualMemberDetail setObject:(NSString *)[dataToWrite objectForKey:@"ID"] forKey:@"ID"];

//"GenderID"
[newIndividualMemberDetail setObject:(NSString *)[dataToWrite objectForKey:@"GenderID"] forKey:@"GenderID"];

//"ProfileImageURL" - Incomplete URL. Would save simply as NSString
[newIndividualMemberDetail setObject:(NSString *)[dataToWrite objectForKey:@"ProfileImageURL"] forKey:@"ProfileImageURL"];

//"Name"
[newIndividualMemberDetail setObject:(NSString *)[dataToWrite objectForKey:@"Name"] forKey:@"Name"];

//"Company"
[newIndividualMemberDetail setObject:(NSString *)[dataToWrite objectForKey:@"Company"] forKey:@"Company"];

//"DOB"
[newIndividualMemberDetail setObject:(NSString *)[dataToWrite objectForKey:@"DOB"] forKey:@"DOB"];

//"NRIC"
[newIndividualMemberDetail setObject:(NSString *)[dataToWrite objectForKey:@"NRIC"] forKey:@"NRIC"];

//"Passport"
[newIndividualMemberDetail setObject:(NSString *)[dataToWrite objectForKey:@"Passport"] forKey:@"NPassportRIC"];

//"Nationality"
[newIndividualMemberDetail setObject:(NSString *)[dataToWrite objectForKey:@"Nationality"] forKey:@"Nationality"];

//"Username"
[newIndividualMemberDetail setObject:(NSString *)[dataToWrite objectForKey:@"Username"] forKey:@"Username"];

//"Password"
[newIndividualMemberDetail setObject:(NSString *)[dataToWrite objectForKey:@"Password"] forKey:@"Password"];

//"PhoneNo"
[newIndividualMemberDetail setObject:(NSString *)[dataToWrite objectForKey:@"PhoneNo"] forKey:@"PhoneNo"];

//"FaxNo"
[newIndividualMemberDetail setObject:(NSString *)[dataToWrite objectForKey:@"FaxNo"] forKey:@"FaxNo"];

//"MobileNo"
[newIndividualMemberDetail setObject:(NSString *)[dataToWrite objectForKey:@"MobileNo"] forKey:@"MobileNo"];

//"Email"
[newIndividualMemberDetail setObject:(NSString *)[dataToWrite objectForKey:@"Email"] forKey:@"Email"];

//"ShowFields" group of data
NSString * jsonShowFields = [dataToWrite objectForKey:@"ShowFields"];

NSString *jsonString = jsonShowFields;
NSData *dataShowFields = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary * showFields = [NSJSONSerialization JSONObjectWithData:dataShowFields options:0 error:nil];

//"isName"
if (![[showFields objectForKey:@"isName"] isKindOfClass:[NSNull class]])
{
[newIndividualMemberDetail setObject:(NSString *)[showFields objectForKey:@"isName"] forKey:@"isName"];
}
else
{
[newIndividualMemberDetail setObject:@"0" forKey:@"isName"];
}


//"isAge"
if (![[showFields objectForKey:@"isAge"] isKindOfClass:[NSNull class]])
{
[newIndividualMemberDetail setObject:(NSString *)[showFields objectForKey:@"isAge"] forKey:@"isAge"];
}
else
{
[newIndividualMemberDetail setObject:@"0" forKey:@"isAge"];
}


//"isMobileNo"
if (![[showFields objectForKey:@"isMobileNo"] isKindOfClass:[NSNull class]])
{
[newIndividualMemberDetail setObject:(NSString *)[showFields objectForKey:@"isMobileNo"] forKey:@"isMobileNo"];
}
else
{
[newIndividualMemberDetail setObject:@"0" forKey:@"isMobileNo"];
}

//"isContactNo"
if (![[showFields objectForKey:@"isContactNo"] isKindOfClass:[NSNull class]])
{
[newIndividualMemberDetail setObject:(NSString *)[showFields objectForKey:@"isContactNo"] forKey:@"isContactNo"];
}
else
{
[newIndividualMemberDetail setObject:@"0" forKey:@"isContactNo"];
}

//"isPhoneNo"
if (![[showFields objectForKey:@"isPhoneNo"] isKindOfClass:[NSNull class]])
{
[newIndividualMemberDetail setObject:(NSString *)[showFields objectForKey:@"isPhoneNo"] forKey:@"isPhoneNo"];
}
else
{
[newIndividualMemberDetail setObject:@"0" forKey:@"isPhoneNo"];
}



//Final copy
[newIndividualPost setObject:newIndividualMemberDetail forKey:@"MemberID"];
[newGroupsOfIndividualPost addObject:newIndividualPost];
}


[newDataToBeStored setObject:newGroupsOfIndividualPost forKey:@"Content"];
[newDataToBeStored setObject:[petsData objectForKey:@"Count"] forKey:@"Count"];

if (pageCount>1)
{
//Means this info grab should add into the existing backup and not to replace it.
NSUserDefaults * storage = [NSUserDefaults standardUserDefaults];
NSDictionary * existingData = (NSDictionary*) [NSKeyedUnarchiver unarchiveObjectWithData:[storage objectForKey:@"PetPageBackup"]];

NSMutableDictionary * newExistingData = [existingData mutableCopy];
[newExistingData addEntriesFromDictionary:newDataToBeStored];
[storage setObject:[NSKeyedArchiver archivedDataWithRootObject:newExistingData] forKey:@"PetPageBackup"];

[storage synchronize];

}
else
{

//Directly save to NSUserDefaults by packaging it to a NSData format
NSUserDefaults * storage = [NSUserDefaults standardUserDefaults];
[storage setObject:[NSKeyedArchiver archivedDataWithRootObject:newDataToBeStored] forKey:@"PetPageBackup"];

[storage synchronize];
}

return YES;

}
else
{

return NO;
}
}

我的检索方法是:
NSUserDefaults * storage = [NSUserDefaults standardUserDefaults];
NSDictionary * petsData = (NSDictionary*) [NSKeyedUnarchiver unarchiveObjectWithData:[storage objectForKey:@"PetPageBackup"]];

最后一行是发出警告的地方。知道为什么会这样吗?

救命!

编辑:

保存到NSUserDefaults之前,这是“newDataToBeStored”变量的状态:

最佳答案

试试这个,

newIndividualPost = [[NSMutableDictionary alloc]initWithDictionary: individualPost];

希望这能解决您的问题。

关于ios - iOS:对象数(0)与键数不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28039690/

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