在Label中显示Integer值,int到String转换?

时间:2013-08-18 15:34:33

标签: ios nsstring stringwithformat

如何在UILabel的{​​{1}}中显示整数值?我做了文本和日期和图像,但如何将int转换为字符串。我正在尝试使用以下代码

ViewDidLoad

但没有用。请帮助我。如何使用UILabel设置整数?

这是我的评论。

 NSString* label=[aa stringWithFormat:@"%d",((Comments *) [self.list objectAtIndex:0]).noofcomm]]; 
 [self.comments2 setText:label];

DBClass.m

    @interface Comments : NSObject
{
    NSInteger iD;
    UIImage *photo;
    NSString *name;
    NSString *descrp;
    NSDate *date;
    NSString *msg;
    NSInteger noofcomm;
    NSInteger nooflikes;
}
@property(nonatomic,assign)NSInteger iD;
@property(nonatomic,retain)UIImage *photo;
@property(nonatomic,retain)NSString *name;
@property(nonatomic,retain)NSString *descrp;
@property(nonatomic,strong)NSDate *date;
@property(nonatomic,retain)NSString *msg;
@property(nonatomic,assign)NSInteger noofcomm;
@property(nonatomic,assign)NSInteger nooflikes;


@end

RootViewController.m

     #import "DBClass.h"
#import "Comments.h"

@implementation DBClass
- (NSMutableArray *) getMyComments{
    NSMutableArray *wineArray = [[NSMutableArray alloc] init];
    @try {
        NSFileManager *fileMgr = [NSFileManager defaultManager];
        NSString *dbPath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"ComntDB.sqlite"];
        BOOL success = [fileMgr fileExistsAtPath:dbPath];
        if(!success)
        {
            NSLog(@"Cannot locate database file '%@'.", dbPath);
        }
        if(!(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK))
        {
            NSLog(@"An error has occured.");
        }
        const char *sql = "SELECT id, photo,name,descrp, time,msg,comments,likes FROM Com";
        sqlite3_stmt *sqlStatement;
        if(sqlite3_prepare(db, sql, -1, &sqlStatement, NULL) != SQLITE_OK)
        {
            NSLog(@"Problem with prepare statement");
        }

        //
        while (sqlite3_step(sqlStatement)==SQLITE_ROW) {
            Comments *MyWine = [[Comments alloc]init];

            MyWine.iD = sqlite3_column_int(sqlStatement, 0);

            const char *raw = sqlite3_column_blob(sqlStatement, 1);
            int rawLen = sqlite3_column_bytes(sqlStatement, 1);
            NSData *data = [NSData dataWithBytes:raw length:rawLen];
            MyWine.photo = [[UIImage alloc] initWithData:data];
            MyWine.name = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,2)];

            MyWine.descrp = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 3)];

            MyWine.date=[NSDate dateWithTimeIntervalSince1970:sqlite3_column_double(sqlStatement,4)];
             MyWine.msg = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,5)];
            MyWine.noofcomm = sqlite3_column_int(sqlStatement, 6);
            MyWine.nooflikes = sqlite3_column_int(sqlStatement, 7);

            [wineArray addObject:MyWine];
        }
    }
    @catch (NSException *exception) {
        NSLog(@"An exception occured: %@", [exception reason]);
    }
    @finally {
        return wineArray;
    }

}

@end

4 个答案:

答案 0 :(得分:2)

NSInteger someInteger = myInteger;
NSString *someString = [NSString stringWithFormat:@"%d", someInteger];
myLabel.text = someString;

NSNumber *someNumber = @(myInteger);
NSString *someString = [someNumber stringValue];
myLabel.text = someString;

两者都有效。

编辑:

在你的情况下,它将是这样的:

NSInteger someInteger = ((Comments *) [self.list objectAtIndex:0]).noofcomm;
NSString someString = [NSString stringWithFormat:@"%d", someInteger];
self.comments2.text = someString;

如果它仍然无法正常工作,请确保问题出在其他地方,而不是转换。使用属性noofcomm检查具有有效值,检查您的标签引用是否正常(在转换之前使用随机值进行测试)以及类似的事情。

答案 1 :(得分:1)

您需要构建一个NSString

int someInteger = 10;
NSString *someString = [[NSString alloc] initWithFormat:@"%d", someInteger];

答案 2 :(得分:0)

你可以使用类似[NSString string_from_int:42]的东西,因为0.1.3:https://github.com/superarts/LCategory

答案 3 :(得分:-1)

_lbl_yourLabel.text=[NSString stringWithFormat:@"%d",[[dic valueForKey:@"your integer value"] intValue]];

左上角是您的标签,名为" yourLabel" ," dic"是你的json响应字典,其中所有数据都以关键值术语出现,"你的整数值"是将值分配给标签" yourLabel"的键,我们采用了intValue,因为我们不能直接将整数值赋给标签。

或者你也可以在下面试试:

int anyInteger = 13;
NSString *yourString = [[NSString alloc] initWithFormat:@"%d", anyInteger];
self.yourLabel.text = yourString;