Why does NSObject's “isMemberOfClass:class” sp

2019-02-14 14:39发布

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条回答
Ridiculous、
2楼-- · 2019-02-14 15:15

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.

查看更多
登录 后发表回答