MAZeroingWeakRef icon indicating copy to clipboard operation
MAZeroingWeakRef copied to clipboard

Xcode 4.5 warns about sending Class pointer to parameter of type id<NSCopying>

Open MSch opened this issue 12 years ago • 2 comments

Here:

static void RegisterCustomSubclass(Class subclass, Class superclass)
{
    [gCustomSubclassMap setObject: subclass forKey: superclass];
    [gCustomSubclasses addObject: subclass];
}

Can this warning be ignored?

MSch avatar Jun 17 '12 23:06 MSch

It may be worth filing a bug about this with Apple. Classes do conform to NSCopying and in fact the docs explicitly call out use as a dictionary keys as a reason for this:

https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSObject_Class/Reference/Reference.html#//apple_ref/occ/instm/NSObject/copy

In any case, casting it to id or id<NSCopying> should shut it up.

mikeash avatar Jun 17 '12 23:06 mikeash

I had the same problem and spoke to some engineers at WWDC, but didn't get a clear answer. Class is a subclass from NSObject, and NSObject has a private static method called copyWithZone, so everything will work. They recommended using NSStringFromClass or, as @mikeash noted, just casing the problem away with id<NSCopying>.

steipete avatar Jun 18 '12 00:06 steipete