繁体   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