当我得到我的服务器响应并尝试将其保存在plist中时,但是plist文件没有创建。
我已经记录了文件路径和字典内容,但所有这些数据仍然没有创建
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self sideMenu:@"ML"];
return YES;
}
这将调用以下方法
-(void)sideMenu:(NSString *)title{
NSString *myRequestString = [[NSString alloc] initWithFormat:@"data=%@&deviceid=e8ef8c98262185ec",title];
NSData *myRequestData = [NSData dataWithBytes:[myRequestString UTF8String ] length: [ myRequestString length ] ];
NSMutableURLRequest *request = [ [ NSMutableURLRequest alloc ] initWithURL: [ NSURL URLWithString:SIDE_MENU]];
[request setHTTPMethod:@"POST"];
[request setHTTPBody: myRequestData];
NSURLResponse *response;
NSError *err;
NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse:&response error:&err];
NSString* responseString = [[NSString alloc] initWithData:returnData encoding:NSNonLossyASCIIStringEncoding];
NSArray *myArray = [responseString componentsSeparatedByString:@"<!DOC"];
//NSLog(@"%@",myArray);
if (myArray != nil || [myArray count] > 0) {
NSData *data = [[[myArray objectAtIndex:0]stringByReplacingOccurrencesOfString:@"\n" withString:@""] dataUsingEncoding:NSUTF8StringEncoding];
id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
[self sideMenuFile:title :json];
}
}
-(NSString *)sideMenuFile:(NSString *)titleName :(NSDictionary *)dict{
NSString *filePath;
MyManager *sharedManager =[MyManager sharedManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
if([titleName isEqualToString:@"ML"])
{ sharedManager.sideMenu = [[NSDictionary alloc]init];
sharedManager.sideMenu = dict;
filePath = [documentsDirectory stringByAppendingPathComponent:@"ML.plist"];
}else if ([titleName isEqualToString:@"HM"]){
filePath = [documentsDirectory stringByAppendingPathComponent:@"HM.plist"];
}else if ([titleName isEqualToString:@"PDC"]){
filePath = [documentsDirectory stringByAppendingPathComponent:@"PDC.plist"];
}else if ([titleName isEqualToString:@"SM"]){
filePath = [documentsDirectory stringByAppendingPathComponent:@"SM.plist"];
}else if ([titleName isEqualToString:@"GL"]){
filePath = [documentsDirectory stringByAppendingPathComponent:@"GL.plist"];
}else if ([titleName isEqualToString:@"CU"]){
filePath = [documentsDirectory stringByAppendingPathComponent:@"CU.plist"];
}else if ([titleName isEqualToString:@"BR"]){
filePath = [documentsDirectory stringByAppendingPathComponent:@"GL.plist"];
}
NSLog(@"%@",filePath); //printing path
[dict writeToFile:filePath atomically:YES];
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
NSString *plistName = @"GL";
NSString *finalPath = [basePath stringByAppendingPathComponent:
[NSString stringWithFormat: @"%@.plist", plistName]];
NSDictionary *dictww=[[NSDictionary alloc]initWithContentsOfFile:finalPath];
NSLog(@"dict%@",dictww); //printing nill
return filePath;
}
JSON文件结构
{
sigallery = {
rows = (
{
active = True;
gallerytext = "<null>";
galleryurl = "assets/img/content-images/1.jpg";
moddt = "2016-07-19T12:28:18.873";
onclickurl = "testd.in";
settingsid = 1;
showfromdt = "1901-01-01T00:00:00.0";
showsequence = 1;
showuptodt = "2099-12-31T00:00:00.0";
videoimagebanneraudioswitch = I;
},
{
active = True;
gallerytext = "<null>";
galleryurl = "assets/img/content-images/2.jpg";
moddt = "2016-07-19T12:28:18.873";
onclickurl = "testd.in";
settingsid = 1;
showfromdt = "1901-01-01T00:00:00.0";
showsequence = 2;
showuptodt = "2099-12-31T00:00:00.0";
videoimagebanneraudioswitch = I;
}
)
}
}
答案 0 :(得分:0)
您的代码会做出一些非最佳假设。
1)
无论何时写出文件,都应该检查是否存在任何错误情况。无论文件是否已写出,NSDictionary的writeTo...
方法都会返回布尔值,所以你可以这样做:
NSLog(@"%@",filePath); //printing path
BOOL success = [dict writeToFile:filePath atomically:YES];
if (success == false)
{
NSLog(@"did not successfully write dictionary to path %@", filePath);
} else {
// ... do everything else in here
}
2)
您使用titleName
+ GL | PDC | SM.plist
格式写出文件,但当您尝试重新阅读时,titleName
中没有finalPath
,所以没有什么排队在这里。
3)
另外,为什么假设basePath
有效(看起来可能是零)?