5

我想将 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>
4

2 回答 2

7

按照给定的片段从 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 -> 用于从 plist 访问特定字段

享受编程!

于 2013-08-01T12:09:00.067 回答
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;
}

简而言之,就是这样。

希望这会有所帮助

于 2013-08-01T12:19:51.000 回答