2011-12-09 2 views
3

저는 iPhone 개발에 익숙하지 않습니다. 웹 서버에서 데이터를 가져 와서 런타임에서 plist를 작성하는 샘플 Objective-C 코드가 있는지 알고 싶습니다. 런타임시 plist를 쉽게 만들 수 있도록 데이터 형식이 무엇인지 알고 싶습니다.iOS SDK를 사용하여 런타임에 plist를 만드시겠습니까?

+0

plist는 xml 파일이므로 모든 텍스트 편집기로 볼 수 있습니다 – progrmr

답변

2

PLIST 파일은 주로 키/값 방식으로 직렬화 된 오브젝트를 저장하는 맥 OS X에 의해 사용된다. 속성 목록 파일에는 여러 가지 종류가 있습니다. ASCII, XML 및 Binary.So 귀하의 경우 서버에서 XML 형식으로 데이터를 보내야합니다. 서버에서 데이터를받은 후 런타임에 plist 생성 할 수 있습니다. 아래 코드를 사용하여 .plist 파일에 데이터를 쓸 수 있습니다.

- (void)writeToPlist{ 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"WebData.plist"]; 
NSArray *dataToSave = [dataToSave writeToFile:filePath atomically:YES]; 
} 

this을 참조하여 주시기 바랍니다 또한 서버 측 link1link2link3link4에 대한 이러한 링크.

+1

이것은 이론적으로 질문에 대답 할 수 있지만 답변에 링크 된 기사의 필수 부분을 포함시키고 [참조 용 링크] (http://meta.stackexchange.com/q/8259)를 제공하십시오. 그렇게하지 않으면 링크 썩음으로 위험에 처해 있습니다. – Kev

9

NSDictionary A의 매우 간단한 :

#define DOCUMENTS_PATH [NSSearchPathForDirectoriesInDomains(NSDocumentsDirectory, NSUserDomainMask, YES) objectAtIndex:0] 

NSDictionary *myDictionary = [self parseWebResult]; 

// note that myDictionary must only contain values of string, 
// int, bool, array (once again containing only the same types), 
// and other primitive types (I believe NSDates are valid too). 
[myDictionary writeToFile:[DOCUMENTS_PATH stringByAppendingPathComponent:@"myDict.plist"] atomically:YES]; 

// reading in the dictionary 
myDictionary = [NSDictionary dictionaryWithContentsOfFile:[DOCUMENTS_PATH stringByAppendingPathComponent:@"myDict.plist"]]; 
1
// Get the full path of file in NSDocuments 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"MyData.plist"]; 

// Save data to file 
NSArray *dataToSave = /* put data in it */; 
[dataToSave writeToFile:filePath atomically:YES]; 
+0

또한 NSDictionary 개체와도 작동합니다. – Niko

관련 문제