[英]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];
}
誰能幫我? 非常感謝你。
您可以使用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.