我有NSDistantObject类对象的数组。

NSLog(@"%@", [[saveClassObject objectAtIndex:0] connectionForProxy]);
        NSLog(@"%@", NSStringFromClass([[saveClassObject objectAtIndex:0] class]));
        if ([[saveClassObject objectAtIndex:0] isKindOfClass:[NSDistantObject class]]) {
            NSLog(@"Yes");
        }
        else {
            NSLog(@"NO");
        }

上面代码的输出是
2011-10-20 20:49:58.841 DistributedServer[4239:a0f] (** NSConnection 0x10013ec80 receivePort <CFSocket 0x1001133d0 [0x7fff70289ee0]>{valid = Yes, type = 1, socket = 3, socket set count = 140733193388036,
    callback types = 0x2, callout = __NSFireSocketAccept (0x7fff8254e74a), source = 0x10010abb0,
    run loops = <CFArray 0x10015f930 [0x7fff70289ee0]>{type = mutable-small, count = 4, values = (
    0 : <0x100112870>
    1 : <0x100112870>
    2 : <0x1001126a0>
    3 : <0x1001126a0>
)},
    context = <CFSocket context 0x1002000b0>} sendPort <02010610 1002c323 c0a801ae 00000000 00000000> refCount 3 **)
2011-10-20 20:49:58.842 DistributedServer[4239:a0f] NSDistantObject
2011-10-20 20:49:58.844 DistributedServer[4239:a0f] NO

NSStringFromClass返回保存的Class对象数组,其中包含NSDistantObject类对象。 isKindofClass同样返回No。为什么?

最佳答案

NSProxy-isKindOfClass:调用forwardInvocation:。它旨在测试代理所代表的对象的类,而不是代理本身。


要检查您拥有的对象是否是代理,您可能需要使用isProxy
要检查代理的类,可以检查它的class是否等于[NSDistantObject class]或调用isSubclassOfClass:

关于objective-c - NSDistantObject类上的NSStringFromClass和isKindofClass,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7838312/

10-09 16:24