亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在plist中寫入數據?

如何在plist中寫入數據?

iOS
明月笑刀無情 2019-11-02 10:14:46
我已經在資源文件夾中創建了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中。我是這個概念的新手。因此,如果有人知道,請幫助我。提前致謝。:-)
查看完整描述

3 回答

?
慕神8447489

TA貢獻1780條經驗 獲得超1個贊

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];

    }

}


查看完整回答
反對 回復 2019-11-02
  • 3 回答
  • 0 關注
  • 484 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號