我可以在Protocols上使用Swift的map()吗?

问题描述 投票:0回答:1

我有一些模型代码,我有一些Thoughts,我想读取和写入plists。我有以下代码:

protocol Note {
    var body: String { get }
    var author: String { get }
    var favorite: Bool { get set }
    var creationDate: Date { get }
    var id: UUID { get }
    var plistRepresentation: [String: Any] { get }
    init(plist: [String: Any])
}

struct Thought: Note {
    let body: String
    let author: String
    var favorite: Bool
    let creationDate: Date
    let id: UUID
}

extension Thought {
    var plistRepresentation: [String: Any] {
        return [
            "body": body as Any,
            "author": author as Any,
            "favorite": favorite as Any,
            "creationDate": creationDate as Any,
            "id": id.uuidString as Any
        ]
    }

    init(plist: [String: Any]) {
        body = plist["body"] as! String
        author = plist["author"] as! String
        favorite = plist["favorite"] as! Bool
        creationDate = plist["creationDate"] as! Date
        id = UUID(uuidString: plist["id"] as! String)!
    }
}

对于我的数据模型,然后在我的数据写入控制器中我有这个方法:

func fetchNotes() -> [Note] {
    guard let notePlists = NSArray(contentsOf: notesFileURL) as? [[String: Any]] else {
        return []
    }
    return notePlists.map(Note.init(plist:))
}

由于某种原因,行return notePlists.map(Note.init(plist:))给出错误'map' produces '[T]', not the expected contextual result type '[Note]'但是,如果我用return notePlists.map(Thought.init(plist:))替换该行我没有问题。显然,我无法映射协议的初始化程序?为什么不,什么是替代解决方案?

swift protocols swift-protocols
1个回答
0
投票

如果您希望有多种类型符合Note,并且想知道它存储在您的词典中的哪种类型的注释,则需要使用所有注释类型向您的协议添加枚举。

enum NoteType {
    case thought 
}

将其添加到您的协议中。

protocol Note {
    var noteType: NoteType { get }
    // ...
}

并将其添加到您的Note对象:

 struct Thought: Note {
     let noteType: NoteType = .thought
     // ...
  }

这样,您可以从字典中读取此属性并相应地映射它。

© www.soinside.com 2019 - 2024. All rights reserved.