How to handle float values ​​in plist

I am reading in plist from a web server generated using php. When I read this in NSArray in my iphone application and then spat out the NSArray with NSLog to check it, I see that the float values ​​are treated as strings. I would like the distance values ​​to be treated as numeric rather than string. This plist is displayed as a table where it can be sorted by distance, but the problem is that the distance is sorted as a string, so I get some funny sorting results.

Is it possible to convert distance values ​​to float from string to NSArray? Or maybe this is a simpler solution, such as setting up a plist definition or maybe something in NSMutableURLRequest code?

My plist looks like this:

<?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> 

After reading it in NSArray, it looks like in NSLog:

  result: ( { distance = "2.1"; name = "Pizza Joint"; }, { distance = 5; name = "Burger Kang"; } ) 

Here is the Objective-C code that plist extracts:

 // 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); 
+4
source share
3 answers

You can use the real key type in your plist files, for example:

 <?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> 
+10
source

This table lists the possible types you can use in plist:

Table 2-1: Property List Types and Their Different Views

+3
source

Use the <real> element, not the <string> .

+1
source

Source: https://habr.com/ru/post/1306875/


All Articles