2014-04-27 2 views
0
self.toDoItems = [[NSMutableArray alloc] init]; //initialize array 
self.dataFileName = @"PropertyList"; 
self.filePath = [self.dataFileName stringByAppendingString:@".plist"]; 
self.rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
                  NSUserDomainMask, YES) objectAtIndex:0]; 
self.filePath = [self.rootPath stringByAppendingPathComponent:self.filePath]; 
self.NSSRootNodeName = @"ToDoList"; //name of the root node in the property list 

// if (![[NSFileManager defaultManager] fileExistsAtPath: self.filePath]) { 
//  self.filePath = [[NSBundle mainBundle] pathForResource:self.dataFileName ofType:@"plist"]; 
// } 

NSLog(@"File path: %@", self.filePath); 

// Uncomment the following line to preserve selection between presentations. 
self.clearsSelectionOnViewWillAppear = NO; 

// Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
self.navigationItem.rightBarButtonItem = self.editButtonItem; 

//read xml file and load to array 
NSString *errorDesc = nil; 
NSPropertyListFormat format; 

NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:self.filePath]; 
if (plistXML==nil) { 
    NSLog(@"Error reading plistXML"); 
} 
NSDictionary *rootDictionary = (NSDictionary *)[NSPropertyListSerialization 
               propertyListFromData:plistXML 
               mutabilityOption:NSPropertyListMutableContainersAndLeaves 
               format:&format 
               errorDescription:&errorDesc]; 
if (!rootDictionary) { 
    NSLog(@"Error reading plist: %@, format: %d", errorDesc, format); 
} 

출력이 아래에 있습니다. 왜 이러한 오류가 아래에 표시되는지 잘 모르겠습니다. 내 PropertyList.plist가 내 애플리케이션의 내 문서 폴더에 있습니다. 누군가 내가 잘못하고있는 것을 지적 할 수 있습니다.objective-c가 속성 목록에서 읽을 수 없습니다.

File path: /Users/Computer/Library/Application Support/iPhone Simulator/7.1/Applications/78B5F9BA-8376-4001-ACA0-936D9B4D6342/Documents/PropertyList.plist 

Error reading plistXML 
Error reading plist: stream had too few bytes, format: 0 
+0

시도 확인 : 경우 ([[NSFileManager를 defaultManager] fileExistsAtPath : self.filePath]) contentsAtPath을 읽을 전에 : – stosha

+0

을하면 I 않은 코멘트 애플리케이션 번들에 관한 내용. 읽기 부분에서 작동하지만 여전히 변경 사항을 특성 파일에 다시 써서 문제가 있습니다. ~/documents 폴더에서 파일을 찾을 수 없다면 Apple에 따르면 내 이해는 응용 프로그램 번들에서 시도 할 것이므로 감각을 만듭니다. 하지만 내 변경 사항을 다시 작성하려면 어떻게해야합니까? – HaiNguyen

+0

@HaiNguyen : 응용 프로그램 번들에 * 쓸 수 없습니다. 읽기 전용입니다. 따라서 응용 프로그램 번들에서 plist를 읽은 경우에도 Documents 디렉토리 (또는 다른 쓰기 가능한 디렉토리)에 써야합니다. –

답변

0

FWIW, 여기에 나를 위해 일한 것입니다 :

NSFileManager *fileManager = [[NSFileManager alloc] init]; 
if ([fileManager fileExistsAtPath: [self filePathForState]]) 
{ 
    printf("found state file\n"); 
    NSData *data = [NSData dataWithContentsOfFile:[self filePathForState]]; 
    ... 
} 
else 
{ 
    printf("no state file found\n"); 
} 
[fileManager release]; 

- (NSString *) filePathForState 
{ 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *result = [documentsDirectory stringByAppendingPathComponent: @"circles06"]; 
    printf("filePathForState: %s\n", [result UTF8String]); 
    return result; 
} 

- (void) saveState 
{ 
    NSData *state = [model stateData]; 
    [state writeToFile: [self filePathForState] atomically: NO]; 
} 
+0

을 filePathForState의 값이 같이 무엇인가 -.? 감사 – HaiNguyen

+0

난 당신의 코드가있는 viewDidLoad 방법에 방법을 사용, 대체 "circleles06"내 파일 "PropertyList.plist"와 나는이 메시지를 받고있다. 내가 할 수있는 다른 것을 알고 있니? [link] (http://example.com) _italic_ ** bold **'code' filePathForState :/사용자/컴퓨터/라이브러리/응용 프로그램 지원/아이폰 시뮬레이터/7.1/응용 프로그램/78B5F9BA-8376-4001-ACA0-936D9B4D6342이/문서/PropertyList.plist 더 상태 파일을 찾을 수 없습니다 [/ 코드] – HaiNguyen

+0

이 당신이 아직하지 않은 것을 나타냅니다 크레아 파일을 제출 했어. 이 코드는 파일을 읽거나 파일을 작성하거나 쓰는 것이 아닙니다. 이제 파일에 쓰는 내 방법을 추가했습니다 (내 코드에서 축 어적으로). –

관련 문제