我正在使用某些php生成的Web服务器读取plist。当我在我的iphone应用程序中将其读入NSArray,然后使用NSLog将NSArray吐出来检查它时,我看到浮点值被视为字符串。我希望将“距离”值视为数字而不是字符串。这个plist显示在一个表视图中,它可以按距离排序,但问题是距离被排序为一个字符串,所以我得到了一些有趣的排序结果。
我可以将距离值从NSArray中的字符串转换为float吗?或者也许是一个更简单的解决方案,比如调整plist定义,或者NSMutableURLRequest代码中的某些东西?
我的plist看起来像这样:
<?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">
<array>
<dict>
<key>name</key>
<string>Pizza Joint</string>
<key>distance</key>
<string>2.1</string>
</dict>
<dict>
<key>name</key>
<string>Burger Kang</string>
<key>distance</key>
<string>5</string>
</dict>
</array>
</plist>
将其读入NSArray后,每个NSLog看起来像这样:
result: (
{
distance = "2.1";
name = "Pizza Joint";
},
{
distance = 5;
name = "Burger Kang";
}
)
以下是检索plist的Objective-C代码:
// Set up url request
// postData and postLength are left out, but I can post in this question if needed.
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:@"http://mysite.com/get_plist.php"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSError *error;
NSURLResponse *response;
NSData *result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *string = [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];
// libraryContent is an NSArray
self.libraryContent = [string propertyList];
NSLog(@"result: %@", self.libraryContent);
答案 0 :(得分:10)
您可以在plist文件中使用real
键类型,例如:
<?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>value</key>
<real>1</real>
</dict>
</plist>
答案 1 :(得分:3)
此表列出了您可以在plist中使用的可能类型:
Table 2-1: Property list types and their various representations
答案 2 :(得分:1)
使用<real>
元素,而不是<string>
。