Objective c 如何在另一个方法中读取一个方法中的值
下面是我正在使用的代码Objective c 如何在另一个方法中读取一个方法中的值,objective-c,sqlite,Objective C,Sqlite,下面是我正在使用的代码 - (void) readDataFromDatabase{ sqlite3 *database; persons = [[NSMutableArray alloc]init]; if (sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK){ const char *sqlStatement = "select * from sdata";
- (void) readDataFromDatabase{
sqlite3 *database;
persons = [[NSMutableArray alloc]init];
if (sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK){
const char *sqlStatement = "select * from sdata";
sqlite3_stmt *compiledStatement;
if (sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK){
while (sqlite3_step(compiledStatement) == SQLITE_ROW) {
pID = sqlite3_column_int(compiledStatement, 0);
pName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
pAdd = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];
pPh = sqlite3_column_int(compiledStatement, 3);
pCp = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 4)];
pLat = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 5)];
pLong = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 6)];
Person *temp = [[Person alloc]initWithID:pID name:pName address:pAdd phone:pPh contactperson:pCp fLat:pLat fLong:pLong];
[persons addObject:temp];
NSLog(@"Inside Persons : %@",persons);
[temp release];
}
}
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
}
For the NSLog in NSLog(@"Inside Persons : %@",persons);i get the output as Inside Persons : ("<Person: 0x4c26ae0>"
- (void)startTest
{
for(int j=0;j<[persons count];j++){
Person *arr = [persons objectAtIndex:j];
NSString *s = [[NSString alloc]initWithFormat:@"%f",arr.fLat] ;
NSLog(@" data in array : %@", s);
}
#define COORDS_COUNT 1
#define RADIUS 100.0f
CLLocationCoordinate2D coords[COORDS_COUNT] = {
CLLocationCoordinate2DMake([arr.fLat floatValue], [arr.fLong floatValue ]),
};
CLLocationCoordinate2D center = CLLocationCoordinate2DMake([lat floatValue], [longt floatValue]);
CLRegion *region = [[CLRegion alloc] initCircularRegionWithCenter:center radius:RADIUS identifier:@"Banashankari"];
for (int i = 0; i < COORDS_COUNT; i++)
{
CLLocationCoordinate2D coord = coords[i];
NSString *coordString = [NSString stringWithFormat:@"%f,%f",coord.latitude, coord.longitude];
[dataArray addObject:coordString];
if ([region containsCoordinate:coord])
{
NSLog(@"location %f, %f is within %.0f meters of coord %f, %f", coord.latitude, coord.longitude, RADIUS, center.latitude, center.longitude);
[resultArray addObject:coordString];
NSLog(@"data in resultArray %@",resultArray);
}
else
{
NSLog(@"location %f, %f is not within %.0f meters of coord %f, %f", coord.latitude, coord.longitude, RADIUS, center.latitude, center.longitude);
}
}
}
-(void)从数据库读取数据{
sqlite3*数据库;
persons=[[NSMutableArray alloc]init];
if(sqlite3_打开([databasePath UTF8String],&database)==SQLITE_正常){
const char*sqlStatement=“select*from sdata”;
sqlite3_stmt*编译语句;
if(sqlite3\u prepare\u v2(数据库,sqlStatement,-1,&compiledStatement,NULL)==SQLITE\u OK){
while(sqlite3_步骤(compiledStatement)==SQLITE_行){
pID=sqlite3\u column\u int(compiledStatement,0);
pName=[NSString stringWithUTF8String:(char*)sqlite3\u column\u text(compiledStatement,1)];
pAdd=[nsstringwithutf8string:(char*)sqlite3\u column\u text(compiledStatement,2)];
pPh=sqlite3\u column\u int(compiledStatement,3);
pCp=[NSString stringWithUTF8String:(char*)sqlite3_column_text(compiledStatement,4)];
pLat=[nsstringwithutf8string:(char*)sqlite3\u column\u text(compiledStatement,5)];
pLong=[nsstringwithutf8string:(char*)sqlite3\u column\u text(compiledStatement,6)];
Person*temp=[[Person alloc]initWithID:pID name:pName地址:pAdd电话:pPh联系人:pCp平面:pLat fLong:pLong];
[人员添加对象:临时];
NSLog(@“内部人员:%@”,人员);
[临时释放];
}
}
sqlite3_最终确定(编译语句);
}
sqlite3_关闭(数据库);
}
对于NSLog中的NSLog(@“内部人员:%@”,人员);我以内部人员的身份获取输出:(“”
-(无效)开始测试
{
对于(int j=0;j为nsmutablearray
人员使用@property(非原子,retain)?还尝试检查是否在数组中获取值。在数组填充值后使用retain。我已为nsmutablearray人员使用@property(非原子,retain)。之后如何保留可变数组[persons addObject:temp];还尝试检查是否在数组中获取值??我还尝试在[persons addObject:temp]之后保留可变数组;但未获取值persons的输出。使用NSLog并检查。persons输出在persons:(“”)内。