2016-03-24 8 views
1

Ich möchte eine Variable, die Protokoll entspricht, aber der schnelle Compiler sagt mir das Protokoll nicht bestätigt.Swift Protokollkonformität

protocol A {} 
protocol B { 
    var a : A { get } 
} 
class AA : A {} 
// Type 'BB' does not conform to protocol 'B' 
class BB : B { 
    let a = AA() 
} 

Antwort

3

Sie sind nicht konform mit dem Protokoll, weil Sie ist a nicht eindeutig als A getippt, sondern als AA abgeleitet. Mach es explizit.

protocol A {} 
protocol B { 
    var a : A { get } 
} 
class AA : A {} 

class BB : B { 
    let a: A = AA() // Explicitly typed here. 
} 

Die Tatsache, dass Sie var im Protokoll und let bei der Umsetzung verwendet haben, ist ein roter Hering, wie die var nur ein get sind, und haben keine set.