简体   繁体   English

可选只读变量的Swift协议默认实现

[英]Swift protocol default implementation for optional readonly variables

I have the following piece of code, the protocol MyDisplayable has three optional String s, and I have a default implementation of the protocol via extension. 我有以下代码,协议MyDisplayable具有三个可选的String ,并且我具有通过扩展名的协议的默认实现。 My question is, since I'm sure the extension returns the three strings, is there a way I can use them as non-optional and is there any risk if some other implementation overwrites it? 我的问题是,由于我确定扩展名会返回这三个字符串,因此有没有办法将它们用作非可选字符串,并且如果其他实现将其覆盖,是否会有任何风险? (see the question points 1 and 2 in code below) (请参见下面的代码中的问题1和2)

Thanks a lot! 非常感谢!

protocol MyDisplayable {
    var displayName: String? { get }
    var shortDescription: String? { get }
    var longDescription: String? { get }
}

protocol MyObject : MyDisplayable, CustomStringConvertible {
}

extension MyObject {
    var displayName: String? {
        return "noname"
    }

    var shortDescription: String? {
        return "something can't be described"
    }

    var longDescription: String? {
        return "no way to describe it further"
    }

    var description: String {
        // **1. is there a way to use the strings as if they are non-optional?**
        // **2. is it a problem if another class implements the protocol and returns `nil` for any of the strings, but here they are force unwrapped?**
        return "\(displayName!): \(shortDescription!)\n\(longDescription!)"
    }
}

class Something : MyObject {
}

let something = Something()
print("Something: \(something)")

默认实现中的条件展开怎么样?

return "\(displayName ?? "" ): \(shortDescription ?? "" )\n\(longDescription ?? "")"

Unfortunately, it's not possible to treat a declared optional as a non-optional. 不幸的是,不可能将声明的可选内容视为非可选内容。 You have declared those strings as optional in your protocol, thus when you implement that protocol they stay optional. 您已在协议中将这些字符串声明为可选,因此在实现该协议时,它们仍为可选。

However, you can use getter-setter to ensure that your variables always store some value even when they are declared as optional. 但是,您可以使用getter-setter来确保您的变量即使声明为可选变量也始终存储一些值。

I'll elaborate with some code : 我将详细说明一些代码:

protocol MyDisplayable {
    var displayName: String? { get set }
    var shortDescription: String? { get set }
    var longDescription: String? { get set }
}

protocol MyObject : MyDisplayable, CustomStringConvertible {
}

extension MyObject {
    var displayName: String? {
        get {
            return "noname"
        }
        set {
            newValue ?? ""
        }
    }

    var shortDescription: String? {
        get {
            return "something can't be described"
        }
        set {
            newValue ?? ""
        }
    }

    var longDescription: String? {
        get {
            return "no way to describe it further"
        }
        set {
            newValue ?? ""
        }
    }

    var description: String {
        // **1. is there a way to use the strings as if they are non-optional?**
        // **2. is it a problem if another class implements the protocol and returns `nil` for any of the strings, but here they are force unwrapped?**
        return "\(displayName!): \(shortDescription!)\n\(longDescription!)"
    }
}

class Something : MyObject {
}

let something = Something()
print("Something: \(something)")

Now even if some other class overwrites a nil value to those strings they will return empty string "". 现在,即使其他类将nil值覆盖到了这些字符串,它们也将返回空字符串“”。 They will still be optional as they are declared optional, but now they will always have a non-nil value. 它们仍然是可选的,因为它们被声明为可选,但是现在它们将始终具有非null值。

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

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