무엇이 잘못되었는지 전혀 알지 못하는 문제에 직면했습니다. 정보를 NSUserDefaults에 저장 한 다음이를 검색하는 다른 방법을 저장하는 방법이 있습니다. 나는 그것을 검색 할 때 오류 메시지가 와서 :iOS : 개체 개수 (0)가 키 수와 다릅니다
*** 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"]];
그리고 마지막 줄에
경고가 나온 곳입니다. 그 일이 왜 일어 났는지에 대한 어떤 생각?도움말!
편집:이 방법으로 문제가 해결
전체 스택 추적을 표시하십시오. unarchiver가 오류를 발생 시킨다는 말씀입니까? – Wain
예, 저장된 NSData의 보관을 시도한 위 코드의 마지막 줄은 오류가 발생한 곳입니다. – Marcus