簡體   English   中英

將NSInteger轉換為NSString Xcode iOS8

[英]Convert a NSInteger to NSString Xcode iOS8

我正在嘗試將NSInteger轉換為NSString以在UIAlerView中顯示NSInteger值。 但問題是我得到這個錯誤:“當我試圖轉換NSInteger時,錯誤的接收器類型'NSInteger'(又名'long')”。

NSInteger:

NSInteger clasesDadas = clases.count;

clases.count是TableView中的行數。

代碼是:

-(void)infoAction
{
    NSInteger clasesDadas = clases.count;

    NSString *inStr = [NSString stringWithFormat:@"%d", [clasesDadas intValue]]; /*HERE IS THE ERROR*/

    UIAlertView *alertatiempo = [[UIAlertView alloc] initWithTitle:@"Información" message:inStr delegate:self cancelButtonTitle:@"Aceptar" otherButtonTitles:nil];
    [alertatiempo show];

}

誰能幫我? 非常感謝你。

intValue不是NSInteger上存在但NSNumber上存在的方法。 由於NSInteger是32Bit上的基本類型int的typedef,而在64bit上是long ,因此它沒有方法。

以下是您可以做到的方法,以確保它可以在32位和64位設備上運行。

NSString *inStr = [NSString stringWithFormat:@"%ld", (long)clasesDadas ]; 

同樣如meronix所述,count方法將導致NSUInteger因此您可能需要相應地檢查類型和格式

您可以使用Boxed表達式。

例如@(10).stringValue@(variable).stringValue

你的clasesDadas已經是一個NSInteger ,所以你不能使用intValue方法。

要將整數轉換為字符串,可以將其轉換為NSNumber並使用方法stringValue如下所示:

NSString *inStr = [[NSNumber numberWithInteger:clasesDadas] stringValue]

您還可以使用語法@()NSInteger轉換為對象(NSNumber)。 例如 :

NSString *inStr = [NSString stringWithFormat:@"%@", @(clasesDadas)];

嘗試這個:

NSString *inStr = [NSString stringWithFormat:@"%lu", (unsigned long)clasesDadas];

NSInteger聲明可能在32位或64位設備上有所不同,因此這是獲得NSString的array.count的蘋果建議

NSString *inStr = [NSString stringWithFormat:@"%d", clasesDadas];
NSString *inStr = [NSString stringWithFormat:@"%d", (int)clasesDadas];

這應該工作正常。

 NSString *inStr = [NSString stringWithFormat:@"%d", (int)clases.count]; 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM