Why does NSObject's “isMemberOfClass:class” specify __unsafe_unretained in XCode's autocompletion?

我只是一个虾纸丫 提交于 2019-11-30 17:18:45

问题


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

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

 -(BOOL)isMemberOfClass:(Class)aClass;

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

 [self isMemberOfClass:(__unsafe_unretained Class)]

My questions are:

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?

Thanks!


回答1:


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.

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



来源:https://stackoverflow.com/questions/14243003/why-does-nsobjects-ismemberofclassclass-specify-unsafe-unretained-in-xcode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!