본문 바로가기

Objective-C

[Objective-C] plist 간단한 사용법

출처 : http://blog.daum.net/shakalis/3

 

다음은 plist를 만들어서 내용을 읽어들인 후, 출력하는 예제이다.

 

1) 우선 새로운 plist를 생성한다.

-> xcode의 [resources] 디렉토리에서 오른쪽 클릭

-> [add] -> [new file] -> [resource] -> [property list]

 

2) 작성한 plist를 연다.

key type value

-----------------------------

root dictionary

 

현재 예제에서는 array를 쓸 예정이므로 type을 dictionary가 아닌 array로 바꾼다.

그리고 오른쪽 클릭을 하여 [add row]를 선택하여 데이타를 추가한다.

 

key type value

-----------------------------

root array

item0 string Penguin

item1 string Tiger

item2 string Bunny

3) plist를 출력해야 하는 부분에 다음과 같은 코드를 추가한다.

 

// get MyList.plist path

NSString *path = [[NSBundle mainBundlepathForResource:@"MyList"ofType:@"plist"];

// read the plist from the given path

NSMutableArray *myList = [[NSMutableArray alloc]initWithContentsOfFile:path];

 

// print all value of myList

for(NSString *value in myList)

{

NSLog(@"myList = %@", value);

}

 

// release all the