ios - Apple Swift: Type Casting Generics -
ios - Apple Swift: Type Casting Generics -
i'm writing swift code have array containing generic type:
let _data: array<t> = t[]() later in code need determine type stored in array. tried using type casting technique described in documentation (although not used generics).
switch self._data { case allow doubledata array<double>: // doubledata case allow floatdata array<float>: // floatdata default: homecoming nil // if info type unknown homecoming nil } the above switch statement results in next error upon compilation:
while emitting ir sil function @_tfc19adder_example___mac6matrix9transposeus_7element__fgs0_q__ft_gsqgs0_q___ 'transpose' @ /code.viperscience/adder/src/adder library/matrix.swift:45:3 :0: error: unable execute command: segmentation fault: 11 :0: error: swift frontend command failed due signal (use -v see invocation) command /applications/xcode6-beta2.app/contents/developer/toolchains/xcodedefault.xctoolchain/usr/bin/swift failed exit code 254anybody know how can cast generic info actual type in order take specific action?
suppose have array of buttons:
let views: [nsview] = [nsbutton(), nsbutton(), nsbutton()] you can utilize these casts:
let viewsarebuttons = views [nsbutton] // returns true allow buttonsforsure = views as! [nsbutton] // crashes if wrong allow buttonsmaybe = views as? [nsbutton] // optionally set if seek utilize in switch case below, not work. compiler (swift 1.2 xcode 6.3b1) says: "downcast pattern of type [nsbutton] cannot used."
switch views { case allow buttons [nsbutton]: println("buttons") default: println("something else") } call limitation. file radar utilize case. swift team seams listening feedback. if want work, can define own pattern matching operator. in case this:
struct buttonarray { } allow isbuttonarray = buttonarray() func ~=(pattern: buttonarray, value: [nsview]) -> bool { homecoming value [nsbutton] } then works:
switch views { case isbuttonarray: println("buttons") // gets printed. default: println("something else") } try in playground. hope helps!
ios generics swift
Comments
Post a Comment