非常感谢更有经验的 Obj C 编码人员的帮助,因为我已经坚持了几天,我相信代码是“接近”的。
我正在从用户联系人创建一个名称列表,然后将此列表传递回标签。我的问题(我认为)来自我使用 NSKeyedArchiver 将列表存档到 NSString 的 NSData 格式。我已经阅读了我能找到的所有内容,但我怀疑我作为一个 NOOB 正在阻止我破译其他一些例子。
这是我的 *.m 文件的一部分,我在其中创建了一个人员列表到一个名为“_objects”的数组中,然后我归档了“_objects”(NSKeyArchiver)制作了一个 NSData(数据),然后我尝试创建一个 NSString( guestListString) 来自。似乎一切顺利,除了“guestListString”在我的人名列表中包含一堆额外的 gobblygook 字符(是的,一个高科技术语)。我尝试过其他格式,但它们要么返回(null),要么返回一堆外来字符。我怀疑我还没有学会一些需要插入的步骤,以摆脱逗号分隔名称列表周围的这些“gobbly gook”字符。
这是代码:
// now grab the 'person' property from the Addressbook, pass it as a string back to 'tableview cell', inserting it at 'row 0' and update display
- (void)displayPerson:(ABRecordRef)person {
if (!_objects) {
_objects = [[NSMutableArray alloc] init];
}
NSString *selectedPerson = (__bridge NSString *)ABRecordCopyCompositeName(person);
//NSString *firstname =(__bridge_transfer NSString*)ABRecordCopyValue(person,kABPersonFirstNameProperty);
//NSString *lastname =(__bridge_transfer NSString*)ABRecordCopyValue(person,kABPersonLastNameProperty);
#ifdef DEBUG
NSLog(@"displayPerson insertRowsAtIndexPath selectedPerson = "@"%@",selectedPerson); // shows last added name
#endif
[_objects insertObject:selectedPerson atIndex:0];
//[_objects insertObject:firstname atIndex:0];
//[_objects insertObject:lastname atIndex:0];
#ifdef DEBUG
NSLog(@"displayPerson insertRowsAtIndexPath _objects = "@"%@",_objects); // shows 'list' of all names currently in list
//NSLog(@"displayPerson insertRowsAtIndexPath stringWithFormat _objects = "@"%@",[NSString stringWithFormat:@"%@",_objects]);
#endif
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
//
// (Code to Archive an array) Given that "_objects" contains an array of 'selectedPerson' objects
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:_objects];
// Now save to NSUser defaults
[[NSUserDefaults standardUserDefaults] setObject:data forKey:@"_objects"];
// Now let's see what is in 'data'
NSLog(@"archive of 'data' from key _objects = "@"%@",data); // shows long list of 8 bit numbers
//
// (Code to unarchive an array) Now to unarchive:
// I'm doing this code example here just so I understand unarchive procedure; not needed in actual app code
NSData *_objectsData = [[NSUserDefaults standardUserDefaults] objectForKey:@"_objects"];
NSArray *backIntoArray = [NSKeyedUnarchiver unarchiveObjectWithData:_objectsData];
//
// Now lets see what is in "backIntoArray"
NSLog(@"unarchive of _objectsData = "@"%@",_objectsData); // shows long list of 8 bit numbers
NSLog(@"unarchive of NSArray to backIntoArray = "@"%@",backIntoArray); // shows 'list' of all names currently in list
//
// Now convert NSData (data) to NSString so I can pass it to other elecments like 'labels'
//NSString *guestListString = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; // this is equivalent code to next two lines
NSString *guestListString;
guestListString = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; // returns extra characters around list of names instead of list of names ???
// other potential format options that could exist
//guestListString = [[NSString alloc] initWithData:data encoding:NSNEXTSTEPStringEncoding]; // shows
//guestListString = [[NSString alloc] initWithData:data encoding:NSJapaneseEUCStringEncoding]; // shows
//guestListString = [[NSString alloc] initWithData:data encoding:NSNEXTSTEPStringEncoding]; // shows
//guestListString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; // returns (null) ???
//guestListString = [[NSString alloc] initWithData:data encoding:NSISOLatin1StringEncoding]; // shows
//guestListString = [[NSString alloc] initWithData:data encoding:NSSymbolStringEncoding]; // returns (null) ???
//guestListString = [[NSString alloc] initWithData:data encoding:NSNonLossyASCIIStringEncoding]; // returns (null) ???
//guestListString = [[NSString alloc] initWithData:data encoding:NSShiftJISStringEncoding]; // shows
//guestListString = [[NSString alloc] initWithData:data encoding:NSISOLatin2StringEncoding]; // shows
//guestListString = [[NSString alloc] initWithData:data encoding:NSUnicodeStringEncoding]; // returns string of japanese looking characters???
//guestListString = [[NSString alloc] initWithData:data encoding:NSWindowsCP1251StringEncoding]; // shows
//guestListString = [[NSString alloc] initWithData:data encoding:NSWindowsCP1252StringEncoding]; // shows
//guestListString = [[NSString alloc] initWithData:data encoding:NSWindowsCP1253StringEncoding]; // shows
//guestListString = [[NSString alloc] initWithData:data encoding:NSWindowsCP1254StringEncoding]; // shows
//guestListString = [[NSString alloc] initWithData:data encoding:NSWindowsCP1250StringEncoding]; // returns (null) ???
//guestListString = [[NSString alloc] initWithData:data encoding:NSISO2022JPStringEncoding]; // shows
//guestListString = [[NSString alloc] initWithData:data encoding:NSMacOSRomanStringEncoding]; // shows
//guestListString = [[NSString alloc] initWithData:data encoding:NSUTF16StringEncoding]; // returns string of japanese looking characters???
//guestListString = [[NSString alloc] initWithData:data encoding:NSUTF16BigEndianStringEncoding]; // shows
//guestListString = [[NSString alloc] initWithData:data encoding:NSUTF16LittleEndianStringEncoding]; // returns string of japanese looking characters???
//guestListString = [[NSString alloc] initWithData:data encoding:NSUTF32StringEncoding]; // returns (null) ???
//guestListString = [[NSString alloc] initWithData:data encoding:NSUTF32BigEndianStringEncoding]; // returns (null) ???
//guestListString = [[NSString alloc] initWithData:data encoding:NSUTF32LittleEndianStringEncoding]; // returns (null) ???
//guestListString = [[NSString alloc] initWithData:data encoding:NSProprietaryStringEncoding]; // shows
//
// show me what is stored in NSString 'guestListString'
NSLog(@"displayPerson NSString conversion of 'data' to 'guestListString' = "@"%@",guestListString); //
//
// Pass the required text back to 'guestsListLabel.text' on ViewController screen
//((InitialViewController *)self.presentingViewController).guestListLabel.text=selectedPerson; // This WORKS passing back the last selectedPerson 1 name
((InitialViewController *)self.presentingViewController).guestListLabel.text=guestListString; // Not working yet; trying to pass back full list of names
//
// add code here to handle saving of guestlist before leavig this 'insertRowsAtIndexPath' section
// Archiving is simple, using the following code:
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
// This next line archives objects correctly so they can be reloaded later and be editable (mutable)
[[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:_objects] forKey:@"guestListTable"];
// This next line writes the value loaded into 'guestListLabel.text' into variable 'kGuestListText' which is used in InitialViewController to display in all 3 field types
[userDefaults setValue:((InitialViewController *)self.presentingViewController).guestListLabel.text forKey:kGuestsListText];
// update with all userDefaults variables
[userDefaults synchronize];
//
}