将nsarray转换为nsdata到nsstring以在标签中使用

时间:2012-06-06 18:57:00

标签: nsstring nsdata nskeyedarchiver

来自经验丰富的Obj C编码员的帮助将会非常感激,因为我已经坚持了几天,我相信代码是“接近”。

我正在创建用户联系人的名单列表,然后将此列表传回标签。我的问题(我认为)是从我用来将NSKeyedArchiver的列表存档到NSString中的NSData格式。我已经阅读了我能找到的所有内容,但我怀疑我是一个NOOB正在阻止我解读其他一些例子。

这是我的* .m文件的一部分,我将一个人员列表创建为一个名为“_objects”的数组,然后我归档“_objects”(NSKeyArchiver)制作一个NSData(数据)然后我尝试创建来自的NSString(guestListString)。似乎一切顺利,除了“guestListString”在我的人名列表中包含了一堆额外的gobblygook字符(是的,一个高科技术语)。我尝试过其他格式,但它们要么返回(null),要么返回一堆foriegn字符。我怀疑我没有学到一些我需要插入的步骤,以便在我的逗号分隔名称列表周围摆脱这些“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];
    //
}

0 个答案:

没有答案