2

Here's code:

section.name = (alert.textFields[0] as! UITextField).text

I have an error Cannot assign a value of type String! to a value of type String

section is a Core Data NSManagedObject subclass with required value of name.

1
  • Did you try unwrapping .text ? Commented May 17, 2015 at 12:38

2 Answers 2

2

You need to check alert.textFields isn't nil before you can use the subscript. Which you're not doing in your code sample.

In my opinion, the best way to get the value of text is to use optional binding, like so:

if let textField = alert.textFields?[0] as? UITextField {
    section.name = textField.text
}

However, if you're absolutely sure your alert has a text field, should see @Arun Gupta's answer.

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

Comments

1

Try using this:

section.name = (alert.textFields![0] as UITextField).text

For Swift 1.2

section.name = (alert.textFields![0] as! UITextField).text

Comments

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.