简体   繁体   English

在Objective-C开发中从iOS迁移到Mac时,NSIntegers会给出类型匹配错误

[英]When moving to Mac from iOS in Objective-C development, NSIntegers give type matching errors

I've been working on an iOS project for some time now, and I've recently decided to port the code to a Mac project. 我一直在研究iOS项目已有一段时间了,我最近决定将代码移植到Mac项目中。 Because I've chosen to use NSInteger in my code, and NSInteger is a environment-dependent typecast, it means that my variables are different types on the iOS and Mac apps. 因为我选择在我的代码中使用NSInteger,而NSInteger是一个依赖于环境的类型转换,这意味着我的变量在iOS和Mac应用程序中是不同的类型。

When I run the test suite on the Mac, all my STAssertEquals calls fail with the error "Type mismatch --" because the types don't match: 当我在Mac上运行测试套件时,我的所有STAssertEquals调用都会失败并显示错误“Type mismatch - ”,因为类型不匹配:

NSInteger foo = 1;
STAssertEquals(foo, 1, nil); // Test fails!!

Typecasting my scalars seems to work, but this seems really messy: 类似我的标量似乎工作,但这似乎非常混乱:

NSInteger foo = 1;
STAssertEquals(foo, (NSInteger)1, nil); // Succeeds, but is ugly!!

Am I missing something here? 我在这里错过了什么吗? I'm starting to suspect that the decision to use NSIntegers was a poor choice. 我开始怀疑使用NSIntegers的决定是一个糟糕的选择。

Update: Perhaps this article is related. 更新:也许这篇文章是相关的。 It seems to support typecasting scalars. 它似乎支持类型转换标量。

Your use of NSInteger is probably the right choice, but it does cause some issues with STAssertEquals. 您对NSInteger的使用可能是正确的选择,但它确实会导致STAssertEquals出现一些问题。

STAssertEquals fails if the two objects have different Objective-C type encodings 如果两个对象具有不同的Objective-C类型编码,则STAssertEquals会失败

A lone 1 will be interpreted by the C compiler as a (signed int), which has type encoding "i". 单独的1将被C编译器解释为(signed int),其类型编码为“i”。

These macros would also fail: 这些宏也会失败:

unsigned u = 1;
STAssertEquals(u, 1, nil); // Fails because u is type "I" (unsigned int) and 1 is type "i" (int)

char c = 1;
STAssertEquals(c, 1, nil); // Fails because c is type "c" (signed char) and 1 is type "i" (signed int)

To make the compiler use a different type for 1 , you would add a suffix such as 1U for (unsigned int), 1UL for (unsigned long) or 1L for (long). 要使编译器对1使用不同的类型,您可以为(unsigned int)添加后缀,例如1U为(unsigned long)添加1UL或为(long)添加1L

NSInteger is defined to be the same size as pointers, which is dependent on the device architecture. NSInteger被定义为与指针大小相同,这取决于设备体系结构。 On 32-bit Mac OS X and iOS, NSInteger is type (signed int), or "i". 在32位Mac OS X和iOS上,NSInteger是type(signed int)或“i”。 On 64-bit Mac OS X, it is type long, or "q". 在64位Mac OS X上,它是long类型或“q”。 (64-bit Mac OS X is a LP64 architecture, which means that L ongs and P ointers are both 64 -bits wide) (64位Mac OS X是LP64架构,这意味着L ongs和P ointers都是64位宽)

Thus, the left side of your STAssertEquals is a "q", whereas the right side is "i", causing the type mismatch. 因此,STAssertEquals的左侧是“q”,而右侧是“i”,导致类型不匹配。

Your solution of using (NSInteger) 1 seems to accurately represent what you want - test an NSInteger variable against an NSInteger constant of value 1. 您使用(NSInteger) 1解决方案似乎准确地表示您想要的内容 - 针对值为1的NSInteger常量测试NSInteger变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM