ios - Assigning array property is always going to crash EXC_BAD_ACCESS -


i cannot understand why following code every time crashes on xcode 6 gm using swift. me understand issue?

thank in advance.

optionstoselect.swift

import foundation  struct optiontoselect {     var value :     var desc : string     var available : bool } 

someclass.swift

import foundation  class someclass {     var items = array<optiontoselect>() } 

viewcontroller.swift

    override func viewdidload() {         super.viewdidload()         var c = someclass()         c.items = [ /// <------------- __ exc_bad_access here, why?! __             optiontoselect(value: 1, desc: "a", available: true),             optiontoselect(value: 2, desc: "b", available: true)         ]     } 

edit 1 on twitter got answer it's related any, , indeed. why?

the compiler still has various problems any, short answer "bug in swift." has trouble if value protocol. suspect has trouble figuring out how make copy; that's guess.

but should avoid using any here. never should use any. in cases, want generic:

struct optiontoselect<t> {   let value : t   let desc : string   let available : bool } 

(the way you're using it, let seems more appropriate var here; need change these values?)

this require entire array have same kinds of values:

var items = [optiontoselect<int>]() 

but that's correct anyway. if isn't; if need mix of values, should consider protocol values might conform to. won't solve problem (swift crash on protocol here too), design better any. if best can type "well, it's something," you're going lot of complicated (and dangerous) down-casting code. you'll fight every time turn around.

to use protocol here (or any) can either make optiontoselect class (which easiest answer), or hide problem in box:

struct optiontoselect {   let value : anybox   let desc : string   let available : bool }  final class anybox {   let value:   init (_ value: any) { self.value = value } }  [   optiontoselect(value: anybox(1), desc: "a", available: true),   optiontoselect(value: anybox(2), desc: "b", available: true) ] 

(the same technique needed protocol types.)


Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

visual studio 2010 - Connect to informix database windows form application -

android - Associate same looper with different threads -