我想在我的应用程序中读取一个plist。我想尝试创建一棵树。
plist文件内容:
<plist version="1.0">
<array>
<dict>
<key>Name</key>
<string>Test Webservice</string>
<key>child</key>
<array>
<dict>
<key>child</key>
<array>
<dict>
<key>child</key>
<array>
<dict>
<key>id</key>
<integer>4291</integer>
<key>name</key>
<string>1.1.1</string>
</dict>
<dict>
<key>id</key>
<integer>4292</integer>
<key>name</key>
<string>1.1.2</string>
</dict>
</array>
<key>id</key>
<integer>4290</integer>
<key>name</key>
<string>1.1</string>
</dict>
<dict>
<key>child</key>
<array>
<dict>
<key>id</key>
<integer>4294</integer>
<key>name</key>
<string>1.2.1</string>
</dict>
</array>
<key>id</key>
<integer>4293</integer>
<key>name</key>
<string>1.2</string>
</dict>
</array>
<key>id</key>
<integer>4287</integer>
<key>name</key>
<string>1</string>
</dict>
<dict>
<key>child</key>
<array>
<dict>
<key>child</key>
<array>
<dict>
<key>child</key>
<array>
<dict>
<key>id</key>
<integer>4297</integer>
<key>name</key>
<string>2.1.1.1</string>
</dict>
</array>
<key>id</key>
<integer>4296</integer>
<key>name</key>
<string>2.1.1</string>
</dict>
</array>
<key>id</key>
<integer>4295</integer>
<key>name</key>
<string>2.1</string>
</dict>
</array>
<key>id</key>
<integer>4288</integer>
<key>name</key>
<string>2</string>
</dict>
<dict>
<key>id</key>
<integer>4289</integer>
<key>name</key>
<string>3</string>
</dict>
</array>
<key>id</key>
<integer>4286</integer>
</dict>
</array>
</plist>
答案 0 :(得分:5)
按照给定的片段从plist
获取数据NSString *strplistPath = [[NSBundle mainBundle] pathForResource:<plistName> ofType:@"plist"];
// read property list into memory as an NSData object
NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:strplistPath];
NSString *strerrorDesc = nil;
NSPropertyListFormat plistFormat;
// convert static property liost into dictionary object
NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization propertyListFromData:plistXML mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&plistFormat errorDescription:&strerrorDesc];
if (!temp) {
NSLog(@"Error reading plist: %@, format: %d", strerrorDesc, plistFormat);
} else {
// assign values
NSMutableArray *plistArray = [NSMutableArray arrayWithArray:[temp objectForKey:key]];
}
其中Key - &gt;从plist访问特定字段
享受编程!
答案 1 :(得分:1)
首先,首先在头文件中声明plist的属性
@property (strong, nonatomic) NSArray *content;
然后合成它的实现文件
@synthesize content = _content;
然后你必须在实现文件中声明该数组。像这样的东西
-(NSArray *)content
{
if (!_content) {
_content = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"]];
}
return _content;
}
然后你必须声明数据源。类似于以下内容
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.content count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.textLabel.text = [[self.content objectAtIndex:indexPath.row] valueForKey:@"city"];
cell.detailTextLabel.text = [[self.content objectAtIndex:indexPath.row] valueForKey:@"state"];
return cell;
}
简而言之,这就是它。
希望这会有所帮助