我正在从一个Web服务器读取plist,它是用一些PHP生成的。当我在iPhone应用程序中将它读到一个nsarray中,然后用nslog吐出nsarray来检查它时,我看到浮点值被当作字符串处理。我希望将“距离”值视为数字而不是字符串。这个plist显示在一个表视图中,它可以按距离排序,但问题是距离被排序为字符串,所以我得到了一些有趣的排序结果。
我可以将距离值转换为从NSarray中的字符串开始浮动吗?或者可能有一个更简单的解决方案,比如调整plist定义,或者NSmutableurlRequest代码中的某些内容?
我的上唇看起来像这样:
<?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);