Search code examples
iosios5ios6fmdb

error fmdb set label.text


I am using the below code to create a table.I get this error [__NSCFNumber isEqualToString:]: unrecognized selector sent to instance at
label.text = rs[colNum++];

How Do I fix it? Thanks in Advance.

NSInteger rowNum = 0;
NSArray *columnWidths = @[@(50), @(120), @(75), @(75)];
CGFloat rowHeight = 24.0;

FMResultSet *rs = [db executeQuery:sql];

while ([rs next])
{
    CGFloat x = 0.0;
    NSInteger colNum = 0;
    for (NSNumber *columnWidth in columnWidths)
    {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(x,
                                                                    rowHeight * rowNum, 
                                                                    [columnWidth floatValue], 
                                                                    rowHeight)];
        label.textAlignment = NSTextAlignmentCenter;
        label.layer.borderColor = [UIColor blackColor].CGColor;
        label.layer.borderWidth = 1.0;
        label.text = rs[colNum++];
        [self.view addSubview:label];
        x += [columnWidth floatValue];
    }
    rowNum++;
}

[rs close];

Solution

  • The problem is that some of your columns in your result set are not text values. As I recall from your other question from which this stems, one of your columns is the rowid, you have some text columns, and then you have some numeric columns. So instead of:

    label.text = rs[colNum++];
    

    You should probably have something like:

    id value = rs[colNum];
    if ([[rs columnNameForIndex:colNum] isEqualToString:@"rowid"])
        label.text = [NSString stringWithFormat:@"%d", [value intValue]];
    else if ([value isKindOfClass:[NSString class]])
        label.text = value;
    else if ([value isKindOfClass:[NSNumber class]])
    {
        NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
        formatter.maximumFractionDigits = 2;
        formatter.numberStyle = NSNumberFormatterDecimalStyle;
    
        label.text = [formatter stringFromNumber:value]];
    }
    else
        label.text = [value description];
    colNum++;
    

    Clearly, your logic will change depending upon whether you have a rowid column in your result set, or how many decimal places you want for floating point numbers, etc. It all depends upon what you put in the sql statement, but hopefully this gives you an idea of what you might do.