2

I am new to apple Swift.I am trying to build a demo app based on this tutorial. I have found that the app is not working. After several trial and fails, I have found that if I change the line of code:

func textFieldDidEndEditing(textField: UITextField) {

to

func textFieldDidEndEditing(_ textField: UITextField) {

I am able to run the code properly.

I want to know what happens by adding the _ as one of the arguments. Both of the two methods will not result in compilation errors. But the first one does give warnings like:

ViewController.swift:35:10: Instance method 'textFieldDidEndEditing(textField:)' 
nearly matches optional requirement 'textFieldDidEndEditing' of protocol 'UITextFieldDelegate'

Since the first one is given by apple tutorial, I am not sure whether it is a mistake. Can somebody clarify my doubts?

Nirav D
  • 71,513
  • 12
  • 161
  • 183
user3588276
  • 137
  • 1
  • 8
  • I am using the latest version of Xcode – user3588276 Sep 29 '16 at 09:55
  • You can have the correct answer here: [What's the _ underscore representative of in Swift References?](http://stackoverflow.com/questions/24437388/whats-the-underscore-representative-of-in-swift-references) – Dave M. Sep 29 '16 at 10:00

3 Answers3

2

I think you are using Swift 3.0. The tutorial you are following might be a Swift 2.2 or Swift 2.3 version. It is nothing but a Syntax change from what I have observed. Even I faced this situation yesterday when I have updated to Xcode 8.0

SaiPavanParanam
  • 416
  • 6
  • 16
1

From Swift 3 First argument labels are also the standard for initializers.

Swift 3 Evolution

First parameter declarations will match the existing behavior of the second and later parameters. All parameters, regardless of position, will behave uniformly. This will create a simple, consistent approach to parameter declaration throughout the Swift programming language and bring method and function declarations in-sync with initializers, which already use this standard.

For example

func foo(x: Int, y: Int) 

will declare foo(x:y:) and not foo(_:,y:).

The existing external label overrides will continue to apply to first parameters. You establish external parameter names before the local parameter name it supports, separated by a space. For example,

func foo(xx x: Int, yy y: Int) //declares foo(xx:yy:) and

func foo(_ x: Int, y: Int) //explicitly declares foo(_:y:)

Read more about first parameter in SE-0046

Nirav D
  • 71,513
  • 12
  • 161
  • 183
0

if you want multiple parameters with no external name, you must use the ” _ ” underscore character as its external name:

Ram
  • 961
  • 6
  • 14