Format指定类型“unsigned long”但参数的类型为“int”

时间:2015-02-03 05:18:31

标签: ios objective-c format-specifiers

Format指定类型为“unsigned long”,但参数的类型为“int”

我在XCode中出现此错误,无论我放入什么格式说明符,或者如果我更改为NSInteger,NSUInteger,long或int,仍然会出错!?我该如何解决这个问题?

-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view

{

我有这些行,第2行是@“%lu”的错误,(行%max)

NSUInteger max = (NSInteger)[self.calendar maximumRangeOfUnit:NSCalendarUnitHour].length;
[lblDate setText:[NSString stringWithFormat:@"%lu",(row % max)]]; 
lblDate.textAlignment = NSTextAlignmentRight;

感谢您的帮助!

4 个答案:

答案 0 :(得分:8)

对于NSInteger,您应该使用%td%tuNSUInteger

有关详情https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html

,请参阅此链接

答案 1 :(得分:4)

将您的代码更改为以下内容:

NSUInteger max = (NSInteger)[self.calendar maximumRangeOfUnit:NSCalendarUnitHour].length;
[lblDate setText:[NSString stringWithFormat:@"%d",(int)(row % max)]]; 
lblDate.textAlignment = NSTextAlignmentRight;

答案 2 :(得分:1)

使用与NSUInteger类型匹配的格式说明符,如%tu,或将(row%max)的结果强制转换为int。

答案 3 :(得分:0)

好吧,似乎xcode对%td很满意,尽管它总是在暗示其他东西。谢谢 !