The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

hedgehog (8) [Avatar] Offline
#1
While re-doing your examples I find out that "connect to an existing method" from the text fields does not work in Xcode 8.2.

I think I figured out the reason for that problem when testing and playing around with your Github version:

The new Xcode generates an @IBAction like this:
@IBAction func convertToKm(_ sender: Any) {
}


I left the second method as it was and it looks like this:
@IBAction func convertToMiles(_ sender: AnyObject) {
}


You see the difference? In older Xcode the sender type was AnyObject. To this method I'm able to connect as you show in your book.

To the current Xcode generated version of an @IBAction (with sender type Any) I'm not able to connect from the text field (there will no "Connect Action" popup appear!!)

Here i got stuck: how to connect a text field to an existing @IBAction with current Xcode?

When I change the type back to AnyObject is works. But this type is not among the possible values to choose when you let Xcode generate the @IBAction method.

You may easily check this with a new project with only a text field and a button. Generate the @IBAction method with dragging from the button and after that try to connect the text fields action to that method. It will only work after you changed to the AnyObject type.
What is the reason for this behavior?

Thank you very much for help. It's so frustrating when you get stuck in such simple workflows at a totally beginner's level... (Hope this is not a pseudo problem again).
Oliver
hedgehog (8) [Avatar] Offline
#2
Sources

Article

Here is a good article on the difference between Any and AnyObject: ANY AND ANYOBJECT at http://en.swifter.tips.

but at this point I don't understand why the @IBAction method defaults into the wrong type declaration.


Swift Commit: Allow 'Any' as an IBAction's sender and an IBOutlet's type. #4320

This is the Commit #4320 in Swift which allows 'Any' as an IBAction's sender and an IBOutlet's type. It dates on 16 Aug this year.
For me, this is not helpful.
Craig Grummitt (50) [Avatar] Offline
#3
Hi Hedgehog,
Great pickup!

Yes, you're right, some decisions were made in Swift 3 to default the Objective-C id type as the Any type, whereas in Swift 2 it was imported as AnyObject. This decision has affected the sender parameter in @IBAction. You can check out the relevant Swift evolution proposal here.

The article you linked to was a good one, but there have been some changes since it was written, such as the default type for id, and that Foundation types such as NSNumber and NSString are no longer implicitly bridged, here's the relevant Swift evolution proposal for that one. I actually wrote a blog post myself on Any vs AnyObject vs NSObject here, hopefully that helps clear up differences.

I'll update the curriculum and the github project in the next week or two, sorry if the error had you confused! You're right, this should be a fairly beginner level thing to do, but you've found yourself snagged down with some more recent changes to the language on a more advanced level. The main thing to take away is, that the @IBAction parameters should be Any!
hedgehog (8) [Avatar] Offline
#4
Thank you!
Can't wait to see (and learn) how we may now connect actions from the interface builder. Especially when you suggest to use the new default of Any instead of AnyObject.