加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

objective-c – Xcode警告:Format指定类型’long’但参数的类型

发布时间:2020-12-14 17:17:54 所属栏目:百科 来源:网络整理
导读:我收到以下代码行的警告: NSLog(@"selected segment: %li",_segmentControl.selectedSegmentIndex); selectedSegmentIndex属性的类型为NSInteger. 如果我将格式更改为%i,则会收到以下警告: Format specifies type 'int' but the argument has type 'long
我收到以下代码行的警告:

NSLog(@"selected segment: %li",_segmentControl.selectedSegmentIndex);

selectedSegmentIndex属性的类型为NSInteger.

如果我将格式更改为%i,则会收到以下警告:

Format specifies type 'int' but the argument has type 'long _Nullable'

Nullable类型是否有任何新的格式说明符,或者这只是Xcode 7中的一个错误?

解决方法

你应该输入:

NSLog(@"selected segment: %li",(long)_segmentControl.selectedSegmentIndex);

因为NSInteger在32位和64位架构中具有不同的长度.以前您没有看到警告,因为可能您只是针对64位架构进行编译.

我还建议阅读Apple Article,因为Xcode 7中有新的说明符(其中包括nullable和nonnull).

要回答您对评论的疑虑,请参阅此Apple document,其中说明了以下内容:

Type Specifiers

Script action: Warns about potential problems; may generate false negatives.

Typically,in 32-bit code you use the %d specifier to format int
values in functions such as printf,NSAssert,and NSLog,and in
methods such as stringWithFormat:. But with NSInteger,which on 64-bit
architectures is the same size as long,you need to use the %ld
specifier. Unless you are building 32-bit like 64-bit,these
specifiers generates compiler warnings in 32-bit mode. To avoid this
problem,you can cast the values to long or unsigned long,as
appropriate. For example:

06001

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读