小编Rah*_*iya的帖子

协议只能用作通用约束,因为它具有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万
查看次数

Downcast Generic AnyObject to Protocol Associated Type Self.Model

我正在开发一个Restofire库,我想在其中保留一个配置对象.我想在配置对象中有一个ResponseSerializer,但事情是ResponseSerializer是一个通用的.

public struct Configuration<M> {

    /// The Default `Configuration`. 
    static let defaultConfiguration = Configuration<AnyObject>()

    /// The base URL. `nil` by default.
    public var baseURL: String!

    /// The `ResponseSerializer`
    public var responseSerializer: ResponseSerializer<M, NSError> = AlamofireUtils.JSONResponseSerializer()

    /// The logging, if enabled prints the debug textual representation of the 
    /// request when the response is recieved. `false` by default.
    public var logging: Bool = false

}
Run Code Online (Sandbox Code Playgroud)

我用baseUrl设置了defaultConfiguration Configuration.defaultConfiguration.baseUrl = "http://httpbin.org/"

我有一个带有associatedType要求的协议,它使用defaultConfiguration作为默认实现.但我需要将通用AnyObject更改为associatedType模型,以便配置对象的responseSerializer返回类型Model.

public protocol Configurable {

    associatedtype Model

    /// The Restofire …
Run Code Online (Sandbox Code Playgroud)

generics ios swift swift-protocols

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

从另一个框架扩展Swift协议时出现意外行为(Restofire)

我有两个框架

首先 - Restofire.它有一个协议ResponseSerializer与扩展.

public protocol ResponseSerializable {

    /// The type of object returned in response.
    associatedtype Model

    /// The `Alamofire.ResponseSerializer`.
    var responseSerializer: ResponseSerializer<Model, NSError> { get }

}

extension ResponseSerializable {

    /// `CustomJSONResponseSerializer`
    public var responseSerializer: ResponseSerializer<Model, NSError> {
        return AlamofireUtils.JSONResponseSerializer()
    }

}
Run Code Online (Sandbox Code Playgroud)

第二 - Restofire-Gloss.它具有对Restofire框架中符合Decodable的模型的协议的扩展.

public extension ResponseSerializable where Model: Decodable {

    /// `GLOSSResponseSerializer`
    public var responseSerializer: ResponseSerializer<Model, NSError> {
        return GlossUtils.GLOSSResponseSerializer()
    }

}

public extension ResponseSerializable where Model: CollectionType, Model.Generator.Element: Decodable {

    /// `GLOSSResponseSerializer`
    public …
Run Code Online (Sandbox Code Playgroud)

ios swift swift-protocols swift2

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

如何检查两个[String:Any]是否相同?

有没有办法检查两个[String:Any]是否相同?

let actual: [[String: Any]] = [
    ["id": 12345, "name": "Rahul Katariya"],
    ["id": 12346, "name": "Aar Kay"]
]
var expected: [[String: Any]]!

if actual == expected {
    print("Equal")
}
Run Code Online (Sandbox Code Playgroud)

基本上我希望Dictionary符合Swift 3中的Equatable协议.

swift swift3

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

通用方法覆盖在swift中不起作用

有一个协议Printable和一个来自第三方的结构打印机.

protocol Printable {}

struct Printer {

    static func print<T>(object: T) -> String {
        return "T"
    }

    static func print<T: Printable>(object: T) -> String {
        return "Printable"
    }

}
Run Code Online (Sandbox Code Playgroud)

现在我正在制作一个通用的

struct Generic<T> {

    var args: T

    func display() {
        print(Printer.print(args))
    }

}
Run Code Online (Sandbox Code Playgroud)

和两个结构

struct Obj {}
struct PrintableObj: Printable {}
var obj = Generic(args: Obj())
var printableObj = Generic(args: PrintableObj())
Run Code Online (Sandbox Code Playgroud)

当我在它们上面调用显示功能时.

obj.display()
Run Code Online (Sandbox Code Playgroud)

显示T.

printableObj.display()
Run Code Online (Sandbox Code Playgroud)

显示T但我希望它打印"可打印"

我能想到的一个解决方案是拥有两种不同的泛型

struct Generic<T>
struct PrintableGeneric<T: Printable>
Run Code Online (Sandbox Code Playgroud)

是否有任何其他解决方案,而无需更改Printable协议和Printer结构.

generics swift swift2

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

Playground 执行失败:错误:无法查找符号

我正在使用 Xcode 7.3。我已经将 Playground 和 Frameworks 放在同一个工作区中并构建了框架。我仍然收到此错误

Playground execution failed: error: Couldn't lookup symbols:  
_RestofireVersionNumber
Run Code Online (Sandbox Code Playgroud)

Playground 执行失败:错误:无法查找符号

如何解决这个问题?

ios swift swift-playground

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

具有关联类型要求和默认实现的Swift协议

很长一段时间以来,我一直在努力使用Swift协议和关联类型.我再次使用基本功能来真正理解出现了什么问题,我在Rob Napier的Swift Protocols中使用了相关类型要求的TypeErasure 文章,但我仍然没有运气.

找到下面的代码

// An Animal can eat
protocol Animal {
    associatedtype Food
    func feed(food: Food) -> Void
}

struct AnyAnimal<Food>: Animal {
    private let _feed: (Food) -> Void
    init<Base: Animal where Food == Base.Food>(_ base: Base) {
        _feed = base.feed
    }
    func feed(food: Food) { _feed(food) }
}

// Kinds of Food
struct Grass {}

struct Cow: Animal {
    func feed(food: Grass) { print("moo") }
}

struct Goat: Animal {
    func feed(food: Grass) { print("bah") } …
Run Code Online (Sandbox Code Playgroud)

generics ios swift swift-protocols

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

CoreData - NSManagedObject子类的好处

我试图插入到CoreData而不创建NSManagedObject的子类.但我的应用程序崩溃与NSManagedObject setValue:forUndefinedKey"名称"在类别中.

    let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
    let entitiyDesc = NSEntityDescription()
    entitiyDesc.name = "Category"
    guard let data = model.data else { return }
    for d in data {
        let managedObject = NSManagedObject.init(entity: entitiyDesc, insertIntoManagedObjectContext: managedObjectContext)
        managedObject.setValue(d.name, forKey: "name")
    }
    try! managedObjectContext.save()
Run Code Online (Sandbox Code Playgroud)

子类化NSManagedObjects有什么好处

core-data ios swift

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