-2

I have created a protocol as below

protocol FullyNamed {
    var fullName: String { get }
}

Below my class which confirm this protocol

class Starship: FullyNamed {
    var prefix: String?
    var name: String
    init(name: String, prefix: String? = nil) {
        self.name = name
        self.prefix = prefix
    }
    var fullName: String {
        return (prefix != nil ? prefix! + " " : "") + name
    }
}
var ncc1701 = Starship(name: "Enterprise", prefix: "USS")
// ncc1701.fullName is "USS Enterprise"

I realiaze that we use protcol as a listners when we use function in them Now when i add property here so why i will ever use a property in a protcol.Please answer

EDIT: As reading in the This link so is it just developer don't make mistake & must include confirm protocol because those objects will definitely have a 'name' ?

Community
  • 1
  • 1
iOSGuy
  • 171
  • 1
  • 14
  • 2
    Possible duplicate of [Can I have a Swift protocol without functions](http://stackoverflow.com/questions/39154387/can-i-have-a-swift-protocol-without-functions) – Hamish Feb 01 '17 at 11:40

1 Answers1

0

If you have property in protocol and one of your classes confirm this protocol you can be sure that in your class also has propery that protocol have.

If you have two different classes that confirm this protocol and then in some function in other clas you have:

func makeSomething(item:YourProtocolHere){
    //you can be sure and use without any cast
    item.protocolproperty
}