我怎么不遵守这个协议?

时间:2016-08-07 07:45:35

标签: objective-c swift xcode

有什么我不明白的吗?

协议:

public protocol SLKTypingIndicatorProtocol : NSObjectProtocol {

    /**
     Returns YES if the indicator is visible.
     SLKTextViewController depends on this property internally, by observing its value changes to update the typing indicator view's constraints automatically.
     You can simply @synthesize this property to make it KVO compliant, or override its setter method and wrap its implementation with -willChangeValueForKey: and -didChangeValueForKey: methods, for more complex KVO compliance.
     */
    public var visible: Bool { get set }

    /**
     Dismisses the indicator view.
     */
    optional public func dismissIndicator()
}

我的代码:

public class TypingListView: UIView, SLKTypingIndicatorProtocol {

    var _visible: Bool = false
    public var visible: Bool {
        get {
            return self._visible
        }

        set (val) {
            self._visible = val
        }
    }

    public func isVisible() -> Bool {
        return self.visible
    }

    public func dismissIndicator() {
        self.visible = false
    }

// Other code...
}

我不断得到的错误:“类型'TypingListView'不符合协议'SLKTypingIndicatorProtocol'”

当我扩展错误时,它指出:“协议要求属性'可见',类型为'Bool'”。它还说“getter为'可见'提供的Objective-C方法'可见'与要求的选择器不匹配('isVisible')”

我发现协议实际上也是如何在Objective-C中读取的:

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

/** Generic protocol needed when customizing your own typing indicator view. */
@protocol SLKTypingIndicatorProtocol <NSObject>
@required

/**
 Returns YES if the indicator is visible.
 SLKTextViewController depends on this property internally, by observing its value changes to update the typing indicator view's constraints automatically.
 You can simply @synthesize this property to make it KVO compliant, or override its setter method and wrap its implementation with -willChangeValueForKey: and -didChangeValueForKey: methods, for more complex KVO compliance.
 */
@property (nonatomic, getter = isVisible) BOOL visible;

@optional

/**
 Dismisses the indicator view.
 */
- (void)dismissIndicator;

@end

NS_ASSUME_NONNULL_END

1 个答案:

答案 0 :(得分:2)

提示,试试这种风格:

public var visible: Bool {
    @objc(isVisible) get {
        return self._visible
    }

    set (val) {
        self._visible = val
    }
}