将id转换为nsstring

时间:2011-08-22 06:16:36

标签: iphone

我使用NSPropertyListSerialization将nsdictionary转换为xml,代码如下

NSMutableDictionary *rootObj = [NSMutableDictionary dictionaryWithCapacity:2];
    NSDictionary *innerDict;
    NSString *name;
    NSDate *dob;
    NSArray *scores;

    scores = [NSArray arrayWithObjects:[NSNumber numberWithInt:6],
              [NSNumber numberWithFloat:4.6], [NSNumber numberWithLong:6.0000034], nil];
    name = @"George Washington";
    dob = [NSDate dateWithString:@"1732-02-17 04:32:00 +0300"];
    innerDict = [NSDictionary dictionaryWithObjects:
                 [NSArray arrayWithObjects: name, dob, scores, nil]
                                            forKeys:[NSArray arrayWithObjects:@"Name", @"DOB", @"Scores"]];
    [rootObj setObject:innerDict forKey:@"Washington"];

    scores = [NSArray arrayWithObjects:[NSNumber numberWithInt:8],
              [NSNumber numberWithFloat:4.9],
              [NSNumber numberWithLong:9.003433], nil];
    name = @"Abraham Lincoln";
    dob = [NSDate dateWithString:@"1809-02-12 13:18:00 +0400"];
    innerDict = [NSDictionary dictionaryWithObjects:
                 [NSArray arrayWithObjects: name, dob, scores, nil]
                                            forKeys:[NSArray arrayWithObjects:@"Name", @"DOB", @"Scores"]];
    [rootObj setObject:innerDict forKey:@"Lincoln"];


    NSLog(@"the dictionary is :%@",rootObj);


    id plist = [NSPropertyListSerialization dataFromPropertyList:(id)rootObj
                                                          format:NSPropertyListXMLFormat_v1_0 errorDescription:nil];

NSLog(@ "the xml file is :%@",plist);

和控制台的输出如下......

XmltoNSDictionary[2478:207] the dictionary is :{
    Lincoln =     {
        DOB = "1809-02-12 09:18:00 +0000";
        Name = "Abraham Lincoln";
        Scores =         (
            8,
            "4.9",
            9
        );
    };
    Washington =     {
        DOB = "1732-02-17 01:32:00 +0000";
        Name = "George Washington";
        Scores =         (
            6,
            "4.6",
            6
        );
    };
}
  

2011-08-22 11:35:09.611 XmltoNSDictionary [2478:207] xml文件是   :< 3c3f786d 6c207665 7273696f 6e3d2231 2e302220 656e636f 64696e67   3d225554 462d3822 3f3e0a3c 21444f43 54595045 20706c69 73742050   55424c49 4320222d 2f2f4170 706c652f 2f445444 20504c49 53542031   2e302f2f 454e2220 22687474 703a2f2f 7777772e 6170706c 652e636f   6d2f4454 44732f50 726f7065 7274794c 6973742d 312e302e 64746422   3e0a3c70 6c697374 20766572 73696f6e 3d22312e 30223e0a 3c646963   743e0a09 3c6b6579 3e4c696e 636f6c6e 3c2f6b65 793e0a09 3c646963   743e0a09 093c6b65 793e444f 423c2f6b 65793e0a 09093c64 6174653e   31383039 2d30322d 31325430 393a3138 3a30305a 3c2f6461 74653e0a   09093c6b 65793e4e 616d653c 2f6b6579 3e0a0909 3c737472 696e673e   41627261 68616d20 4c696e63 6f6c6e3c 2f737472 696e673e 0a09093c   6b65793e 53636f72 65733c2f 6b65793e 0a09093c 61727261 793e0a09   09093c69 6e746567 65723e38 3c2f696e 74656765 723e0a09 09093c72   65616c3e 342e3930 30303030 30393533 36373433 31363c2f 7265616c   3e0a0909 093c696e 74656765 723e393c 2f696e74 65676572 3e0a0909   3c2f6172 7261793e 0a093c2f 64696374 3e0a093c 6b65793e 57617368   696e6774 6f6e3c2f 6b65793e 0a093c64 6963743e 0a09093c 6b65793e   444f423c 2f6b6579 3e0a0909 3c646174 653e3137 33322d30 322d3137   5430313a 33323a30 305a3c2f 64617465 3e0a0909 3c6b6579 3e4e616d   653c2f6b 65793e0a 09093c73 7472696e 673e4765 6f726765 20576173   68696e67 746f6e3c 2f737472 696e673e 0a09093c 6b65793e 53636f72   65733c2f 6b65793e 0a09093c 61727261 793e0a09 09093c69 6e746567   65723e36 3c2f696e 74656765 723e0a09 09093c72 65616c3e 342e3539   39393939 39303436 33323536 38343c2f 7265616c 3e0a0909 093c696e   74656765 723e363c 2f696e74 65676572 3e0a0909 3c2f6172 7261793e   0a093c2f 64696374 3e0a3c2f 64696374 3e0a3c2f 706c6973 743e0a>

现在我想做的是以xml(nsstring)格式显示id ....

任何人都可以帮助我如何做到这一点....

1 个答案:

答案 0 :(得分:1)

将其添加到代码段的末尾:

NSString *plistString = [[NSString alloc] initWithData:plist encoding:NSUTF8StringEncoding];
NSLog(@"the plist is: %@", plistString);

2011-08-21 23:32:49.324 so7143762iPhone[5474:207] the plist is: <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Lincoln</key>
    <dict>
        <key>DOB</key>
        <date>1809-02-12T09:18:00Z</date>
        <key>Name</key>
        <string>Abraham Lincoln</string>
        <key>Scores</key>
        <array>
            <integer>8</integer>
            <real>4.9000000953674316</real>
            <integer>9</integer>
        </array>
    </dict>
    <key>Washington</key>
    <dict>
        <key>DOB</key>
        <date>1732-02-17T01:32:00Z</date>
        <key>Name</key>
        <string>George Washington</string>
        <key>Scores</key>
        <array>
            <integer>6</integer>
            <real>4.5999999046325684</real>
            <integer>6</integer>
        </array>
    </dict>
</dict>
</plist>

此外,在OS X中,我需要在nil的调用中将arrayWithObjects添加到列表的末尾。我认为它对iOS来说是一样的。