2012-02-17 87 views
5

我正在製作一個應用程序,我需要存儲用戶提供的一些信息。我嘗試使用的.plist文件來存儲信息,我發現這一點:iPhone讀/寫.plist文件

NSString *filePath = @"/Users/Denis/Documents/Xcode/iPhone/MLBB/data.plist"; 
NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath]; 
[plistDict setValue:@"Man" forKey:@"Gender"]; 
[plistDict writeToFile:filePath atomically: YES]; 

的問題是,因爲我在iPhone模擬器測試它的應用程序將只只要工作。我試過這個Changing Data in a Plist,但沒有運氣。我也讀過一些關於我需要將它添加到我的包中的內容,但是如何?

新代碼:

- (IBAction)segmentControlChanged{ 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *plistLocation = [documentsDirectory stringByAppendingPathComponent:@"data.plist"]; 
NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:plistLocation]; 

if (Gender.selectedSegmentIndex == 0) { 
    [plistDict setObject:@"Man" forKey:@"Gender"]; 
    [plistDict writeToFile:plistLocation atomically: YES]; 
} 
else 
{ 
    [plistDict setObject:@"Women" forKey:@"Gender"]; 
    [plistDict writeToFile:plistLocation atomically: YES]; 
} 
} 

回答

8

你在設備上使用相同的路徑?設備上的應用程序是沙盒,只能讀/寫文檔目錄中的文件。抓住該文件路徑like this並追加你的plist名稱。這種方法也適用於模擬器。

試試這個:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *plistLocation = [documentsDirectory stringByAppendingPathComponent:@"myplist.plist"]; 
+0

不,我沒有嘗試過一個「真正」的設備上運行,但我不希望它會運行,因爲該目錄不存在於「真實」設備上。我知道他們只能從他們自己的目錄中讀取/寫入,但我如何告訴Xcode使用應用程序目錄中名爲data.plist的文件而不是/ Users/Denis/Documents/Xcode/iPhone/MLBB/data。 plist?我覺得像n00b :-) – Deni 2012-02-17 21:29:56

+0

我發佈的鏈接顯示如何獲取文件目錄作爲字符串。使用stringByAppendingString在最後添加您的plist名稱,然後使用您指定的代碼。這就是全部:) – Nick 2012-02-17 22:08:37

+0

對不起,但我不知道你的意思。我寫了NSString * filePath = [NSHomeDirectory()stringByAppendingPathComponent:@「Documents」];但我不認爲這就是你的意思。你可以請編輯上面的代碼,以便它可以工作,並在此處寫入。我是Xcode的新手,現在我知道成爲n00b的感覺如何:-) – Deni 2012-02-17 22:34:44

14

我猜你已經在Xcode中添加您的plist文件到您的資源文件夾(在這裏我們把圖像,如果沒有的話,你需要的地方,首先)。資源數據默認爲[NSBundle mainBundle],iOS不允許我們更改包內的數據。所以首先您需要將該文件複製到文檔目錄。

以下是將文件NSBundle複製到Documents目錄的代碼。現在

- (NSString *)copyFileToDocumentDirectory:(NSString *)fileName { 
    NSError *error; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
                 NSUserDomainMask, 
                 YES); 
    NSString *documentsDir = [paths objectAtIndex:0]; 
    NSString *documentDirPath = [documentsDir 
             stringByAppendingPathComponent:fileName]; 

    NSArray *file = [fileName componentsSeparatedByString:@"."]; 
    NSString *filePath = [[NSBundle mainBundle] 
             pathForResource:[file objectAtIndex:0] 
                ofType:[file lastObject]]; 

    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    BOOL success = [fileManager fileExistsAtPath:documentDirPath]; 

    if (!success) { 
     success = [fileManager copyItemAtPath:filePath 
             toPath:documentDirPath 
             error:&error]; 
     if (!success) { 
     NSAssert1(0, @"Failed to create writable txt file file with message \ 
             '%@'.", [error localizedDescription]); 
     } 
    } 

    return documentDirPath; 
} 

您可以使用返回documentDirPath訪問文件和操作(讀/寫)過這一點。

plist中的結構是:

<array> 
    <dict>key-value data</dict> 
    <dict>key-value data</dict> 
</array> 

這裏是代碼寫入數據到plist文件:

/* Code to write into file */ 

- (void)addToMyPlist { 
    // set file manager object 
    NSFileManager *manager = [NSFileManager defaultManager]; 

    // check if file exists 
    NSString *plistPath = [self copyFileToDocumentDirectory: 
                 @"MyPlistFile.plist"]; 

    BOOL isExist = [manager fileExistsAtPath:plistPath];  
    // BOOL done = NO; 

    if (!isExist) { 
     // NSLog(@"MyPlistFile.plist does not exist"); 
     // done = [manager copyItemAtPath:file toPath:fileName error:&error]; 
    } 
    // NSLog(@"done: %d",done); 

    // get data from plist file 
    NSMutableArray * plistArray = [[NSMutableArray alloc] 
              initWithContentsOfFile:plistPath]; 

    // create dictionary using array data and bookmarkKeysArray keys 
    NSArray *keysArray = [[NSArray alloc] initWithObjects:@"StudentNo", nil]; 
    NSArray *valuesArray = [[NSArray alloc] initWithObjects: 
            [NSString stringWithFormat:@"1234"], nil]; 

    NSDictionary plistDict = [[NSDictionary alloc] 
                initWithObjects:valuesArray 
                  forKeys:keysArray]; 

    [plistArray insertObject:poDict atIndex:0]; 

    // write data to plist file 
    //BOOL isWritten = [plistArray writeToFile:plistPath atomically:YES]; 
    [plistArray writeToFile:plistPath atomically:YES]; 

    plistArray = nil; 

    // check for status 
    // NSLog(@" \n written == %d",isWritten); 
} 
+0

我該怎麼處理這段代碼?我試過只是粘貼它,但它沒有奏效。我需要把文件名放在什麼地方嗎? – Deni 2012-02-18 09:15:42

+0

資源文件夾在哪裏? – Deni 2012-02-18 09:24:24

+0

這段代碼是爲plist文件獲取路徑,這只不過是代碼中的「plistLocation」。 首先添加您的plist文件,與您的圖像。 然後調用這個方法來獲得plist文件路徑,將它存儲在你的「plistLocation」變量中,並且用代碼 – Mrunal 2012-02-18 10:33:25