开发者

Download a plist from URL and parse it to a NSMutableDictionary?

HI guys ~

this is how I download a plist from URL

NSURLRequest *theRequest=[NSURLRequest requestWithURL:setDefaultPwdAndSnURL                                              cachePolicy:NSURLRequestUseProtocolCachePolicy
                         timeoutInterval:60.0];
NSData    *returnData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:nil error:nil];
NSString *listFile = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

[listFile dataUsingEncoding:NSUTF8StringEncoding];
NSArray *plistData = [listFile propertyList];

and this is the result I got

plistdata is(
        {
        sn = 1;
        pwd = 123;
    }
)

I want to save it to a NSMutableDictionary

NSArray *paths      = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
 NSString *docPath    = [paths objectAtIndex:0];
 NSString *filePath   = [docPath stringByAppendingPathComponent:@"DevicePassword.plist"];
 NSMutableDictionary *dictread  = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];

 NSMutableDictionary *device_serialnumber = [NSMutableDictionary dictionaryWithObject:[[plistData objectAtIndex:0]valueForKey:@"sn"]
                      forKey:@"serial_number"];

 NSMutableDictionary *device_password = [NSMutableDictionary dictionaryWithObject:[[plistData objectAtIndex:0]  valueForKey:@"pwd"]
                   forKey:@"password"];
 [dictread addEntriesFromDictionary:device_serialnumber];
 [dictread addEntriesFromDictionary:device_password];               
 [dictread writeToFile:filePath atomically:YES];

but I Log(@"dictread is :%@",dictread);

got dictread : (NULL)

.......SO I PARESE A NULL THING TO dictread ????

Please help me to figure out this problem..开发者_运维技巧.. @_@

Many Great Thanks ~

NOTE:

I change some init value and Log it to check

NSMutableDictionary *device_serialnumber = [NSMutableDictionary dictionaryWithObject:@"123"
                      forKey:@"serial_number"];

 NSMutableDictionary *device_password = [NSMutableDictionary dictionaryWithObject:@"456"
                   forKey:@"password"];

I got two dictionary "device_serialnumber" and "device_password"

But "dictread" still (NULL)

Did I give a wrong function to add object to it ????


dictread should not be initialized with initWithContentsOfFile: because the file does not yet exist. This will return nil for dictread if the file has an error (can't read it as a dictionary) or the file is not there. Try using this initializer instead.

NSMutableDictionary *dictread  = [[NSMutableDictionary alloc] initWithCapacity:[plistdata count]];


You could also just say:

[[plistData objectAtIndex:0] writeToFile:filePath atomically:YES];

This would prevent you from changing the names of the keys, though.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜