Iphone 将id转换为nsstring
我使用NSPropertyListSerialization将nsdictionary转换为xml,代码如下Iphone 将id转换为nsstring,iphone,Iphone,我使用NSPropertyListSerialization将nsdictionary转换为xml,代码如下 NSMutableDictionary *rootObj = [NSMutableDictionary dictionaryWithCapacity:2]; NSDictionary *innerDict; NSString *name; NSDate *dob; NSArray *scores; scores = [NSArray arrayW
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 XMLTONSDICTIONAL[2478:207]xml文件是
:
现在我想做的是以xml(nsstring)格式显示id
任何人都可以帮助我如何做到这一点。…将此添加到代码段的末尾:
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>
NSString*plistString=[[NSString alloc]initWithData:plist编码:NSUTF8StringEncoding];
NSLog(@“plist为:%@”,plistString);
2011-08-21 23:32:49.324 SO7143762电话[5474:207]列表为:
林肯
出生日期
1809-02-12T09:18:00Z
名称
亚伯拉罕·林肯
分数
8.
4.9000000953674316
9
华盛顿
出生日期
1732-02-17T01:32:00Z
名称
乔治华盛顿
分数
6.
4.5999999046325684
6.
另外,在OSX中,我需要在调用
arrayWithObjects
的列表末尾添加nil
。我假设iOS也是如此。在代码片段的末尾添加以下内容:
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>
NSString*plistString=[[NSString alloc]initWithData:plist编码:NSUTF8StringEncoding];
NSLog(@“plist为:%@”,plistString);
2011-08-21 23:32:49.324 SO7143762电话[5474:207]列表为:
林肯
出生日期
1809-02-12T09:18:00Z
名称
亚伯拉罕·林肯
分数
8.
4.9000000953674316
9
华盛顿
出生日期
1732-02-17T01:32:00Z
名称
乔治华盛顿
分数
6.
4.5999999046325684
6.
另外,在OSX中,我需要在调用
arrayWithObjects
的列表末尾添加nil
。我想iOS也是如此。我刚刚在iPhone应用程序中尝试过,是的,你确实需要用nil
来结束列表。它不会崩溃,但您会收到警告“函数调用中缺少sentinel”,这是一个很好的例子,说明浮点不能精确表示小数。我还有另一个问题,是否可以编辑xml文件的头标记,即我不想在xml文件中显示此标记。。。有没有可能……如果你还有其他问题,你应该把它作为一个问题发布。评论不是提问的正确位置。我刚刚在iPhone应用程序中尝试过,是的,你确实需要用nil
来结束列表。它不会崩溃,但您会收到警告“函数调用中缺少sentinel”,这是一个很好的例子,说明浮点不能精确表示小数。我还有另一个问题,是否可以编辑xml文件的头标记,即我不想在xml文件中显示此标记。。。有没有可能……如果你还有其他问题,你应该把它作为一个问题发布。评论不是提问的正确位置。