2010-12-31 2 views
1

이 코드가 : 두 번째 경고를 올바르게하지만 장치에 아무것도 표시되지 않습니다 파일의 내용을 보여줍니다 시뮬레이터에장치에 plist를 쓰는 방법은 무엇입니까? 목표 C 아이폰

#define ALERT(X) {UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Info" message:X delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];[alert show];[alert release];} 

- (id)readPlist:(NSString *)fileName { 
    NSData *plistData; 
    NSString *error; 
    NSPropertyListFormat format; 
    id plist; 

    NSString *localizedPath = [[NSBundle mainBundle] pathForResource:fileName ofType:@"plist"]; 
    plistData = [NSData dataWithContentsOfFile:localizedPath]; 

    plist = [NSPropertyListSerialization propertyListFromData:plistData mutabilityOption:NSPropertyListImmutable format:&format errorDescription:&error]; 
    if (!plist) { 
     NSLog(@"Error reading plist from file '%s', error = '%s'", [localizedPath UTF8String], [error UTF8String]); 
     [error release]; 
    } 

    return plist; 
} 

- (void)writeToPlist: (NSString*)a 
{ 
    NSString *path = [[NSBundle mainBundle] bundlePath]; 
    NSString *finalPath = [path stringByAppendingPathComponent:@"data.plist"]; 
    NSMutableArray* pArray = [[NSMutableArray alloc] initWithContentsOfFile:finalPath]; 

    [pArray addObject:a]; 
    [pArray writeToFile:finalPath atomically: YES]; 
    ALERT(@"finished"); 
    /* This would change the firmware version in the plist to 1.1.1 by initing the NSDictionary with the plist, then changing the value of the string in the key "ProductVersion" to what you specified */ 
} 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    [self writeToPlist:@"this is a test"]; 
    NSArray* the = (NSArray*)[self readPlist:@"data"]; 
    NSString* s = [NSString stringWithFormat:@"%@",the]; 
    ALERT(s); 
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
    // self.navigationItem.rightBarButtonItem = self.editButtonItem; 
} 

을 ?? 내가 뭘 잘못하고있어? 코드/스 니펫을 보여주십시오 ....

+0

[아이폰 SDK : 시뮬레이터에 PLIST에 데이터를 쓸 수 있지만 장치에서 같은 일을 할 수 없어 ???]의 중복 (http://stackoverflow.com/questions/4132477/iphone-sdkcan-write -data-to-plist-on-simulator-do-cant-do-same-at-device) –

답변

0

문제는 mainBundle 폴더에 파일을 쓸 수 없다는 것입니다. 단, 문서 폴더 만 장치에서 액세스 할 수 있습니다.

NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
+0

코드를 어떻게 변경해야합니까? 방금 NSString * 경로 = [[NSBundle mainBundle] bundlePath]; to \t NSString * documentsDirectory = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex : 0]; 과 같은 readplist하지만 작동하지 않습니다. 그것은 나를 (null) 당신이 당신의 대답을 편집하고 전체 코드를 추가 할 수 있습니까? 대단히 감사합니다! 추신 : plist 내 프로젝트에 추가 된 및 장치에 그것을 acess 싶어요. – cocos2dbeginner

+0

나는 plist를 프로그래밍 방식으로 만들어야한다고 생각한다. 내가 어떻게 할 수 있니? 경우 예? – cocos2dbeginner

+0

프로젝트에 추가 된 plist에 액세스 할 수 없으면 DucumentsFolder에서 새 plist를 만들고 읽을 수만 있습니다. –

관련 문제