简体   繁体   English

是否可以动态创建swift协议的可选和非可选版本?

[英]Is it possible to create an optional and non-optional version of a swift protocol dynamically?

I need advice on the best approach I should take. 我需要有关最佳方法的建议。

I have lots a data from a server I need to parse in convert into structs. 我有很多来自服务器的数据,需要将其解析为转换为结构。 So let's say I am getting seller information from a server, the struct might look like this: 因此,假设我正在从服务器获取seller信息,该结构可能如下所示:

struct Seller {
    var firstName: String?
    var lastName: String?
    var address: String?
}

I have created a protocol for this and made my seller struct adopt it. 我为此创建了一个协议,并让我的卖方结构采用了该协议。 So my code now looks like this: 所以我的代码现在看起来像这样:

protocol SellerProtocol {
    var firstName: String {get set}
    var lastName: String {get set}
    var address: String {get set}
}

extension Seller: SellerProtocol {
    var firstName: String?
    var lastName: String?
    var address: String?
}

Question: is there a way to make all of the variables in this protocol non-optional dynamically? 问题:是否有一种方法可以使该协议中的所有变量动态非可选?

The thing is, I have many such optional protocols but, I also need to declare a non-optional version of the exact same protocol so that other classes can conform to it. 问题是,我有很多这样的可选协议,但是,我还需要声明一个完全相同协议的非可选版本,以便其他类可以遵循该协议。 I only want the structs that interact directly with the server to be of optional type (because the data from server can contain nils), but after I process the information from the server, I want to create a non-optional structure. 我只希望与服务器直接交互的结构为可选类型(因为来自服务器的数据可以包含nils),但是在处理来自服务器的信息之后,我想创建一个非可选结构。

So do I need to maintain two versions of the exact same protocol? 那么,我需要维护完全相同协议的两个版本吗? An optional version, and a non-optional version? 可选版本和非可选版本?

No, there isn't a way to dynamically change a protocol's required properties / methods or the types used therein. 不,没有办法动态更改协议的所需属性/方法或其中使用的类型。 Protocols are contracts that the compiler needs to be able to consider invariant in order to analyze and validate your code. 协议是编译器必须能够考虑不变性以便分析和验证代码的契约 Changing a protocol (or what protocols a type conforms to) at runtime would essentially break the contracts you promised to the compiler at build time. 在运行时更改协议(或类型符合什么协议)实质上会破坏您在构建时向编译器承诺的约定。

It seems to me that your SellerProtocol should not include optionals if a seller is absolutely expected to have those properties within your application. 在我看来,你的SellerProtocol 不应包括自选如果卖家绝对是预计将有你的应用程序中的那些属性。 At the point where you are getting data from the server, why is it required that a transient / temporary representation of possibly nil data conform to that protocol at all? 在从服务器获取数据的那一刻,为什么要求临时/临时表示可能没有数据的数据完全符合该协议? It seems like only if the data is not nil / missing would you then populate a SellerProtocol-conforming type with it. 似乎只有在数据不为零/缺失的情况下,您才可以用它填充符合SellerProtocol的类型。

What you want to do is to declare the protocol as non-optional, then use a failable initializer to create your struct. 您要做的是将协议声明为非可选的,然后使用故障初始化程序创建您的结构。 That way, if you're missing certain properties, object initialization will fail, but if initialization succeeds, you have all the data you need. 这样,如果缺少某些属性,则对象初始化将失败,但是如果初始化成功,则将拥有所需的所有数据。

You can reference this link if you need help creating a failable initializer . 如果您需要创建故障初始化程序的帮助,可以参考此链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM