簡體   English   中英

如何在plist中寫入數據?

[英]How to write data in plist?

我已經在資源文件夾中創建了save.plist。 我直接在其中寫入了一些數據(無需使用編碼)。 我能夠讀取該數據,但無法通過代碼將其寫入相同的save.plist。 通過使用以下代碼,我試圖寫入數據,但是數據被存儲在我的.app plist中。 代碼在這里

NSString *errorDesc = nil;

NSPropertyListFormat format;

NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"save" ofType:@"plist"];

NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];

NSMutableDictionary *temp = (NSMutableDictionary *)[NSPropertyListSerialization
                     propertyListFromData:plistXML
                              mutabilityOption:NSPropertyListMutableContainersAndLeaves
                  format:&format errorDescription:&errorDesc];

if (!temp) {

    NSLog(errorDesc);

    [errorDesc release];        
    }
    //  [temp setValue:@"123" forKey:@"line1"];
    //  [temp writeToFile:plistPath atomically: YES];

    //Reading data from save.plist
    NSLog([temp objectForKey:@"name"]);
    NSLog([temp objectForKey:@"wish"]);
    NSNumber *num=[temp valueForKey:@"roll"];
    int i=[num intValue];
    printf("%d",i);
        //writitng the data in save.plist

    [temp setValue:@"green" forKey:@"color"];
    [temp writeToFile:plistPath atomically: NO];
    NSMutableDictionary *temp1 = (NSMutableDictionary *)[NSPropertyListSerialization
                propertyListFromData:plistXML                                                            
                                mutabilityOption:NSPropertyListMutableContainersAndLeaves
                format:&format errorDescription:&errorDesc];

    NSLog([temp objectForKey:@"color"]);

我想要的是,我要寫入的數據應僅寫入存儲在引用中的save.plist中。 我是這個概念的新手。 因此,如果有人知道,請幫助我。 提前致謝。 :-)

我不知道我是否理解您的問題,但是如果您想在.app捆綁包中寫入.plist,則可能是您做錯了什么。 如果要存儲首選項,則應考慮使用NSUserDefaults
如果您確實要修改捆綁的.plist,請使用以下代碼:

NSString *plistPath = nil;
NSFileManager *manager = [NSFileManager defaultManager];
if (plistPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"Contents/Info.plist"]) 
{
    if ([manager isWritableFileAtPath:plistPath]) 
    {
        NSMutableDictionary *infoDict = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath];
        [infoDict setObject:[NSNumber numberWithBool:hidden] forKey:@"LSUIElement"];
        [infoDict writeToFile:plistPath atomically:NO];
        [manager changeFileAttributes:[NSDictionary dictionaryWithObject:[NSDate date] forKey:NSFileModificationDate] atPath: [[NSBundle mainBundle] bundlePath]];
    }
}

更新:
Nate Flink指出,已棄用了上面使用的某些NSFileManager方法。 他用以下替換方法發布了答案: https : //stackoverflow.com/a/12428472/100848

weichsel原始示例的更新版本(謝謝!)。 Xcode發出了一些警告,其中之一是NSFileManager上不推薦使用的方法。 使用iOS 5.1中不推薦使用的方法在此處更新

    NSString *plistPath = nil;
NSFileManager *manager = [NSFileManager defaultManager];
if ((plistPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"mySpecial/PathTo.plist"])) 
{
    if ([manager isWritableFileAtPath:plistPath]) 
    {
        NSMutableDictionary *infoDict = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath];
        [infoDict setObject:@"foo object" forKey:@"fookey"];
        [infoDict writeToFile:plistPath atomically:NO];
        [manager setAttributes:[NSDictionary dictionaryWithObject:[NSDate date] forKey:NSFileModificationDate] ofItemAtPath:[[NSBundle mainBundle] bundlePath] error:nil];
    }
}

在構建應用程序時,它將創建一個可執行文件“ appName.app”,所有文件都構建在捆綁軟件中。 因此,在應用程序運行時,您無法訪問資源文件夾,因為所有數據都在捆綁包中(而不是在文件夾中)。

但是,您可以訪問包含應用程序某些信息的臨時文件夾。 您可以在此處找到臨時文件夾: 打開查找器-單擊用戶名(在PLACES下)-庫-應用程序支持-iPhone Simulator-用戶-應用程序-(您可以在其中找到所有臨時文件夾iPhone應用程序)

您可以通過以下方式訪問此臨時文件夾:


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

如果您將文件命名為save.plist,則可以按以下方式訪問它:

NSString *filePath = [documentsDirectory stringByAppendingString:@"_save.plist"];

然后,您只需將文件保存到此filePath,它將出現在名為“ Documents_save.plist”的臨時文件夾中。

*請注意,每次運行應用程序時,臨時文件夾的名稱都會有所不同。

為您推薦一本書:《開始iPhone開發-探索iPhone SDK》。 在第11章中,您可以找到所需的內容。

總結其他一些答案:

您的問題是您試圖將文件寫回到包含您的應用程序的文件夾中。 該文件夾在運行時不可寫。 您所做的一切都很好,您只需要選擇一個其他位置即可將文件寫入其中。

您可以使用NSSearchPathForDirectoriesInDomains函數為該數據找到更合適的文件夾。 (例如@“ Documents”文件夾。)

嘗試這個:

-(void)add:(NSRunningApplication *) app { 
   if ([self contains:app]) return; 
   [self.apps addObject:app.localizedName]; 
   [self.apps writeToFile:self.dataFile atomically:YES];
}

來自“可可編程”。

您必須將plist復制到文檔目錄中...因為您無法保存任何內容而不將其保存到文檔文件中....在復制時,它將允許在plist上進行寫入/修改

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM