[swift-users] CustomStringConvertible?

Michael Rogers mprogers at mac.com
Fri Sep 8 13:26:37 CDT 2017


Thanks for your reply, Rien. I just tried this again, and now it seems that either is acceptable. Seems a little weird, but I guess I’d rather have more flexibility than less.

protocol Powerable {
    var isOn:Bool { get set } // Must have get and set
    var usesDC:Bool { get } // Must have get, may have set
    func turnOn()
    func turnOff()
}

class GPS : Powerable, CustomStringConvertible {
    // Either of these work
//    var description: String {
//        return "I am a GPS"
//    }
    var description:String = "I am a GPS"
    var isOn: Bool = false
    var usesDC: Bool = false
    
    // does the obvious
    func turnOn(){
        isOn = true
    }
    
    // does the obvious
    func turnOff() {
        isOn = false
    }
}

print(GPS()) // output: I am a GPS (both cases)

Michael

> On Sep 8, 2017, at 1:21 PM, Rien <Rien at Balancingrock.nl> wrote:
> 
> It cannot be ‘stored’ I would think.
> I always compute my implementations.
> 
> Regards,
> Rien
> 
> Site: http://balancingrock.nl
> Blog: http://swiftrien.blogspot.com
> Github: http://github.com/Balancingrock
> Project: http://swiftfire.nl - An HTTP(S) web server framework in Swift
> 
> 
> 
> 
> 
> 
> 
>> On 08 Sep 2017, at 19:47, Michael Rogers via swift-users <swift-users at swift.org> wrote:
>> 
>> Quick question (for my own edification, as well as to demonstrate to my students the power of this mail list 😉), is CustomStringConvertible a stored property or a computed property? The docs say the latter, but when using Xcode’s fabulous FIX button, it seems to imply it’s a stored property. 
>> 
>> Michael
>> _______________________________________________
>> swift-users mailing list
>> swift-users at swift.org
>> https://lists.swift.org/mailman/listinfo/swift-users
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170908/bf6ddd81/attachment.html>


More information about the swift-users mailing list