2015-04-06 3 views
0

Я пишу данные для plist с помощью alwys возвращает мне значение nill. Мне также нужно добавить данные к предыдущим значениям массива. Но Значения alwys нуль .. Ниже приведен код:Написание/добавление данных в Plist возвращает nill

NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); 
// get documents path 
NSString *documentsPath = [paths objectAtIndex:0]; 
// get the path to our Data/plist file 
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"a.plist"]; 

// set the variables to the values in the text fields 


// create dictionary with values in UITextFields 
NSDictionary *plistDict = [NSDictionary dictionaryWithObject:@"c" forKey:@"Id"]; 


if ([[NSFileManager defaultManager] fileExistsAtPath:plistPath]) 
{ 
    array = [[NSMutableArray alloc] initWithContentsOfFile:plistPath]; 
    [array addObject:plistDict]; 

    [array writeToFile:plistPath atomically:YES]; 


      NSLog(@"array%@",array); 

} 
else 
{ 
    NSArray *array = [NSArray arrayWithObject:plistPath]; 
    [array writeToFile:plistPath atomically:YES]; 
} 
+0

Ваш массив всегда равен нулю, даже в первый раз? Или где он возвращает nil в блоке 'if' или' else'? –

+0

alwys returm в условии IF – user3226440

ответ

0

Если я правильно понимаю ваш вопрос, я проверил код только с одним небольшим изменением, т.е. Добавлено объявлением массива, и это работает прекрасно. Пожалуйста, простите меня, если это не проблема.

NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); 
    // get documents path 
    NSString *documentsPath = [paths objectAtIndex:0]; 
    // get the path to our Data/plist file 
    NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"a.plist"]; 

    // set the variables to the values in the text fields 
    NSMutableArray *array; 

    // create dictionary with values in UITextFields 
    NSDictionary *plistDict = [NSDictionary dictionaryWithObject:@"c" forKey:@"Id"]; 


    if ([[NSFileManager defaultManager] fileExistsAtPath:plistPath]) 
    { 
     array = [[NSMutableArray alloc] initWithContentsOfFile:plistPath]; 
     [array addObject:plistDict]; 

     [array writeToFile:plistPath atomically:YES]; 


     NSLog(@"array%@",array); 

    } 
    else 
    { 
     NSArray *array = [NSArray arrayWithObject:plistPath]; 
     [array writeToFile:plistPath atomically:YES]; 
    } 
+0

Имеет ли значение ... объявление массива в методе ... или определение глобально в .h – user3226440

Смежные вопросы