[英]Format specifies type “unsigned long” but the argument has type “int”
[英]Format specifies type 'int' but the argument has type 'NSString *'
任何人:
我想使用一個宏來打印日志,接下來,
#define isaObject(parameter) _Generic((parameter), id: YES, id __strong: YES, default: NO)
#define kNSLog(parameter) do \
{ \
BOOL is = isaObject((parameter)); \
if (is) \
{ \
NSLog(@"----Yes : %@", parameter); \
} \
else \
{ \
NSLog(@"----No : %d", parameter); \
} \
} while (NO)
int i = 99;
NSString * s = @"abcd";
kNSLog(i);
kNSLog(s);
然后,編譯器發出警告“Format指定類型'int',但參數的類型為'NSString *'”。
怎么修改,拜托?
您可以顯式地將參數強制轉換為int
以避免該警告:
NSLog(@"----No : %d", (int)parameter); \\
如果您“手動”執行宏替換,那么調試非常容易。
kNSLog(s)
擴展到
do
{
BOOL is = isaObject((s));
if (is)
{
NSLog(@"----Yes : %@", s);
}
else
{
NSLog(@"----No : %d", s); // the warning is thrown here
}
} while (NO)
您正在顯式檢查參數的類型,但是您沒有通知編譯器,因此它看到一個分支實際上可能正在嘗試使用%d
格式打印NSString
。
解決方案是在檢查后顯式轉換參數,以便為編譯器提供足夠的信息以使其保持靜默。 特別
if (is)
{
NSLog(@"----Yes : %@", parameter);
}
else
{
NSLog(@"----No : %d", (int)parameter);
}
#define isaObject(parameter) _Generic((parameter), id: YES, id __strong: YES, default: NO)
#define kNSLog(parameter) do
{
BOOL is = isaObject((parameter));
if (is)
{
NSLog(@"----Yes : %@", parameter);
}
else
{
NSLog(@"----No : %d", parameter);
}
}
while (NO)
請使用------>
NSString * s = @"YES";
kNSLog(s);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.