2011-08-06 46 views
0

我定義類數據類有我有這個方法NSMutableArray裏沒有保留價值

+(NSMutableArray*) returnList :(NSString *) sql 
{ 

    NSMutableArray *tmpList=[[[NSMutableArray alloc]init]autorelease]; 

    sqlite3 *db; 

    NSString *dbPath =[[NSBundle mainBundle] pathForResource:@"DB_MyReef" ofType:@"sqlite"]; 


    if (sqlite3_open([dbPath UTF8String],&db) ==SQLITE_OK) 
    { 

     sqlite3_stmt *ss; 

     int rv = sqlite3_prepare_v2(db, [sql UTF8String], -1,&ss,NULL); 

     if (rv==SQLITE_OK) 
     { 

      while (sqlite3_step(ss)==SQLITE_ROW) 
      { 

       NSString *txt =[NSString stringWithUTF8String:(char *)sqlite3_column_text(ss,0)]; 
       [tmpList addObject:txt]; 
       [txt release]; 

      } 
     } 

     sqlite3_finalize(ss); 
    } 

    sqlite3_close(db); 


    return tmpList; 


} 

在我命名UserSelect我創建其他類。

以h文件

@interface UserSelect : UIViewController <UIPickerViewDelegate,UIPickerViewDataSource> { 

    NSMutableArray *List; 
    UIPickerView *myPicker; 

} 

@property (nonatomic,retain) NSMutableArray *List; 
@property (nonatomic,retain) UIPickerView *myPicker; 


@end 

且m文件

- (void)viewDidLoad { 



    [super viewDidLoad]; 

    //List=[[NSMutableArray alloc] init; 

    NSString *sql; 
     //Loading Data 
     if ([self.title isEqualToString:@"Marca Teste"]) 
     { 
      [email protected]"SELECT TESTMARK FROM TBL_TESTSMARK ORDER BY TESTMARK"; 
     } 
     else 
     { 
      [email protected]"SELECT PT_PARAMNAME FROM TBL_PARAMETERS " 
      "WHERE COD IN " 
      "(SELECT A.CODTEST FROM TBL_TEST AS A " 
      "INNER JOIN " 
      "TBL_TESTSMARK AS B " 
      "ON " 
      "A.CODTESTMARK =B.COD " 
      "WHERE " 
      "B.TESTMARK='Salifert')"; 
     } 

    self.List=[[NSMutableArray alloc] init]; 
    self.List=[[NSMutableArray arrayWithArray:[DataClass returnList:sql]]retain]; 

    myPicker=[[UIPickerView alloc] init]; 
    myPicker.delegate=self; 
    myPicker.dataSource=self; 

     /*for (NSString *ts in List) { 
      NSLog(@"%@",ts); 
     }*/ 

     [self.view addSubview:myPicker];  


    } 

好,我調用此方法,並預期一個該返回。

但在UiPickerView方法中,我的List變量只保留NSMutable數組中的第一個對象。

這些都是UIPicker方法

#pragma mark pickerView 

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView 
{ 
    NSLog(@"%@",@"numberOfComponentsInPickerView"); 
    return 1; 
} 

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component 
{ 
    NSLog(@"%@",@"numberOfRowsInComponent"); 

    return [List count]; 
} 


-(NSString *)pickerView:(UIPickerView *) pickerView titleForRow:(NSInteger) row forComponent:(NSInteger) component 
{ 
    NSString *txt=[[NSString alloc] initWithFormat:@"%@",[self.List objectAtIndex:(int)row]]; 
    return txt; 
    [txt release]; 
} 

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component 
{ 
    NSString *txt=[[NSString alloc] initWithFormat:@"%@",[self.List objectAtIndex:(int)row]]; 

    [DataClass writePlist:txt toKey:TEST_MARK]; 

    [txt release]; 


} 

我試圖引導列表的方法很多,什麼`錯了嗎?

+0

您從rudy中得到了一個很好的答案,我只想添加下面這行是不必要的:'self.List = [[NSMutableArray alloc] init];'因爲您將該屬性設置爲下一行的其他內容。 –

+0

超過不必要;它會泄漏創建的數組。/cc @fichek –

+0

這段代碼經過很多改變,所以爲什麼他根本不工作。 但仍不明白它是如何工作的回報。 謝謝 –

回答

1

到這裏看看:

NSString *txt =[NSString stringWithUTF8String:(char *)sqlite3_column_text(ss,0)]; 
[tmpList addObject:txt]; 
[txt release]; 

字符串TXT會被自動釋放(NSString stringXXX函數返回一個autoreleased字符串)。然後將其添加到tmpList並明確釋放它。一旦autorelease池被排空,字符串將被釋放。換句話說,你過度釋放字符串。刪除

[txt release]; 

和值應保留tmpList。由於tmpList也是autoreleased,它不確定這是否重要。它可能會給你一個運行時錯誤。

這也是可疑:

self.List=[[NSMutableArray alloc] init]; 
self.List=[[NSMutableArray arrayWithArray:[DataClass returnList:sql]]retain]; 

第一的alloc-INIT是完全不必要的。如果財產被合理地合成,它不會產生泄漏,但它沒有任何意義。刪除這兩行中的第一行。

此外,您的returnList:方法將返回一個字符串,但您將其用作上面引用的第二行中的數組。

總而言之,您的代碼中存在不少邏輯錯誤。您可能想要通過調試器和分析器(菜單Product - Analyze)運行它。

+0

我試過也不行! 在viewDidLoad中列出的數組是正確的,具有所有的值,在註釋中顯示的結果是,問題出在UIPickerView方法 –

+0

魯迪非常感謝!在目標中! 多一個懷疑爲什麼在viewDidLoad這個數組是正確的? –