objective-c - 将 NSNumber* 传递给 NSString* 预期参数不会导致编译器警告/错误

标签 objective-c debugging xcode4

有任何解决这个问题的方法吗?

我更改了函数的签名以传递 NSString 对象而不是 NSNumber 对象。除了我有一些实例仍然通过旧的 NSNumber 对象。很难追踪,因为编译器不会为此显示任何错误或警告。我尝试删除 DerivedData 文件夹。没有帮助。

我也尝试过分析,也没有发现这些问题。

我知道我可以进行 NSAssert 检查以确保传入的参数类型正确,但这似乎是倒退的。这应该是编译器发现并警告我的东西。

有什么建议么?
- (void) test:(NSString *)par;
打电话
NSString *str = (NSString *)[array objectAtIndex:0];
除了对象是 NSNumber,所以从技术上讲,它会将 NSNumber 转换为 NSString。调试器将其视为 NSNumber,因此不确定为什么首先允许它。

最佳答案

Any way to fix this?



不容易。断言(正如你提到的)是一个起点。

I changed my a signature of a function to pass NSString object instead of NSNumber object.



objc 不能那样工作 - objc 对象只是在执行时通过它们的参数/变量传递地址。

类型转换时语言没有提供显式类型转换;如果那是你的期望。

objc 也不使用类型安全转换来确定参数是否属于它被转换为的类型。

Except I have some instances that still pass the old NSNumber object. It's hard to track down because the compiler doesn't display ANY errors or warnings for this. I tried deleting the DerivedData folder. Doesn't help.



你得到 NSNumbers 因为那是数组中存在的。

I also tried to do Analyze, that doesn't catch those problems either.



它是一种非常动态的语言,这就是它的设计方式。

分析器无法发现或查找这些问题,因为必须在运行时执行检查。

此外,传递 objc 对象并动态使用它们是司空见惯的(使用 respondsToSelector:isKindOfClass: )。

I know I can do an NSAssert check to make sure that the incoming parameter type is proper, but this seems backwards. This should be something the compiler picks up and warns me.



我使用断言并编写了几种检查和类型来在我想要的地方返回类型安全。

我知道没有为此的公共(public)库-您可能需要自己实现所需的检查。为这些检查创建一个简单的 header 并实现它们很容易。

Except the object is NSNumber, so technically it casts an NSNumber to NSString. Debugger sees it as NSNumber, so not sure why it's being allowed in the first place.



对于 objc 变量,调试器评估地址处的对象以确定其类型,而不是使用变量声明的类型。

关于objective-c - 将 NSNumber* 传递给 NSString* 预期参数不会导致编译器警告/错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7194003/

相关文章:

iphone - 根据 JSON 响应在 Tapku 日历月上标记日期

iphone - UIS ScrollView 。内部有许多 UIView 的页面 Controller

objective-c - 指点一下,我使用得正确吗? Objective-c/ cocoa

c++ - 在调试器上的 CStringArray 中显示元素一再一?

visual-studio-2010 - Visual Studio Debug模式:how to go to the location of the next statement to be executed?

javascript - 如果对象的类型只是 "Object",如何在 DevTools 内存快照中找到特定对象?

iphone - 如果我已经发布的 v1 没有版本化的核心数据模型,我可以使用 "Automatic Lightweight Migration"吗?

iphone - 我想在持续时间达到 5 分钟时停止 AVAudioRecorder

xcode - 图片中显示的这个是什么? (iPad 中的界面)

c++ - printf() 在 Xcode 4 调试期间间歇性停止打印