経験豊富な Obj C コーダーの助けを借りて、私は数日間この問題に取り組んできました。コードは「近い」と思います。
ユーザーの連絡先から名前のリストを作成し、このリストをラベルに渡します。私の問題 (私が思うに) は、NSKeyedArchiver を使用してリストを NSString にアーカイブするために使用している NSData 形式から取得することです。見つけたものはすべて読みましたが、私が NOOB であるために、他の例を解読するのが妨げられているのではないかと思います。
これは *.m ファイルの一部で、人のリストを「_objects」という配列に作成し、「_objects」(NSKeyArchiver) をアーカイブして NSData (データ) を作成し、それから NSString ( guestListString) から。「guestListString」には、個人名のリスト全体に余分な gobblygook 文字 (ハイテク用語) が含まれていることを除いて、すべてうまくいっているようです。私は他の形式を試しましたが、それらは (null) を返すか、外国のような文字を返します。コンマで区切られた名前のリストの周りにあるこれらの「ゴブリングック」文字を取り除くために挿入する必要があるいくつかの手順を学んでいないと思われます。
コードは次のとおりです。
// 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];
//
}