0

I just upgraded to Swift 2.0 And Xcode 7 Beta and I am getting the error:

Cannot assign a value of type 'String' to a value of type 'String'

while writing:

 var string = (change[NSKeyValueChangeNewKey]) as! String?
2
  • I would file a radar. The bang in the as! seems necessary because you're forcing a downcast. But you also want to include the ? to indicate it's a String type that may not have a value. This is different from (1) a String that will always have a value, and from (2) a generic type (AnyObject). I see String? as a very distinct, third option. Commented Jun 11, 2015 at 0:47
  • Swift's error codes are really, really bad sometimes. Treat the error message as the compiler saying "Duh... I don't understand!" and proceed from there. Patricks suggestion of optional binding is worth trying. Commented Jun 11, 2015 at 1:16

1 Answer 1

1

I think the as! String? part is the problem.

You should probably be doing this:

if let string = change[NSKeyValueChangeNewKey] as? String {
    // Do something with `string`
}
Sign up to request clarification or add additional context in comments.

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.