English 中文(简体)
内容提要
原标题:iOS:Read and Write into Plist

I want to save the token from from the server into a plist. I am not sure If I have to create a plist firs or it can automatically get created with the following code in my Document directroy. However, I am not able to create a plist and write my dictionary into it. Here is my code

-(void)writeToPlist:(NSString*)value  forkey:(NSString *)key

{
    NSLog(@"Write plist here");

    //NSError *error;

    NSArray *paths=NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *documentDirectory=[paths objectAtIndex:0];

    NSString* path=[documentDirectory stringByAppendingFormat:@"Util.plist"];

    NSLog(@"The path is %@",path);

    NSFileManager *fileManager=[NSFileManager defaultManager];

    NSMutableDictionary *data;

    if(![fileManager fileExistsAtPath:path])
    {
        path=[[NSBundle mainBundle]pathForResource:@"Util" ofType:@"plist"];
    }


    [data setObject:value forKey:key];

    [data writeToFile:path atomically:YES];//will it create the plist?


}   
问题回答

为什么不要求你利用NSUserDefault来挽救它?

一、导 言

NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
if([standardUserDefaults objectForKey:@"your-key-goes-here"] == nil) //this means you don t have that key
{[standardUserDefaults setValue:@"your-value-goes-here" forKey:@"your-key-goes-here"];}
[standardUserDefaults synchronize];  

最后,不要忘记同步。

以及当需要数据时,你需要打上<条码>,可读书>:tForKey。

希望这一帮助。

我找到了一种办法,可以方便地从方案角度编制清单,这对我来说是这样:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingString:@"/myFile.plist"];
if (![[NSFileManager defaultManager] fileExistsAtPath:path]){
   NSDictionary *emptyDic = [NSDictionary dictionary];
   [emptyDic writeToFile:path atomically:YES];
}

改变发言内容,并将工作。





相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签