相关疑难解决方法(0)

覆盖非动态类delaration

使用新的Xcode 8.3,我收到错误:

无法覆盖扩展中的非动态类声明

在代码行

 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
Run Code Online (Sandbox Code Playgroud)

我怎么能避免这个警告?

declaration uitableview ios swift xcode8

8
推荐指数
2
解决办法
5133
查看次数

如何为子类覆盖快速协议功能(例如,UIView中的UILabel)

我正在尝试实现一个扩展功能,该功能应根据使用它的类的类型而有所不同。这些对象必须是UIView(或子类)。它应该始终使用在指定类型上扩展的函数,但如果不符合任何扩展函数,则应改用UIView方法(作为后备)。

这是我要执行的操作的一个示例:

protocol aProtocol {
    typealias completionBlock = (_ finished:Bool)->()
    func doSomething(completion: completionBlock)
}


extension UIView: aProtocol {
    func doSomething(completion: (Bool) -> ()) {
        print("Im an UIView")
    }
}

extension aProtocol where Self: UILabel {
    func doSomething(completion: (Bool) -> ()) {
        print("im an UILabel")
    }
}

extension aProtocol where Self: UIImageView {
    func doSomething(completion: (Bool) -> ()) {
        print("im an UIImageView")
    }
}
Run Code Online (Sandbox Code Playgroud)

执行:

UIView().doSomething { (foo) in } // Should print "Im an UIView"
UIButton().doSomething { (foo) in } …
Run Code Online (Sandbox Code Playgroud)

protocols uiview ios swift

8
推荐指数
1
解决办法
169
查看次数

为什么我们要使用扩展添加协议一致性?

如果我有一个类Christmas和一个协议Merry,为了使Christmas符合Merry,许多人会这样做:

class Christmas {
    ...
}

extension Christmas: Merry {
    ...
} 
Run Code Online (Sandbox Code Playgroud)

它也受到Apple的鼓励.

但是,在定义类时,让类符合协议是不是更方便?像这样:

class Christmas: Merry {
    ...
}
Run Code Online (Sandbox Code Playgroud)

两种方法有什么区别?

swift

1
推荐指数
1
解决办法
546
查看次数

标签 统计

swift ×3

ios ×2

declaration ×1

protocols ×1

uitableview ×1

uiview ×1

xcode8 ×1