Swift中无法使用ARC的方法 [英] ARC unavailable methods in Swift
问题描述
我能够看到一个有趣的案例 Estimote Nearables SDK
I was able to see an interesting case using Estimote nearables SDK
他们有一个名为zone
的类ESTNearable
.
// ENUM
typedef NS_ENUM(NSInteger, ESTNearableZone ) {
ESTNearableZoneUnknown = 0,
ESTNearableZoneImmediate,
ESTNearableZoneNear,
ESTNearableZoneFar,
};
// CLASS
@interface ESTNearable : NSObject <NSCopying, NSCoding>
// ...
@property (nonatomic, assign, readonly) ESTNearableZone zone;
// ...
@end
因此,当我尝试在 Swift 中使用此方法时,编译器将失败,并显示以下错误:
So when I try to use this method in Swift, compiler fails with that error:
据我了解,存在某种编译器错误,由于某种原因,它认为我想使用NSObject
- (struct _NSZone *)zone OBJC_ARC_UNAVAILABLE;
中的旧zone
方法,因此我可以使用该类的其他特定属性,而不会出现任何问题.
As I understand there is some kind of compiler bug and for some reason it believes that I want to use old zone
method from NSObject
- (struct _NSZone *)zone OBJC_ARC_UNAVAILABLE;
I can use other specific properties of that class without any problems.
当我使用SDK时,无法更改zone
方法的名称.我相信我可以编写某种obj-c类别,在其中添加一些新方法,该方法将返回原始方法的值,但是我不想在我的项目中添加obj-c类.
As I use an SDK I can not change the name of the zone
method. I believe I can write some kind of obj-c category, add some new method there, which will return value of original one, but I do not want to add obj-c classes in my project.
是否有可能迅速调用此方法,因为我相信类实例将调用正确的zone
方法?
Is there any possibility to call this method from swift as I believe correct zone
method will be called for class instances?
提前谢谢!
推荐答案
在这里,我发现了同样的问题.我回答在那儿更深入.我找不到更好的东西,所以我继续了以前的假设.
Here I found the same question. I answered more deeply there. I could not find something more good, so I went ahead with my old assumptions.
我将此类别添加到桥接标题中.效果很好.
I Added this category to Bridging Header. It worked fine.
#import <EstimoteSDK/EstimoteSDK.h>
@interface ESTNearable (Ex)
/// Solving the compiler problem that "zone" method is unavailable in Swift
@property (readonly) ESTNearableZone nearableZone;
@end
// Implementation
@implementation ESTNearable (Ex)
- (ESTNearableZone)nearableZone
{
return self.zone;
}
@end
之后,我只在 Swift
var zone = someNearable.nearableZone
这篇关于Swift中无法使用ARC的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!