Overriding OBJC_ARC_UNAVAILABLE methods in Swift

I believe I found a small bug in Swift Compiler when used one third party SDK.


Basically I have some obj-c class with overriden zone method. Swift compiler doesn't compile

zone
method as for some reason it assumes that I want to call
zone
method from
NSObject
, which can not be used in ARC environment.

I believe problem exists for all methods marked with OBJC_ARC_UNAVAILABLE flag.


Here is an example:


@interface Test : NSObject
@property (nonatomic, readonly) NSString *zone;
@end


@implementation Test
- (NSString *)zone {
    return @"Test";
}
@end


When I use this class in Swift I get Compiler error.

let test = Test()
test.zone()

Error: 'zone()' is unavailable; not available in automatic reference counting mode


To make it work I added small category with new method.

@interface Test(Extended)
- (NSString *)testZone;
@end

@implemntation Test (Extended)
- (NSString *)testZone {
    return self.zone;
}
@end

I don't think this is a bug.


It looks like you are overriding a propery of a super class for a completely different purpose, which is probably not a great idea, and the compiler has no way of knowing that.


While 'zone' is unavailable in ARC, it *still* exists in the class and is just marked as unavailable to prevent you from accidentally using it.


Until the property is completely removed from NSObject, you should use a different name for the property.

Overriding OBJC_ARC_UNAVAILABLE methods in Swift
 
 
Q