Reputation: 2022
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 ????
Upvotes: 0
Views: 1375
Reputation: 2289
You could also just say:
[[plistData objectAtIndex:0] writeToFile:filePath atomically:YES];
This would prevent you from changing the names of the keys, though.
Upvotes: 1
Reputation: 4285
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]];
Upvotes: 2