2016-09-08 1 views
0

私は自分のサーバの応答を得てplistに保存しようとしましたが、plistファイルは作成されません。PlistがObjective Cで作成されていない

私は、ファイルのパスと辞書の内容を記録しているが、これらすべてのデータはまだ方法

-(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

参照 - http://stackoverflow.com/questions/12170871/how-to-add-a-key-value-pair-to-a-plist-file-in-objective-c and http:// stackoverflow .com/questions/23867337/how-can-i-save-retrieve-delete-update-my-data-in-plist-file-in-ios – Signare

+0

'[finalPath isEqualToString:filePath]'?メソッドの宣言/名前付け 'sideMenuFile:noTextHere:'は推奨されません。 – Larme

答えて

0

この次のコールます

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    [self sideMenu:@"ML"]; 

    return YES; 
} 

を作成しないplistの持っていますあなたのコードは、いくつかの最適でない仮定をしています。

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 WIHファイルを書き出すが、物事が戻ると、finalPathの一部としてtitleNameが存在しないので、ここには何も並んでいません。

3)

また、なぜあなたはbasePathは(それがnilかもしれないように見える)が有効であると仮定していますか?

関連する問題