simplytunde
simplytunde

Reputation: 197

Swift Protocol: what am i doing wrong?

@objc protocol Animal{
typealias ElementType
func getSiblings() ->[ElementType]
func getMother() -> ElementType?
func getFather() -> ElementType?
optional func addSibling(sibling:ElementType)
}    

It keep giving me the error message: method cannot be marked @objc because its result type cannot be represented in Objective-C. Thanks in advance

Upvotes: 2

Views: 2350

Answers (1)

Airspeed Velocity
Airspeed Velocity

Reputation: 40965

I don’t think you can mix @objc protocols and protocols with associated types (i.e. that declare a typealias). You can’t declare a stand-alone variable representing a protocol with an associated type – they exist only for use as generic constraints, and generics are a Swift-only thing. Whereas the purpose of @objc protocols are for use in passing into Objective-C functions. So combining the two doesn’t make sense.

Put it this way – when you pass your Animal type into a non-generic Objective-C class, what will the type of ElementType be?

Upvotes: 2

Related Questions