0

I'm trying to build a function in my app through which users can add custom specifications to their products. the specifications should be added in an NSMutableArray, but when I want to add an object to it, I get an EXC_BAD_ACCESS error. here's my 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 anybody see what I'm doing wrong?

3
  • is your var specs property or local variable? Commented Jun 19, 2014 at 10:51
  • is self.newSpecificationTitle not nil? Commented Jun 19, 2014 at 10:53
  • Don't use an NSMutableArray(), use an Array Commented Jun 19, 2014 at 12:04

1 Answer 1

1

I'm not sure what you're doing in or where you get your configurationTextField block. But the following code works for 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)

    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks! The only problem I have left is that I apparently can not access the newSpecificationTextField!.text, because it returns nil. do you maybe know how to access the textfield in another way to solve this?
Did you set the newSpecificationTitleTextField to the returned textField in the addTextFieldWithConfigurationHandler callback block?
you need to something like "if let newText = newSpecificationTextField?.text { self.specs.addObject(newText) }" ... this is what Swift is all about - what would it mean to add a nil to an NSMutableArray?
@DavidArve I have added the code you posted in my app. the initial error is gone, but now it sais that the object I'm trying to add is nil. What should I add to your code to get this error away?
Got it! the error was the way in which the textfield.text is passed to the array. it should be: self.specs.addObject(NSString(string: self.newSpecificationTitle.text))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.