2013-07-11 2 views
0
- (void)viewDidLoad 
{ 
NSError *error; 
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES); 
NSString *documentDirectory = [path objectAtIndex:0]; 
NSString *path1 = [documentDirectory stringByAppendingPathComponent:@"data.plist"]; 
NSFileManager *filemanager = [NSFileManager defaultManager]; 
NSLog(@"%@" ,documentDirectory) ; 
if(![filemanager fileExistsAtPath:path1]) 
{ 
    NSString *bundle = [[NSBundle mainBundle]pathForResource:@"data" ofType:@"plist"]; 
    [filemanager copyItemAtPath:bundle toPath:path1 error:&error]; 
} 


NSMutableDictionary *dictMioDB = [[NSMutableDictionary alloc] initWithContentsOfFile:documentDirectory] ; 

NSMutableArray *arryRandomNumber =[[NSMutableArray alloc]init]; 
while (arryRandomNumber.count<10) { 
    NSInteger randomNumber=1+arc4random()%10; 
    if (![arryRandomNumber containsObject:[NSString stringWithFormat:@"%d",randomNumber]])  { 
     [arryRandomNumber addObject:[NSString stringWithFormat:@"%d",randomNumber]]; 
     [dictMioDB setValue: arryRandomNumber forKey:?????????]; 
    } 
    continue; 
} 

[dictMioDB writeToFile:path1 atomically:YES]; 
NSLog(@"%@ - %@",arryRandomNumber,[arryRandomNumber objectAtIndex:2]); 

//[data writeToFile: path atomically:YES]; 
[super viewDidLoad]; 
// Do any additional setup after loading the view, typically from a nib. 
} 

Я новичок в Objective C и я пытаюсь написать массив элементов в мой PLISTкак написать массив элементов для PLIST

+0

Что произошло, когда вы используете это? –

ответ

0

Если вы используете массив, то используйте:

[urArray writeToFile:pListFilePath atomically:YES]; 

Если для того, чтобы использовать словарь:

[urDictionary writeToFile:(NSString *)filePath atomically:YES]; 
1

Эта линия:

NSMutableDictionary *dictMioDB = [[NSMutableDictionary alloc] initWithContentsOfFile:documentDirectory] ; 

Проблема связана с тем, что вы пытаетесь загрузить полный каталог, а не файл plist.

Итак, когда вы пришли к этой линии:

[dictMioDB setValue: arryRandomNumber forKey:?????????]; 

Вы пытаетесь добавить значения в словарь, который не существует. И тогда, когда вы пытаетесь его сохранить, ничего не нужно экономить.

1

Попробуйте, как это

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:@"username.plist"]; 


    NSString *x = @"xxx"; 
    NSString *y = @"yyy"; 
    NSString *z = @"zzz"; 

    //Load the original file 
    NSMutableArray *arr; 
    if([[NSFileManager defaultManager] fileExistsAtPath:plistPath]) 
     //File exist load 
     arr = [[NSMutableArray alloc] initWithContentsOfFile:plistPath]; 
    else 
     //File does not exist create 
     arr = [[NSMutableArray alloc] init]; 

    //Create dictionary 
    NSDictionary *plistDict = [NSDictionary dictionaryWithObjects: [NSArray arrayWithObjects: x,y,z,nil] forKeys:[NSArray arrayWithObjects: @"object1",@"object2",@"object3", nil]]; 

    //Append to arr 
    [arr removeAllObjects]; 
    [arr addObject:plistDict]; 

    //Save to file 
    [arr writeToFile:plistPath atomically:YES]; 

Надеется, что это будет поможет вам ...

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