associated-types

    0熱度

    2回答

    我正在閱讀由Apple發佈的Swift書。我有XCode 7.2.1 下面的代碼給出了一個編譯錯誤。我在遊樂場使用了代碼。 protocol Container { associatedtype ItemType mutating func append(item: ItemType) var count: Int { get } subscript(i:

    0熱度

    2回答

    我試圖這樣做: protocol HasElement { associatedtype ItemType func getElement() -> ItemType func setElement(element: ItemType) } class Element {} class BarElement: Element {} class Foo: N

    4熱度

    2回答

    我想強制關聯類型爲Self,但編譯器沒有它。 這裏就是我想要去編譯: protocol Protocol { // Error: Inheritance from non-protocol, non-class type 'Self' associatedtype Type: Self } 你可能會問,爲什麼不使用Self代替相關類型的?僅僅因爲我不能:關聯類型是從父協議

    2熱度

    1回答

    我無法瞭解關聯的類型。我的問題代碼: trait Fooer { fn foo(&self); } trait FooStore { type T: Fooer; fn store_foo(&self, fooer: Self::T); } #[allow(dead_code)] struct DB {} impl FooStore for DB {

    0熱度

    1回答

    當我嘗試編譯下面的代碼時,出現了分段錯誤。我試圖在CellUpdater結構上創建一個類型受限擴展,該結構訪問一個屬性,該屬性的類型是在泛型類型的關聯類型上定義的。不知道我是否做錯了什麼,或者它是否是Swift編譯器的限制,有什麼想法? protocol CellUpdaterType { func generateDetailsDrillDownController(index: In

    0熱度

    1回答

    我嘗試獲得更通用的東西,但是在嘗試使用關聯類型時失敗。鑑於是Shape特質和Renderer類型,它試圖返回一個Builder爲特定類型的形狀。 形狀 #[derive(Clone, Copy)] pub struct Rectangle{ pub origin: Point, pub height: usize, pub width: usize, } pu

    1熱度

    1回答

    我有下面的代碼: protocol NextType { associatedtype Value associatedtype NextResult var value: Value? { get } func next<U>(param: U) -> NextResult } struct Something<Value>: NextType

    2熱度

    2回答

    我有一些實現Resource協議的結構。這定義它們必須具有符合ExtendedInfo協議的變量extendedInfo,以便通過init(json: [String: AnyObject]提供使用json初始化它們的方式。我試圖提供一種方法來動態實例化這些,使用JSON,提供正確類型的ExtendedInfo並將其分配給結構的extendedInfo變量。然而,試圖通過他們的dynamicTyp

    6熱度

    1回答

    我有一個協議我的swift代碼庫我有協議與關聯的類型和兩種方法。這兩種方法都爲協議的關聯類型定義了不同的通用約束。我想使結構符合兩個協議,但有兩種不同的關聯類型。 protocol Convertable { associatedtype TargetType func convert() -> TargetType } func show<T : Convertable

    3熱度

    1回答

    在Swift 2.3中數組是否定義了元素關聯類型? 它必須定義它,因爲它實現了具有元素和關聯類型的GeneratorType。 Dictionary和Set都定義了元素,但是Array在哪裏定義它? 數組有一個稱爲元素的泛型類型,具有泛型類型是否滿足GeneratorType協議? 我在操場上試過這個,它對我沒有幫助。 例如 protocol Animal{ associatedtype