Sunday, 15 June 2014

ios - Error when adding object to NSMutableArray in Swift -



ios - Error when adding object to NSMutableArray in Swift -

i'm trying build function in app through users can add together custom specifications products. specifications should added in nsmutablearray, when want add together object it, exc_bad_access error. here's code:

var specs = nsmutablearray() func addspec () { var alert = uialertcontroller(title: "nieuwe specificatie", message: "vul hier een naam voor de nieuwe specificatie in:", preferredstyle: uialertcontrollerstyle.alert) alert.addtextfieldwithconfigurationhandler(configurationtextfield) alert.addaction(uialertaction(title: "opslaan", style:uialertactionstyle.default, handler: {(uialertaction) in self.specs.addobject(self.newspecificationtitle.text) })) alert.addaction(uialertaction(title: "annuleren", style:uialertactionstyle.default, handler: {(uialertaction) in println("annuleren") })) self.presentviewcontroller(alert, animated: true, completion:nil) }

can see i'm doing wrong?

i'm not sure you're doing in or configurationtextfield block. next code works me in xcode 6 dp2

class viewcontroller: uiviewcontroller { var newspecificationtitletextfield: uitextfield? var specs = nsmutablearray() @ibaction func addspec(sender: anyobject) { var alert = uialertcontroller(title: "title", message: "message", preferredstyle: .alert) alert.addtextfieldwithconfigurationhandler { textfield in self.newspecificationtitletextfield = textfield } alert.addaction(uialertaction(title: "ok", style:.default, handler: {(uialertaction) in self.specs.addobject(self.newspecificationtitletextfield!.text) println("\(self.specs)") })) alert.addaction(uialertaction(title: "cancel", style:.cancel, handler: {(uialertaction) in println("cancel") })) self.presentviewcontroller(alert, animated: true, completion:nil) } }

ios nsmutablearray swift exc-bad-access

No comments:

Post a Comment