-1

I am learning Objective-C language. I have covered the following questions:

What is plist?
How to create a plist file?
What are the representation of plist file?

But I did not find the answer of these questions:
Why do we store a plist in NSArray?
What is the need to store a plist into NSArray?

Can you please help me to clear my doubts?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Anzal Beg
  • 19
  • 3
  • you can read more (everything) about property lists in [Apple's documentation](https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/PropertyLists/Introduction/Introduction.html). – holex Jun 08 '15 at 15:32
  • Have you been able to resolve your questions ? – Miknash Jul 23 '15 at 09:09

4 Answers4

0

Sometime you want to do something at runtime or create something base on something else.

Let's imagine you have a game with a different levels and you want the levels to be vary you can hard code the levels in code but much nicer would be store it in the file. iOS lets you very easily load plist to array or dictionary and that's the reason why iOS developers choose to use it.

// Example of plist file

Level1:
    NumberOfEnemies: 6
    ScoreTarget: 100
    ....
Level2:
    NumberOfEnemies: 12
    ScoreTarget: 120
    ....

When you load the game you can read the plist and you can load the level base on the content. It's much easier to add another level or add more customisation to the plist (file) that go back to the code any do it there.

It's just one of the examples but you could use plist to do much more.

Greg
  • 25,317
  • 6
  • 53
  • 62
0

Plist are mostly configuration files or place to store some values that you know that will never change. One particular example of the plist file is info.plist where you define attributes for the application.

Plist are nothing else but the same old XML file with attributes and elements. That said, you actually access plist through dictionary, not array.

For your question why do we store it in dictionary, you use dictionary to access these values since there is no other way to extract the information from it.

One more thing, plists have their own restrictions regarding the type you can store: Array, Dictionary, String, Date, Data, Number and Boolean.

Miknash
  • 7,888
  • 3
  • 34
  • 46
  • One small comment. Plists can be structured as arrays or dictionaries, we just more commonly see them structured as dictionaries :) – Logan Jun 08 '15 at 13:40
  • Thank you for giving me such a sufficient information.But i want to discuss the situation where we use +arrayWithContentsOfFile method of NSArray class in order to store plist file into an array. I want to know the need ,why we storing the plist in array using this method. – Anzal Beg Jun 08 '15 at 13:58
  • well, you can access plist from any part of code, wherever you need it. It can be appdelegate, viewcontroller etc. Do you need the code for that ? – Miknash Jun 08 '15 at 14:06
0

PList : is a "Property List" file, uses the XML format to store objects such as arrays, dictionaries, String, Data, Number and Boolean.

How to create :

NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"myPlistFile.plist"];
if (![fileManager fileExistsAtPath: path])
{
    NSString *bundle = [[NSBundle mainBundle] pathForResource:@”myPlistFile” ofType:@”plist”];
    [fileManager copyItemAtPath:bundle toPath:path error:&error];
}
[myDict writeToFile:plistPath atomically: YES];

Representation : Based on what is stored in plist file .

  1. Key and Value. (NSDictionaries)
  2. Key and multiple value . (NSArray) etc..
Vicky
  • 602
  • 5
  • 16
0

What is PLIST?

Plist stands for Property List. Plist is used to store data in hierarchical manner.A Property List can contain containers and primitives.

Containers –  Array, Dictionary
Primitives  –  Boolean, String, Number(Integer,Float), Date, Data

A Container can contain other containers and primitive data-types. i.e. Dictionary can contain other Dictionaries,Arrays and Primitives. Array can contain other Dictionaries,Arrays,Primitives.

How to Create PLIST File?

 STEP 1: Right Click the Project
 STEP 2: Choose New File
 STEP 3: Select Resource from left side of the Template.
 STEP 4: Then Click Property List
 STEP 5: Click NEXT button of the Bottom.
 STEP 6: Give property List Name.
 STEP 7: Finally Click OK

For more Reference please go through the below link

https://stackoverflow.com/questions/9044735/steps-to-create-and-edit-a-plist-file-in-xcode

What are the Representation of PLIST file?

  A property list can be stored in one of three different ways:

      1. in an XML representation, 
      2. in a binary format, or 
      3. in an “old-style” ASCII format inherited from OpenStep. 

 You can serialize property lists in the XML and binary formats. The serialization API with the old-style format is read-only

XML property lists are more portable than the binary alternative and can be manually edited, but binary property lists are much more compact; as a result, they require less memory and can be read and written much faster than XML property lists. In general, if your property list is relatively small, the benefits of XML property lists outweigh the I/O speed and compactness that comes with binary property lists. If you have a large data set, binary property lists, keyed archives, or custom data formats are a better solution.

I refer the link for Why do we store a plist in NSArray? and What is the need to store a plist into NSArray?

1.http://hayageek.com/plist-tutorial/

2.https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/PropertyLists/AboutPropertyLists/AboutPropertyLists.html

3.http://nscookbook.com/2013/02/ios-programming-recipe-13-using-property-lists-plists/

4.Getting plist data in a order

Community
  • 1
  • 1
user3182143
  • 9,459
  • 3
  • 32
  • 39