4

I have a basic question about swift function calling syntax. I have read documentation but not able to figure it out. So decided to put a query over here.

A piece of code i wrote

    var string1 = NSString().stringByAppendingString("First string")
    var string2 = NSString.stringByAppendingString("Second string")

    println(string1)
    println(string2)

Both string have same function calling but return type is different. And only difference here is (). I got out put like

First string

(Function)

Question is why its not giving a warning/error. Is it that var string2 holds method body of stringByAppendingString? Whats going on, New swift developer like me can easily make a this type of typo mistake and not able to figure out.

Can you please explain why its not return value?

1 Answer 1

6

This happens because swift methods are curried functions (you can find detailed explanation in Ole Begemann's post).

So what you actually got in the following line:

var string2 = NSString.stringByAppendingString("Second string")

is a function that takes a string as parameter and returns the result of

"Second string".stringByAppendingString(parameter)

You can check that by calling string2 as an ordinary function:

string2("123")
// Prints: "Second string123"
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.