Saturday, May 14, 2016

How do you fix this argument label error in the SWIFT program?

While trying to compile and run the following (see link at the bottom of post) program using SwiftFor Windows,
---
func sayHello(personName: String) -> String {
        let greeting = "Hello, " + personName + "!"
        return greeting
    }

print(sayHello("Jay"))

--------
The SwiftForWindows compiler spit out this error:

SwiftError_00

The compiler did indicate the location and the missing label.  The change was made in the print statement by providing the missing label (personName)

The program was modified as shown:
-------------------
func sayHello(personName: String) -> String {
        let greeting = "Hello, " + personName + "!"
        return greeting
    }

print(sayHello(personName:"Jay"))

---
With this the compiler produced no errors.


SwiftError_01

Now after hiting the Run button, the program produced the following response:


SwiftError_02
This sample was taken from SWIFT ver2.2 documentation:

https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Functions.html#//apple_ref/doc/uid/TP40014097-CH10-ID158Is the error due to differences in the SWIFT compiler version?

No comments: