2011-08-19 48 views
1

我有一個的UITextField,當用戶點擊保存按鈕,文本應保存與下面的代碼plist文件:讀取值

-(IBAction)saveButtonPressed 
{ 
     NSError *error; 
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //1 
     NSString *documentsDirectory = [paths objectAtIndex:0]; //2 
     NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Data.plist"]; //3 

     NSFileManager *fileManager = [NSFileManager defaultManager]; 

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

      [fileManager copyItemAtPath:bundle toPath: path error:&error]; //6 
     } 
     NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile: path]; 
     //here add elements to data file and write data to file 
     [data setObject:[inkTextField text] forKey:@"inkText"]; 

     BOOL didWrite=[data writeToFile: path atomically:YES]; 
     if(didWrite==YES) 
      NSLog(@"didWrite"); 
     else NSLog(@"nope"); 
     [data release]; 
    } 

然後,我有一個UITableView和我要plist中的字符串值裝入電池文本字段用下面的代碼:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellViewController"]; 
    if (cell == nil) { 
     // Load the top-level objects from the custom cell XIB. 
     NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CellViewController" owner:self options:nil]; 
     // Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain). 
     cell = [topLevelObjects objectAtIndex:0]; 
    } 
    NSError *error; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //1 
    NSString *documentsDirectory = [paths objectAtIndex:0]; //2 
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Data.plist"]; //3 

    NSFileManager *fileManager = [NSFileManager defaultManager]; 

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

     [fileManager copyItemAtPath:bundle toPath: path error:&error]; //6 
    } 
    NSMutableDictionary *savedInks = [[NSMutableDictionary alloc] initWithContentsOfFile: path]; 

    //load from savedStock example int value 
    NSString *value; 
    value = [[savedInks objectForKey:@"inkText"]stringValue]; 
    [savedInks release]; 
    inkTitle.text=value; 
    return cell; 
} 

當我保存,我得到一個日誌輸出didWrite,所以我知道它正確保存。然而,當我參觀的tableView,我得到一個崩潰並出現以下錯誤:

2011-08-19 13:56:45.717 MyApp[36067:b303] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString stringValue]: unrecognized selector sent to instance 0x4bb5fa0' 

所以我認爲這是事做這一行:

value = [[savedInks objectForKey:@"inkText"]stringValue]; 

所以我試圖

value = [savedInks objectForKey:@"inkText"]; 

但這也導致崩潰,但沒有消息。我究竟做錯了什麼?

回答

2

您應該刪除'stringValue',因爲您已經返回了NSString值。

在下一行中,您將保留'value'之前釋放'savedInks'。將版本與下面的線交換,看看是否有效。:

NSString *value; 
value = [savedInks objectForKey:@"inkText"]; 
inkTitle.text=value; 
[savedInks release]; 

當savedInks被釋放時,字典中的所有對象也被釋放。設置inkTtile.text應自動執行保留值。

+0

謝謝你這個工作!但是現在'價值'沒有保存已保存在InkInks中的值,因此我可以在不影響'value'值的情況下釋放它。 – Snowman

+0

否 - 你沒有保留'value',所以當字典發佈時它會被釋放。記住'value'只是對包含實際字符串的內存中某個地方的引用。一旦釋放計數達到零,內存就被釋放,指針無效。 –

+0

不,值是指向(也)從SavedInks引用的字符串的指針。 SavedInks被釋放,數組中的所有項目也被釋放。您需要保留該字符串,或將其從Dictionary中刪除(並保留)。 – Flyingdiver