【问题标题】:How am I not following this protocol?我怎么不遵守这个协议?
【发布时间】:2016-08-07 07:45:35
【问题描述】:

有什么我不明白的地方吗?

协议:

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' 的属性 'visible'”。它还说“getter for 'visible' 提供的 Objective-C 方法 'visible' 与需求的选择器 ('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

【问题讨论】:

标签: objective-c swift xcode


【解决方案1】:

提示,试试这种风格:

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

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

【讨论】:

  • 就可以了。我总是遇到一些 Objective-C 绑定的问题。
猜你喜欢
  • 2022-12-18
  • 1970-01-01
  • 1970-01-01
  • 2010-11-03
  • 2017-10-16
  • 1970-01-01
  • 2022-08-21
  • 2019-06-06
  • 1970-01-01
相关资源
最近更新 更多