I Create a class named DataClass there i have this method
+(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;
}
In my other class named UserSelect i create.
in h file
@interface UserSelect : UIViewController <UIPickerViewDelegate,UIPickerViewDataSource> {
NSMutableArray *List;
UIPickerView *myPicker;
}
@property (nonatomic,retain) NSMutableArray *List;
@property (nonatomic,retain) UIPickerView *myPicker;
@end
and m file
- (void)viewDidLoad {
[super viewDidLoad];
//List=[[NSMutableArray alloc] init;
NSString *sql;
//Loading Data
if ([self.title isEqualToString:@"Marca Teste"])
{
sql=@"SELECT TESTMARK FROM TBL_TESTSMARK ORDER BY TESTMARK";
}
else
{
sql=@"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];
}
Ok, i call this method and this return is a expected.
But in the UiPickerView methods my List variable only retain the first object in the NSMutable array.
These are the UIPicker methods
#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];
}
I tried to boot List in many ways, what`s wrong?
Take a look here:
The string txt is autoreleased (
NSString stringXXXfunctions return an autoreleased string). Then you add it to thetmpListand explicitly release it. As soon as the autorelease pool is drained, the string will be released. In other words, you over-release the string. Removeand the values should be retained by
tmpList. Since tmpList is autoreleased too, it is not sure if this matters. It could give you a runtime error.This is also suspicious:
The first alloc-init is totally unnecessary. If the property was synthesized properly, it will not produce a leak, but it doesn’t make any sense. Remove the first of these two lines.
Also, your
returnList:method returns a string, but you use it as array in the second line I quoted above.All in all, there are quite a few logical errors in your code. You might want to run it through the debugger and the analyzer (menu Product – Analyze).