简体   繁体   English

我应该使用NSInteger,NSNumber和NSUinteger吗?

[英]Should i cast with NSInteger, NSNumber, and NSUinteger?

In objective c, I often run into warnings in the compiler when I have methods that (for instance) return/take as an argument an NSInteger, and I instead place the argument as say a long int/NSNumber/etc value or variable. 在目标c中,当我有(例如)返回/接受NSInteger作为参数的方法时,我经常在编译器中遇到警告,而我将参数放置为long int / NSNumber / etc值或变量。 The code compiles fine, but I am always tempted to just do casts because the warnings make me uneasy. 编码编译得很好,但我总是很想做演员,因为这些警告让我感到不安。 I understand that it probably does not make a big difference either way, but is casting the preferred way to handle these warnings or not? 我知道它可能不会产生很大的不同,但是是否会抛出处理这些警告的首选方式?

If the warning is just a cast issue then by all means add the cast. 如果警告只是一个演员问题,那么一定要添加演员。 Many times Xcode is very helpful with this. 很多时候Xcode对此非常有帮助。

Many times the best answer is to change the declared type so there is neither an error nor a warning. 很多时候,最好的答案是更改声明的类型,因此既没有错误也没有警告。 Example: You have declared a variable as an int . 示例:您已将变量声明为int A method is expecting a NSInteger . 一种方法期待一个NSInteger Instead of a cast change the declaration of the variable to an NSInteger . 而不是NSInteger转换将变量的声明更改为NSInteger

But note that casting an NSNumber to an NSInteger for example will not fix anything, as @Josh points out they are very different things. 但请注意,例如,将NSNumber转换为NSInteger将无法解决任何问题,因为@Josh指出它们是非常不同的东西。

Eliminate all warnings! 消除所有警告! Then when a new one occurs it is readily apparent. 然后,当出现新的时,很明显。 If there are warnings then the code is not compiling fine. 如果有警告,那么代码编译不正确。

Note: There are times when individual warnings will need to be eliminated with a #pragma but these are extremely rare and only done when the cause is completely understood. 注意:有时需要使用#pragma消除个别警告,但这些警告非常罕见,只有在完全理解原因后才能完成。 An example: I needed to be able to cause a crash in an application for testing, that code caused a warning, a #pragma was added to eliminated the warning. 示例:我需要能够在应用程序中导致测试崩溃,该代码导致警告,添加了#pragma以消除警告。

Also run Analyzer and fix any warnings there. 还运行Analyzer并修复那里的任何警告。

The warnings are there for a reason, they help catch errors. 警告是有原因的,它们有助于发现错误。

int , NSInteger , and NSUInteger are scalars. intNSIntegerNSUInteger是标量。 NSNumber is an object. NSNumber是一个对象。 They have nothing to do with one another, and casting between them to hide that fact from the compiler will bring disaster. 它们彼此无关,并且在它们之间进行转换以隐藏编译器中的这一事实将带来灾难。

Remember, casting means that you throw away the compiler's ability to check that you are doing the right thing. 请记住,强制转换意味着您抛弃编译器检查您正在做正确事情的能力。 You can use casting to lie to the compiler, as if to say to it, "Don't worry, everything will be okay." 您可以使用强制转换来欺骗编译器,就好像对它说:“别担心,一切都会好的。” Do not lie to the compiler! 不要欺骗编译器! If you do, then when the app runs, you will get nonsense at worst, and a crash at best. 如果你这样做,那么当应用程序运行时,你会在最糟糕的情况下得到废话,最好是崩溃。 (The crash is best, because it stops you dead; not crashing means you now have a big mistake that will be very difficult to track down later.) (崩溃是最好的,因为它会阻止你死亡;不会崩溃意味着你现在有一个很大的错误,以后很难追查。)

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

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