2010-12-31 2 views
1

Ich habe diesen Code:Wie schreibe ich einen Plist auf ein Gerät? Objective C iphone

#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; 
} 

Auf dem Simulator der zweite Alarm, den Inhalt der Datei korrekt zeigt aber auf dem Gerät zeigt es nichts ?? Was mache ich falsch? Bitte zeigen Sie einen Code/Schnipsel ....

+0

Duplikat [iPhone SDK: Kann Daten schreiben auf Simulator plist kann aber gleiche Sache auf Gerät nicht tun ???] (http://stackoverflow.com/questions/4132477/iphone-sdkcan-write -data-to-plist-on-simulator-aberkann-same-thing-at-device) –

Antwort

0

Das Problem ist, dass Sie keine Datei in den Ordner mainBundle schreiben konnten, nur der Ordner Dokumente ist auf Ihrem Gerät zugänglich.

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

Wie soll ich meinen Code ändern? Ich habe gerade NSString geändert * path = [[NSBundle mainBundle] bundlePath]; bis \t NSString * documentsDirectory = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex: 0]; und das gleiche in readplist, aber es funktioniert nicht. es gibt mir (null) könntest du deine antwort bearbeiten und bitte den vollen code hinzufügen? vielen Dank! PS: Das Projekt wurde zu meinem Projekt hinzugefügt und ich möchte es auf dem Gerät nutzen. – cocos2dbeginner

+0

Ich denke, ich muss plist programmatisch erstellen. wie kann ich das machen? Falls ja? – cocos2dbeginner

+0

Sie konnten nicht auf ein Plist zugreifen, das zu Ihrem Projekt hinzugefügt wurde. Sie konnten nur ein neues Plist in DucumentsFolder erstellen und es lesen. –