Использовать протокол, а не структуру в качестве расширения (необязательного массива)? [дубликат]IOS

Программируем под IOS
Anonymous
Использовать протокол, а не структуру в качестве расширения (необязательного массива)? [дубликат]

Сообщение Anonymous »


Say you have

struct Screw: Codable { let id: String .. more } struct Nail: Codable { let id: String .. more } then

struct Parts: Codable { var Screws: [Screw]? var Nails: [Nail]? } and an extension on the optional array

extension Optional { mutating func blah() where Wrapped == [Screw] { if self == nil { self = [] } // we use the .id property .. if self!.count > 13 && self![13].id == 666 { self?.remove(at: 13) } } } you can then conveniently

stuff.parts.Screws.blah() However if I sensibly

protocol IDable { var id: Int { get } } struct Screw: Codable, IDable { .. struct Nail: Codable, IDable { .. It seems you can NOT do this,

extension Optional { mutating func blah() where Wrapped == [IDable] { .. Because at compile time

stuff.parts.Screws.blah() Instance method 'blah()' requires the types '[Screw]' and '[any IDable]' be equivalent

Is there a way to achieve this?
The solution is ...
mutating func blah() where Wrapped == [Screw] { becomes

mutating func blah() where Wrapped == [T] { or

mutating func bleh(item: Screw) where Wrapped == [Screw] { becomes

mutating func bleh(item: T) where Wrapped == [T] { Very nice.


Источник: https://stackoverflow.com/questions/780 ... -extension

Вернуться в «IOS»