Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read plist file in iphone

Tags:

ios

I want to read a plist into my application. I want to try to create a tree.

The plist file contents:

  <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>
like image 448
Mukesh Dabhi Avatar asked Aug 01 '13 12:08

Mukesh Dabhi


People also ask

What is a plist file on iPhone?

plist file contains critical information about the configuration of an iOS mobile app—such as iOS versions that are supported and device compatibility—which the operating system uses to interact with the app. This file is automatically created when the mobile app is compiled.

What is plist file Xcode?

plist file in your Xcode project to display the property list editor. Figure 1 shows the editor for the information property list file of a new Cocoa app project. The file created by Xcode comes preconfigured with keys that every information property list should have.


2 Answers

Follow the given snippet for fetching data from 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

Enjoy Programming!

like image 69
Niru Mukund Shah Avatar answered Sep 19 '22 17:09

Niru Mukund Shah


First thing first, declare a property for your plist in the header file

@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

like image 29
Adrian P Avatar answered Sep 22 '22 17:09

Adrian P