Plist 没有在 Objective C 中创建

Posted

技术标签:

【中文标题】Plist 没有在 Objective C 中创建【英文标题】:Plist is not creating in Objective C 【发布时间】:2016-09-08 07:22:04 【问题描述】:

当我收到服务器响应并尝试将其保存在 plist 中时,但 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;
            
            )
            
            

【问题讨论】:

参考 - ***.com/questions/12170871/… 和 ***.com/questions/23867337/… [finalPath isEqualToString:filePath]?显然不建议声明/命名您的方法 sideMenuFile:noTextHere: 【参考方案1】:

您的代码做了许多非最佳假设。

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 是有效的(看起来可能是 nil)?

【讨论】:

以上是关于Plist 没有在 Objective C 中创建的主要内容,如果未能解决你的问题,请参考以下文章

在 Objective C 中创建 JSON

在 Objective C for iPhone 中创建 zip 文件

在Objective C中创建JSON数组[重复]

如何在类属性的Objective C中创建类似json的结构?

如何在 xcode 4.3.2 中创建 plist 文件

如何在 Xcode 中创建 Entitlement.plist 文件?