2011-03-03 62 views
0

我使用plist文件中的值填充選取器。 plist的格式如下。iPhone plist將新字符串添加到數組

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 
<plist version="1.0"> 
<dict> 
    <key>type</key> 
    <array> 
     <string>AAAA</string> 
     <string>BBBBB</string> 
     <string>CCCCC</string> 
    </array> 
</dict> 
</plist> 

在項目調用這個字典/數組到Picker,這工作正常。

NSBundle *bundle = [NSBundle mainBundle]; 
NSString *plistPath = [bundle pathForResource:@"typecategory" ofType:@"plist"]; 
NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:plistPath]; 
self.typeCategorySettings = dictionary; 
[dictionary release]; 

NSArray *typeComponents = [self.typeCategorySettings allKeys]; 
NSArray *sorted = [typeComponents sortedArrayUsingSelector:@selector(compare:)]; 
self.typeCategory = sorted; 

NSString *selectedTypeCategory = [self.typeCategory objectAtIndex:0]; 
NSArray *array = [typeCategorySettings objectForKey:selectedTypeCategory]; 
self.typeValues = array; 

在如下

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component 
{ 
    return [self.typeValues count]; 

} 

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component 
{ 
    return [self.typeValues objectAtIndex:row]; 
} 

但是,調用self.typeValues此外,我已經添加了新的modalViewcontroller爲選擇器增加更多的價值。 隨着從的TextField.text上述方法的modalviewcontroller

NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 

NSString *path = [NSString stringWithFormat:@"%@/typecategory.plist", documentsDirectory]; 
NSLog(@"path: %@", path); 

NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:path]; 
//self.typeCategorySettings = dictionary; 
//[dictionary release]; 

NSArray *typeComponents = [dictionary allKeys]; 
NSArray *sorted = [typeComponents sortedArrayUsingSelector:@selector(compare:)]; 
//self.typeCategory = sorted; 

NSString *selectedTypeCategory = [sorted objectAtIndex:0]; 
NSMutableArray *array = [dictionary objectForKey:selectedTypeCategory]; 
NSString *plistt = textField.text; 
NSLog(@"path: %@", plistt); 
[array addObject:plistt]; 
[array writeToFile:path atomically:YES]; 

目的是一個單個陣列中添加的新行/串項「n」個。 like below

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 
<plist version="1.0"> 
<dict> 
    <key>type</key> 
    <array> 
     <string>AAAA</string> 
     <string>BBBBB</string> 
     <string>CCCCC</string> 
       <string>DDDDD</string> 

       <string>NNNNN</string> 
    </array> 
</dict> 
</plist> 

測試模擬器和設備。但是,將新的字符串添加到現有的數組中並未提交到plist文件。

控制檯報告是在這裏:

DataFile: file open error: /var/mobile/Library/Keyboard/en_GB-dynamic-text.dat, (Permission denied) 2011-03-03 22:49:05.028 TesApp[1562:307] path: /var/mobile/Applications/05074277-7E4D-4BC0-9CFA-XXXXXXXX/Documents/typecategory.plist 2011-03-03 22:49:05.031 TesApp[1562:307] path: DDDDD

任何建議來解決這個plist中添加字符串數組? ...以及拾取器的任何簡單解決方案。在運行中添加值的選項。來自Core Data或plist。

希望能有所幫助。

祝您好運 Devaski。

+0

我建議你參考這個鏈接:http://iphonesdevsdk.blogspot.com/2011/04/plist.html你可以在plist上找到完整的解決方案。 – Anand 2011-04-15 05:44:27

回答

3

解決了在運行時在上面的plist結構中插入新記錄的問題。

第1步:頭文件聲明。

NSMutableDictionary *typeCategorySettings; 
    NSArray *typeCategory; 
    NSMutableArray *typeValues; 

第2步:保存/寫入新的記錄從文本框plist文件..最後一行

NSError *error; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 

    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"typecategory.plist"]; 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 

if (![fileManager fileExistsAtPath:filePath]) //4 
{ 
    NSString *bundle = [[NSBundle mainBundle] pathForResource:@"typecategory" ofType:@"plist"]; //5 

    [fileManager copyItemAtPath:bundle toPath:filePath error:&error]; //6 
} 


    NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath]; 
    self.typeCategorySettings = dictionary; 
    NSLog(@"Dictionary Read%@", self.typeCategorySettings); 
    //[dictionary release]; 

    NSArray *typeComponents = [self.typeCategorySettings allKeys]; 
    NSMutableArray *sorted = [typeComponents sortedArrayUsingSelector:@selector(compare:)]; 
    self.typeCategory = sorted; 
    NSLog(@"Array Read%@", self.typeCategory); 

    NSString *selectedTypeCategory = [self.typeCategory objectAtIndex:0]; 
    NSMutableArray *array = [typeCategorySettings objectForKey:selectedTypeCategory]; 
    self.typeValues = array; 
    NSLog(@"Values Read%@", self.typeValues); 

      NSString *test; 
    test = textField.text; 
    [array insertObject:test atIndex:([self.typeValues count]-1)]; 
    [dictionary setObject:array forKey:@"type"]; 
    [dictionary writeToFile:filePath atomically:YES]; 
    NSLog(@"Written Read%@", dictionary);` 

第3步前一個記錄:閱讀從plist中數組。

NSError *error; 

NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 

NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"typecategory.plist"]; 
NSFileManager *fileManager = [NSFileManager defaultManager]; 

if (![fileManager fileExistsAtPath:plistPath]) //4 
{ 
    NSString *bundle = [[NSBundle mainBundle] pathForResource:@"typecategory" ofType:@"plist"]; //5 

    [fileManager copyItemAtPath:bundle toPath:plistPath error:&error]; //6 
} 

//NSBundle *bundle = [NSBundle mainBundle]; 
//NSString *plistPath = [bundle pathForResource:@"typecategory" ofType:@"plist"]; 
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath]; 
self.typeCategorySettings = dictionary; 

NSArray *typeComponents = [self.typeCategorySettings allKeys]; 
NSArray *sorted = [typeComponents sortedArrayUsingSelector:@selector(compare:)]; 
self.typeCategory = sorted; 

NSString *selectedTypeCategory = [self.typeCategory objectAtIndex:0]; 
NSMutableArray *array = [typeCategorySettings objectForKey:selectedTypeCategory]; 
self.typeValues = array;