NSMutableArray里没有保留价值

问题描述:

我定义类数据类有我有这个方法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

这段代码经过很多改变,所以为什么他根本不工作。 但仍不明白它是如何工作的回报。 谢谢 –

到这里看看:

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这个数组是正确的? –