标签: swift-protocols

协议只能用作通用约束,因为它具有Self或associatedType要求

我有一个协议RequestType,它有关联类型模型,如下所示.

public protocol RequestType: class {

    associatedtype Model
    var path: String { get set }

}

public extension RequestType {

    public func executeRequest(completionHandler: Result<Model, NSError> -> Void) {
        request.response(rootKeyPath: rootKeyPath) { [weak self] (response: Response<Model, NSError>) -> Void in
            completionHandler(response.result)
            guard let weakSelf = self else { return }
            if weakSelf.logging { debugPrint(response) }
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

现在我正在尝试为所有失败的请求排队.

public class RequestEventuallyQueue {

    static let requestEventuallyQueue = RequestEventuallyQueue()
    let queue = [RequestType]()

}
Run Code Online (Sandbox Code Playgroud)

但我得到的错误是let queue = [RequestType](),Protocol RequestType只能用作通用约束,因为它具有Self或associatedType要求.

generics ios swift swift-protocols swift2

73
推荐指数
6
解决办法
3万
查看次数

Protocol func返回Self

我有一个协议P,它返回一个对象的副本:

protocol P {
    func copy() -> Self
}
Run Code Online (Sandbox Code Playgroud)

以及实现P的C类:

class C : P {
    func copy() -> Self {
        return C()
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,我是否输入返回值,因为Self我得到以下错误:

无法将类型为"C"的返回表达式转换为返回类型"Self"

我也试过回来了C.

class C : P {
    func copy() -> C  {
        return C()
    }
}
Run Code Online (Sandbox Code Playgroud)

这导致以下错误:

非终结类'C'中的方法'copy()'必须返回Self以符合协议'P'

没有什么可行的,除了我前缀class Cfinalie 的情况:

final class C : P {
    func copy() -> C  {
        return C()
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,如果我想继承C,那么什么都行不通.有没有办法解决?

protocols subclassing swift swift-protocols

70
推荐指数
5
解决办法
2万
查看次数

为什么我不能在Swift中使用let in protocol?

我对Swift中有关var的使用和关键字{get set}的协议有疑问.

来自Apple文档:

如果协议要求属性可获取和可设置,则不能通过常量存储属性或只读计算属性来满足该属性要求.如果协议只要求属性可以获取,那么任何类型的属性都可以满足要求,如果这对您自己的代码有用,则该属性也可以设置.

属性要求始终声明为变量属性,前缀为var关键字.Gettable和settable属性通过在类型声明后写入{get set}来指示,并且通过编写{get}来指示gettable属性.

我无法理解为什么我不能使用let.只有get的协议中的var不只是一个let

像这样的东西:

protocol someProtocol 
{
   var someProperty: String { get }
}
Run Code Online (Sandbox Code Playgroud)

它不会只是:

protocol someProtocol 
{
   let someProperty: String
}
Run Code Online (Sandbox Code Playgroud)

我错过了什么?

protocols getter-setter ios swift swift-protocols

59
推荐指数
2
解决办法
2万
查看次数

我可以在协议中使用init函数吗?

当我尝试以这种方式实现我的协议时:

protocol Serialization {
    func init(key keyValue: String, jsonValue: String)
}
Run Code Online (Sandbox Code Playgroud)

我收到一个错误说:函数声明中的预期标识符.

为什么我收到此错误?

protocols initialization swift swift-protocols

55
推荐指数
2
解决办法
3万
查看次数

Swift中的泛型数组

我一直在玩不同类型的泛型类数组.用一些示例代码解释我的问题最容易:

// Obviously a very pointless protocol...
protocol MyProtocol {
    var value: Self { get }
}

extension Int   : MyProtocol {  var value: Int    { return self } }
extension Double: MyProtocol {  var value: Double { return self } }

class Container<T: MyProtocol> {
    var values: [T]

    init(_ values: T...) {
        self.values = values
    }

    func myMethod() -> [T] {
        return values
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,如果我尝试创建一个像这样的容器数组:

var containers: [Container<MyProtocol>] = []
Run Code Online (Sandbox Code Playgroud)

我收到错误:

协议"MyProtocol"只能用作通用约束,因为它具有Self或关联类型要求.

要解决这个问题,我可以使用[AnyObject]:

let containers: [AnyObject] …
Run Code Online (Sandbox Code Playgroud)

arrays generics swift swift-protocols

52
推荐指数
2
解决办法
3万
查看次数

Swift 2在协议扩展中使用变异函数时出错"无法在不可变值上使用变异成员:'self'是不可变的

不知道这里发生了什么,这似乎应该是非常直接的.我有一个可变var的协议,一个带有变异函数的扩展.在testClass.testFunc中出现问题,当我尝试使用扩展中声明的mtkAnimQueAppend时,我得到这个错误:"不能在不可变值上使用变异成员:'self'是不可变的.

protocol MTKAnimateValueDelegate {
    var mtkAnimQue:[MTKAnimateValue]? {get set}
}

extension MTKAnimateValueDelegate {
    ///Adds element to que
    mutating func mtkAnimQueAppend(element:MTKAnimateValue) {

        if mtkAnimQue != nil {
          mtkAnimQue?.append(element)
        } else {
          mtkAnimQue = [element]
        }
    }
}

class testClass: MTKAnimateValueDelegate {

  var mtkAnimQue:[MTKAnimateValue]?

  func testFunc() {
    var animValue = MTKAnimateValue(fromValue: 10, toValue: 20, inSeconds: 2)
    animValue.isAnimating = true
    mtkAnimQueAppend(animValue) //ERROR: "Cannot use mutating member on immutable value: 'self' is immutable
  }

}
Run Code Online (Sandbox Code Playgroud)

protocols swift-extensions swift-protocols ios9 swift2

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

swift我应该使用协议还是协议:class

我已经设置了一个协议,将一些信息发送回以前的VC.

我这样定义它:

protocol FilterViewControllerDelegate: class  {
    func didSearch(Parameters:[String: String]?)
}
Run Code Online (Sandbox Code Playgroud)

但使用时有什么区别:

protocol FilterViewControllerDelegate  {
        func didSearch(Parameters:[String: String]?)
    }
Run Code Online (Sandbox Code Playgroud)

我什么时候应该使用: class协议?

swift swift-protocols

40
推荐指数
4
解决办法
9501
查看次数

为什么协议中的get-only属性要求不能满足符合的属性?

为什么以下代码会产生错误?

protocol ProtocolA {
    var someProperty: ProtocolB { get }
}

protocol ProtocolB {}
class ConformsToB: ProtocolB {}

class SomeClass: ProtocolA { // Type 'SomeClass' does not conform to protocol 'ProtocolA'
    var someProperty: ConformsToB

    init(someProperty: ConformsToB) {
        self.someProperty = someProperty
    }
}
Run Code Online (Sandbox Code Playgroud)

这个类似问题的答案是有道理的.但是,在我的示例中,属性是get-only.为什么不能这样做?它是Swift的缺点,还是有一些合理的理由?

swift swift-protocols

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

Swift的hash和hashValue之间的区别

HashableSwift中的协议要求您实现一个名为的属性hashValue:

protocol Hashable : Equatable {
    /// Returns the hash value.  The hash value is not guaranteed to be stable
    /// across different invocations of the same program.  Do not persist the hash
    /// value across program runs.
    ///
    /// The value of `hashValue` property must be consistent with the equality
    /// comparison: if two values compare equal, they must have equal hash
    /// values.
    var hashValue: Int { get }
}
Run Code Online (Sandbox Code Playgroud)

然而,似乎还有类似的属性叫做hash.

hash和之间有什么区别 …

hash swift swift-protocols

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

如何以通用方式调用协议上的静态函数?

是否有必要在协议上声明静态函数?使用协议的客户端必须在符合协议的类型上调用该函数吗?这打破了不必知道符合IMO协议的类型的想法.有没有办法以一种我不必知道符合我的协议的实际类型的方式调用协议上的静态函数?

protocols swift swift-protocols

23
推荐指数
4
解决办法
1万
查看次数