为什么要使用NSObject的"isMemberOfClass:class"?在XCode的自动补全中指定__unsafe_unretain? [英] Why does NSObject's "isMemberOfClass:class" specify __unsafe_unretained in XCode's autocompletion?

查看:59
本文介绍了为什么要使用NSObject的"isMemberOfClass:class"?在XCode的自动补全中指定__unsafe_unretain?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

模糊的概述是,我正在NSArray类别中编写一个方法,该方法将采用Class并将一个Array过滤为属于该类成员的元素.像这样:

The vague overview is that I'm writing a method in an NSArray Category that will take a Class and filter an Array down to elements that are members of that class. Something like:

@implementation NSArray(filter)
-(NSArray*)objectsOfClass:(Class)aClass {
   NSMutableArray *ret = [[NSMutableArray alloc] init];   
   for (id obj in self)
       if ([obj isMemberOfClass:aClass])
          [ret addObject:obj];

   return [NSArray arrayWithArray:ret];
}
@end

那么,顺便说一句,继续我的问题. NSObject.h显示isMemberOfClass:具有以下签名:

Sooo, with that out of the way, on to my question. NSObject.h shows that isMemberOfClass: has the following signature:

 -(BOOL)isMemberOfClass:(Class)aClass;

当我在XCode中键入此方法时,自动补全提示方法签名如下:

When I type this method in XCode, the autocompletion hints a method signature that looks like:

 [self isMemberOfClass:(__unsafe_unretained Class)]

我的问题是:

1)为什么NSObject.h中的方法原型与XCode的自动完成功能之间存在差异? 2)在我自己的方法中(显示在此问题的开头),我是否应该包含__unsafe_unretained修饰符?如果是这样,为什么?如果没有,为什么不呢?

1) Why the discrepancy between the method prototype in NSObject.h and XCode's autocompletion?
2) In my own method (shown at the start of the this question), should I include the __unsafe_unretained modifier? If so, why? If not, why not?

谢谢!

推荐答案

如果没有明确的所有权限制,则可以推断出一个;通常为__strong,但对于Class,则为__unsafe_unretained.这是有道理的,因为Class对象是不朽的,不需要由代码进行内存管理.

In the absence of an explicit ownership qualification one is inferred; this is usually __strong but in the case of Class it is __unsafe_unretained. This makes sense as Class objects are immortal and need not be memory managed by your code.

因此Xcode只是使隐式显式,而您不必自己执行此操作.

So Xcode is just making the implicit explicit and you do not need to do this yourself.

这篇关于为什么要使用NSObject的"isMemberOfClass:class"?在XCode的自动补全中指定__unsafe_unretain?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆