所以我有一个plist结构化字符串,它是动态获取的(不是来自文件系统).我如何将此字符串转换为NSDictionary.
我尝试将其转换为NSData,然后转换为带有NSPropertyListSerialization的NSDictionary,但当我尝试访问NSDictionary时,它返回"[NSCFString objectAtIndex:]:无法识别的选择器发送到实例0x100539f40",表明我的词典未成功创建.
NSString的示例(即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">
<dict>
<key>Key1</key>
<dict>
<key>Test1</key>
<false/>
<key>Key2</key>
<string>Value2</string>
<key>Key3</key>
<string>value3</string>
</dict>
</dict>
</plist>
Run Code Online (Sandbox Code Playgroud)
谢谢!
我在我的身上宣布了这个伊娃
ViewController.h
#import <UIKit/UIKit.h>
@interface FirstViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
{
NSArray *sortedCountries;
}
@property (nonatomic, retain) NSArray *sortedCountries;
@end
Run Code Online (Sandbox Code Playgroud)
在ViewController.m中,sortedCountries -(void)ViewDidLoad{}通过存储已排序的.plist的结果来完成它的工作 .
什么时候
-(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {}
Run Code Online (Sandbox Code Playgroud)
在下面调用,sortedCountries返回 (null)
为什么sortedCountries的值不存在?我retain在第一个函数中添加了一个......我想我在这里缺少一个基本的Objective-C租户.
ViewController.m
#import "FirstViewController.h"
@implementation FirstViewController
@synthesize sortedCountries;
-(void)viewDidLoad {
NSString *path = [[NSBundle mainBundle] pathForResource:@"countries" ofType:@"plist"];
NSArray *countries = [NSArray arrayWithContentsOfFile:path];
NSSortDescriptor *descriptor = [[[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES] autorelease];
NSArray *sortedCountries = [[countries sortedArrayUsingDescriptors:[NSArray arrayWithObject:descriptor]]retain];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView …Run Code Online (Sandbox Code Playgroud)