plistから数値を読み込む

NSString *path = [[NSBundle mainBundle] pathForResource:@"places" ofType:@"plist"];
placesDictionary = [[NSDictionary alloc] initWithContentsOfFile:path];
NSArray *places = [placesDictionary objectForKey:@"places"];
NSDictionary *place = [places objectAtIndex:indexPath.row];
NSNumber *lat = [place objectForKey:@"latitude"];
NSNumber *lng = [place objectForKey:@"longitude"];

PlaceAppDelegate *appDelegate = (PlaceAppDelegate *) [[UIApplication sharedApplication] delegate];
appDelegate.latitude = [lat doubleValue];
appDelegate.longitude = [lng doubleValue];
  • (id)objectForKey:(id)aKeyはid型を返すので、doubleなどプリミティブ型に直接入れることができない
  • 数値をオブジェクトとして扱うためにNSNumberを使う
  • NSNumberを通してプリミティブ型に変換する
<?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>places</key>
	<array>
		<dict>
			<key>name</key>
			<string>Tokyo</string>
			<key>latitude</key>
			<real>35.689488</real>
			<key>longitude</key>
			<real>139.691706</real>
		</dict>
		<dict>
			<key>name</key>
			<string>New York</string>
			<key>latitude</key>
			<real>40.714353</real>
			<key>longitude</key>
			<real>-74.005973</real>
		</dict>
		<dict>
			<key>name</key>
			<string>London</string>
			<key>latitude</key>
			<real>51.500152</real>
			<key>longitude</key>
			<real>-0.126236</real>
		</dict>
	</array>
</dict>
</plist>