I want to read a plist into my application. I want to try to create a tree.
我想在我的应用程序中读取一个plist。我想尝试创建一棵树。
The plist file contents:
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>
2 个解决方案
#1
5
Follow the given snippet for fetching data from plist
按照给定的片段从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]];
}
where Key -> for accessing particular field from plist
其中Key - >用于从plist访问特定字段
Enjoy Programming!
#2
1
First thing first, declare a property for your plist in the header file
首先,在头文件中为plist声明一个属性
@property (strong, nonatomic) NSArray *content;
Then synthesize it implementation file
然后合成它的实现文件
@synthesize content = _content;
Then you have to declare that array in implementation file. Something like this
然后你必须在实现文件中声明该数组。像这样的东西
-(NSArray *)content
{
if (!_content) {
_content = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"]];
}
return _content;
}
Then you have to declare the data source. Something like the following
然后你必须声明数据源。像下面这样的东西
- (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;
}
That's pretty much it in the nutshell.
简而言之,这就是它。
Hope this helps out
希望这会有所帮助
#1
5
Follow the given snippet for fetching data from plist
按照给定的片段从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]];
}
where Key -> for accessing particular field from plist
其中Key - >用于从plist访问特定字段
Enjoy Programming!
#2
1
First thing first, declare a property for your plist in the header file
首先,在头文件中为plist声明一个属性
@property (strong, nonatomic) NSArray *content;
Then synthesize it implementation file
然后合成它的实现文件
@synthesize content = _content;
Then you have to declare that array in implementation file. Something like this
然后你必须在实现文件中声明该数组。像这样的东西
-(NSArray *)content
{
if (!_content) {
_content = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"]];
}
return _content;
}
Then you have to declare the data source. Something like the following
然后你必须声明数据源。像下面这样的东西
- (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;
}
That's pretty much it in the nutshell.
简而言之,这就是它。
Hope this helps out
希望这会有所帮助