ios - Immutable value type [string] only has mutating members named append -
i'm trying append new store object stores array in user struct...i added mutating func append struct i'm still missing i'm still getting the
immutable value type [store] has mutating members named 'append'
error
import foundation import uikit struct user { var name: string var stores: [store] mutating func append(newstore: store) { stores.append(newstore) } } extension user: printable { var description: string { var printedname = ("\(name string, stores [store])") return printedname } } struct store { var name: string var data: [[string: string]] } extension store: printable { var description: string { var printedstores = ("\(name string, data [[string : string]])") return printedstores } } @ibaction func done(segue: uistoryboardsegue) { let addstoreviewcontroller = segue.sourceviewcontroller as! addstoreviewcontroller let store = addstoreviewcontroller.store if let selectedindex = addstoreviewcontroller.index { stores[selectedindex] = store! } else { if let selecteduser = user { selecteduser.stores.append(store!) } } storetableview.reloaddata() } import uikit class addstoreviewcontroller: uiviewcontroller { var store: store? var index: int? @iboutlet weak var storetextfield: uitextfield! override func viewdidload() { super.viewdidload() if let name = store?.name { storetextfield?.text = name } // additional setup after loading view. } // mark: - navigation override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject?) { let name = storetextfield?.text if segue.identifier == "addaction" { if var store = store { self.store?.name = name! } else { store = store(name: name!, data: [["": ""]]) println(store) } } } }
if let selectedindex = addstoreviewcontroller.index { stores[selectedindex] = store! } else { if let selecteduser = user { selecteduser.stores.append(store!) } }
this appears problem section.
we can both simultaneously fix problem and clean else
code bit:
if let store = store, selectedindex = addstoreviewcontroller.index { stores[selectedindex] = store } else if let store = store, var selecteduser = user { selecteduser.append(store) }
we're able append
correctly, don't have unnecessary level of nesting, , don't have force unwrap (which leads crashes).
Comments
Post a Comment