[英]How can I make my iPhone App run both on 64 bit and 32 bit?
當我嘗試在64位模式下運行時,遇到的確切錯誤是Format specifies type 'int' but the argument has type 'long'
。
我可以通過將%d
更改為%ld
來解決此錯誤,但是當我在32位(正常)模式下運行應用程序時,出現一條錯誤消息: Format specifies type 'long' but the argument has type 'int'
如何計算64位和32位? 我是否創建一個if(condition)?
- (void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent:(NSInteger)component {
// Handle the selection
if(pickerView.tag == 1){
start = row+1;
[startButton setTitle:[NSString stringWithFormat:@"%d. %@", row+1, [stops objectForKey:[NSString stringWithFormat:@"%d", row+1]]] forState:UIControlStateNormal];
}else if (pickerView.tag == 2){
stop = row+1;
[endButton setTitle:[NSString stringWithFormat:@"%d. %@", row+1, [stops objectForKey:[NSString stringWithFormat:@"%d", row+1]]] forState:UIControlStateNormal];
}
}
如果右鍵單擊NSInteger
,然后單擊“轉到定義”,則可以確切地看到它是#define
d的方式,並以此為骨架來為%d
/ %ld
設置類似的#define
以匹配您的NSInteger
。
#if __LP64__
typedef long NSInteger;
#else
typedef int NSInteger;
#endif
這就是NSInteger
工作方式。 您可以執行類似的操作:
#if __LP64__
#define FS_NSInt ld
#else
#define FS_NSInt d
#endif
然后,只需使用FS_NSInt
作為NSInteger
的格式規范即可。 (仍然在前面加上一個%
)
不要使用NSInteger
或NSUInteger
作為格式參數。 而是將它們強制轉換(例如: long long
:
[NSString stringWithFormat:@"%lld", (long long)row+1]
從Apple文檔 :
類型說明符:
通常,在32位代碼中,您可以使用%d說明符來設置諸如printf,NSAssert和NSLog之類的函數以及諸如stringWithFormat:之類的方法中的int值格式。 但是對於NSInteger(在64位體系結構上其大小一樣長),您需要使用%ld說明符。 除非您要像32位一樣構建32位,否則這些說明符將以32位模式生成編譯器警告。 為避免此問題,可以將值強制轉換為long或unsigned long。 例如:
NSInteger i = 34;
printf("%ld\n", (long)i);
解決此問題的一種方法是使用stdint.h中的類型:
int8_t, uint8_t, int16_t, uint16_t, int32_t, uint32_t, int64_t, uint64_t
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.