2013-08-18 42 views
2

如何在的ViewDidLoad中顯示整數值?我爲文本和日期和圖像,但如何將int轉換爲字符串。我試圖用下面的代碼顯示標籤中的整數值,int轉換爲字符串?

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

但沒有工作。請幫助我。 如何用UILabel設置Integer?

這是我comments.h

@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 

DBClass.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 

RootViewController.m

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

@interface RootViewController() 

@end 

@implementation RootViewController 
@synthesize list; 
@synthesize image2; 
@synthesize name2; 
@synthesize descrp2; 
@synthesize msg2; 
@synthesize date2; 
@synthesize comments2; 
@synthesize likes2; 

- (void)viewDidLoad 
{ 
    DBClass * mywines =[[DBClass alloc] init]; 
    self.list = [mywines getMyComments]; 
    [self.image2 setImage:((Comments *) [self.list objectAtIndex:0]).photo]; 
    [self.name2 setText:((Comments *) [self.list objectAtIndex:0]).name]; 

    [self.descrp2 setText:((Comments *) [self.list objectAtIndex:0]).descrp]; 

    NSDateFormatter* fmtr = [[NSDateFormatter alloc] init]; 
    [fmtr setDateFormat:@"MM/dd/yy"]; 
    NSString* label_str = [fmtr stringFromDate:((Comments *) [self.list objectAtIndex:0]).date]; 
[self.date2 setText:label_str]; 
      [self.msg2 setText:((Comments *) [self.list objectAtIndex:0]).msg]; 
    //[self.comments2 setText:((Comments *) [self.list objectAtIndex:0]).noofcomm]; 
     // int solution = 1; 
// [self.comments2 setText:[NSString stringWithFormat:@"%d", solution]]; 
// int solution2 = 1; 
// [self.likes2 setText:[NSString stringWithFormat:@"%d", solution2]]; 

    [super viewDidLoad]; 
} 
- (void)viewDidUnload 
{ 
    [self setImage2:nil]; 
    [self setName2:nil]; 
    [self setMsg2:nil]; 
    [self setDescrp2:nil]; 
    [self setComments2:nil]; 
    [self setLikes2:nil]; 
    [self setDate2:nil]; 
    [super viewDidUnload]; 
} 
- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

@end 
+0

使用'[NSString的stringWithFormat:@ 「%d」,溶液]'是假設'solution'是一個'int'變量的正確方法。 – rmaddy

+0

請只發布相關的代碼。 –

+0

@rmaddy從sqlite數據庫獲取數據。如何將int轉換爲string.I被試過。我的問題是如何將整數轉換並設置爲UILabel。 – user2694118

回答

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有一個有效的值,檢查您的標籤參考是否可以(在轉換前用一個隨機值進行測試),以及類似的東西。

+0

您提到someInteger = 20;不是這一個,我從sqlite數據庫中獲取數據請參閱DBClass.m,我問如何設置與UIlabel進行轉換的問題。如何使用整數與UILabel設置? – user2694118

+1

整數的來源無關緊要,轉換將是相同的。只需在上面的代碼中將'myInteger'更改爲sqlite數據庫中的整數。 '20'是一個例子 –

+0

PLease看到我的RootViewController.m我已經設置了文本,圖像和日期,但如何設置整數一次看上面的代碼。 – user2694118

1

你需要建立一個NSString

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

從sqlite數據庫中獲取數據在這裏我問如何設置與UIlabel轉換 – user2694118

-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; 
+0

請使用[編輯]鏈接解釋此代碼如何工作,不要只給代碼,因爲解釋更有可能幫助未來的讀者。 –