有没有办法在 Cocoa 上强制执行 Objective-C 运行时类型检查?

发布于 2024-08-17 13:47:33 字数 843 浏览 9 评论 0原文

大家好,我正在寻找一种方法来在 Cocoa 上的 Objective-C 中强制执行运行时类型检查或类似的事情。

这是我的代码示例。 我预计会出现关于变量“b”的错误分配的运行时错误。但事实并非如此。编译并执行没有任何错误。

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

 NSArray* a = [NSArray arrayWithObject: @"TEST"]; 
 NSURL* b = [a objectAtIndex:0]; 

    NSLog(@"Is this URL? %i", [b isKindOfClass:NSURL.class]);
    [pool drain];
    return 0;
}

// Console log after program execution:
// 2010-01-11 10:25:02.948 Type Checking[98473:a0f] Is this URL? 0

我很惊讶没有运行时类型检查。因为我使用了所有高级语言,如 VB、C#、Java、ActionScript...我不知道像 C 这样的低级语言,所以我不能确定这是正确的方法... 确实很难弄清楚为什么没有编译或运行时错误。但我逐渐理解这是现实 C 世界中的自然规则。但更强大的类型检查会对我有很大帮助。即使仅在调试会话中也是如此。有什么办法可以做到这一点吗?

如果没有运行时类型检查,我必须对错误类型的值使用什么样的编码和调试策略?运行时类型检查与否之间的权衡是什么?

Hi I'm finding a way to enforce runtime type checking or such things in Objective-C on Cocoa.

This is my code sample.
I expected runtime error about wrong assignment to variable 'b'. But it wasn't. Compiled and executed without any error.

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

 NSArray* a = [NSArray arrayWithObject: @"TEST"]; 
 NSURL* b = [a objectAtIndex:0]; 

    NSLog(@"Is this URL? %i", [b isKindOfClass:NSURL.class]);
    [pool drain];
    return 0;
}

// Console log after program execution:
// 2010-01-11 10:25:02.948 Type Checking[98473:a0f] Is this URL? 0

I surprised about there is no runtime type checking is there. Because I used all high level languages like VB, C#, Java, ActionScript... I don't know low-level language like C, so I can't sure this is right way...
It was really hard to figuring out why there is no compile or runtime error. But I'm getting to understand this as a natural rule in real C world. But more strong type checking will help me a lot. Even only in debugging session. Is there any way to do this?

And if there is no runtime type checking, what kind of coding and debugging strategy do I have to use about wrong typed values? And what's the trade off between runtime type checking is or isn't?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

柠檬色的秋千 2024-08-24 13:47:33

嗯,有运行时类型检查,但发生得晚一些。我假设当您尝试将 NSString 实例放入 NSURL* 变量时,您预计会出现某种异常。相反,当您尝试在 NSString 实例上调用任何特定于 NSURL 的方法时,您将收到异常。

例如,如果您尝试[b isFileURL],您将收到类似“NSString 不响应选择器‘isFileURL’”的异常。

了解为什么示例中没有编译时类型检查也很重要。具体来说,缺少编译时类型检查是 id 类型的一个独特且重要的属性,这就是 NSArray 的 -objectAtIndex: 返回的内容。

Well, there is run-time type checking, but it happens a little later. I assume you're expecting some sort of exception when you try to put an NSString instance into a NSURL* variable. Instead, you'll get the exception when you try to call any NSURL-specific methods on your NSString instance.

For example, if you try [b isFileURL] you'll get an exception like "NSString does not respond to selector 'isFileURL'".

It's also important to understand why there's no compile-time type checking in your example. Specifically, the lack of compile-time type checking is a unique and important property of the id type, which is what NSArray's -objectAtIndex: returns.

哥,最终变帅啦 2024-08-24 13:47:33

Objective-C 确实对消息参数和返回类型进行编译时类型检查,但它比许多其他语言要宽松一些。一个主要区别是像 NSArray 和 NSDictiomary 这样的集合类是通用的。你可以将任何类型的对象放入 NSArray - 元素不必是相同的类型。因此,添加或访问元素时不会进行类型检查。

没有对变量赋值进行运行时类型检查。我相信这是性能的捷径。一般来说,这不是什么大问题。如果将错误类型的值分配给变量,它最终会收到一条它不理解的消息,这会生成一条非常有用的错误消息。

Objective-C does have compile-time type checking for message arguments and return types, but it is somewhat looser than many other languages. One major difference is that collection classes like NSArray and NSDictiomary are generic. You can put any type of object into an NSArray - the elements don't have to be the same type. Therefore, there's no type-checking when adding or accessing elements.

There is no run-time type checking for variable assignment. I believe that is a performance short-cut. In general, it's not much of an issue. If the wrong type of value gets assigned to a variable, it'll eventually get sent a message it doesn't understand, which generates a pretty useful error message.

蓬勃野心 2024-08-24 13:47:33

当类型错误时,NSObject 框架通常会大惊小怪,但您可以手动检查类型并抛出异常:

if (![obj isKindOfClass:SomeObjectClass.class])
  [NSException raise:@"BadTypeException"
    format:@"Bad type at line %d", (int)__LINE__];
[obj xyz];

...

if (![obj conformsToProtocol:@protocol(SomeProtocol)])
  [NSException raise:@"BadTypeException"
    format:@"Bad type at line %d", (int)__LINE__];
[obj abc];

编辑:删除了前面的部分,声称调用不存在的方法会返回 nil 或零;正如 Mark Bessey 所说,如果非 nil 对象没有实现方法,则会抛出异常(不是返回零)。

The NSObject Framework will often kick up a fuss when the type is wrong but you can manually check for the type and throw an exception:

if (![obj isKindOfClass:SomeObjectClass.class])
  [NSException raise:@"BadTypeException"
    format:@"Bad type at line %d", (int)__LINE__];
[obj xyz];

...

if (![obj conformsToProtocol:@protocol(SomeProtocol)])
  [NSException raise:@"BadTypeException"
    format:@"Bad type at line %d", (int)__LINE__];
[obj abc];

Edit: Removed previous section claiming that calls to non-existent methods return nil or zeroes; as Mark Bessey says, an exception is thrown (not zero returned) if a non-nil object does not implement a method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文