0

我正在尝试从数据库中获取一个随机字符串我试图将数组转换为字符串但是当我单击按钮时我只得到地址谁能帮助我如何在我的按钮操作中显示该字符串?

- (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;


}
4

5 回答 5

3

[db randomQuote] 方法返回用户数组而不是字符串,您可以将其类型转换为用户对象,而不是从中获取所需的属性!

于 2012-12-26T11:10:13.093 回答
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];
   }
于 2012-12-26T11:06:06.430 回答
2

看起来您的数组是某个user类的对象数组而不是字符串数组。因此呼吁

[obj description]

正在返回一个对象而不是一个字符串。

您需要覆盖user类中的描述方法,并返回字符串。

于 2012-12-26T11:10:27.913 回答
1

此代码使问题:

for (NSObject * obj in arr)
{
    [show appendString:[obj description]];
}

因为您的实体类没有像description. 而不是这样写:

for (quoteDC * obj in arr)
{
    [show appendString:[obj quotes]];
}
于 2012-12-26T12:09:51.443 回答
0

而不是for (NSObject * obj in arr)使用存储在arr.

我猜这是一些 DBHandler 对象。所以使用

for (DBHandler * dbh in arr){
    [show appendString:[dbh thePropertyYouWantToAppend]];
}
于 2012-12-26T11:18:01.657 回答