我试图从数据库中获取一个随机字符串我试图在字符串中转换数组但是当我点击按钮我只得到地址时,任何人都可以帮助我如何在我的按钮操作中显示该字符串吗?
- (IBAction)randBtn:(id)sender {
SendQuoteVC *svc= [[SendQuoteVC alloc]initWithNibName:@"SendQuoteVC" bundle:nil];
DBHandler *db =[[DBHandler alloc]init];
arr =[db randomQuote];
NSMutableString * show = [[NSMutableString alloc] init];
for (NSObject * obj in arr)
{
[show appendString:[obj description]];
}
NSLog(@"The concatenated string is %@", show);
NSString *rs =[NSString stringWithFormat:@" %@",show];
svc.quote=rs;
[self.navigationController pushViewController:svc animated:YES];
}
我的日志消息是
2012-12-26 17:02:46.062 SendQuote[2281:c07] QUERY: SELECT * FROM quotes ORDER BY RANDOM() LIMIT 1
2012-12-26 17:02:46.097 SendQuote[2281:c07] txtQuote is (
"<quoteDC: 0xa9a94c0>"
)
2012-12-26 17:02:46.100 SendQuote[2281:c07] rand is (
"<quoteDC: 0xa9a94c0>"
)
我已将我的用户实体类更改为quoteDC类,而我的Db函数是
-(NSMutableArray *)randomQuote{
NSMutableArray *dataArray =[[NSMutableArray alloc]init];
NSString * sqlStr=[NSString stringWithFormat:@"SELECT * FROM quotes ORDER BY RANDOM() LIMIT 1"];
sqlite3_stmt *ReturnStatement = (sqlite3_stmt *) [self getStatement: sqlStr];
while (sqlite3_step(ReturnStatement)==SQLITE_ROW)
{
@try
{
quoteDC *worddc = [[quoteDC alloc] init];
NSString *userid = [NSString stringWithUTF8String:(char *)sqlite3_column_text(ReturnStatement, 0)];
NSString *name = [NSString stringWithUTF8String:(char *)sqlite3_column_text(ReturnStatement, 1)];
worddc.quote_id=[userid integerValue];
worddc.quotes=name;
[dataArray addObject:worddc];
}
@catch (NSException *ept) {
NSLog(@"Exception in %s, Reason: %@", __PRETTY_FUNCTION__, [ept reason]);
}
}
return dataArray;
}
答案 0 :(得分:3)
答案 1 :(得分:2)
尝试这个我觉得它适用于你...
- (IBAction)randBtn:(id)sender {
SendQuoteVC *svc= [[SendQuoteVC alloc]initWithNibName:@"SendQuoteVC" bundle:nil];
DBHandler *db =[[DBHandler alloc]init];
arr =[db randomQuote];
NSString *rs =(NSString *)arr;
svc.quote=rs;
[self.navigationController pushViewController:svc animated:YES];
}
答案 2 :(得分:2)
看起来你的数组是一些user
类的对象数组而不是字符串数组。
因此呼吁
[obj description]
返回一个对象而不是一个字符串。
您需要覆盖user
类中的description方法,并返回字符串。
答案 3 :(得分:1)
此代码出现问题:
for (NSObject * obj in arr)
{
[show appendString:[obj description]];
}
因为您的实体类没有description
之类的属性。
而不是写这个:
for (quoteDC * obj in arr)
{
[show appendString:[obj quotes]];
}
答案 4 :(得分:0)
而不是for (NSObject * obj in arr)
使用arr
中存储的对象。
我猜这是一些DBHandler对象。所以使用
for (DBHandler * dbh in arr){
[show appendString:[dbh thePropertyYouWantToAppend]];
}