0

I'm pretty new in Swift so i'm asking here not why it doesn't work but why it does. I expected something different. I have three class, one called User.swift with User variables, get methods and constructor, another called UserList.swift with an array and a custom method to add objects to it and another in which simply i use this method to add User to this array. As you can see, when i call listManager.addUser(...) i expected to write between parenthesis something like this: name: "random name", surname: "random surname", age: 0 but this give me a compilation error saying "Extraneous argument label 'name:' in call". If i delete "name:" it works, but if i delete "surname:" or "age:" it doesn't. I'll put here my code (it works as i said, i have only to understand why).

User.swift

import Foundation

class User: NSObject {

private var name: String
private var surname: String
private var age: Int

init(name: String, surname: String, age: Int){
    self.name = name
    self.surname = surname
    self.age = age;
}

func getName() -> String{
    return self.name
}

func getSurname() -> String{
    return self.surname
}

func getAge() -> Int{
    return self.age
}

}

UserList.swift

import UIKit

var listManager: UserList = UserList()

class UserList: NSObject {

var userList = [User]()

func addUser(name: String, surname: String, age: Int){
    userList.append(User(name: name, surname: surname, age: age))
}

}

Class in which i use addUser()

listManager.addUser("random name", surname: "random surname", age: 0)

As you can see there isn't "name:" label

2 Answers 2

1

Because that is the rule for external parameter names for methods. From the document:

Specifically, Swift gives the first parameter name in a method a local parameter name by default, and gives the second and subsequent parameter names both local and external parameter names by default. This convention matches the typical naming and calling convention you will be familiar with from writing Objective-C methods, and makes for expressive method calls without the need to qualify your parameter names.

If you want to call it like:

listManager.addUser(name: "random name", surname: "random surname", age: 0)

You have to declare addUser like:

func addUser(#name: String, surname: String, age: Int){
//           ^ This forces the external parameter name
    // ...
}
Sign up to request clarification or add additional context in comments.

2 Comments

Note that, Swift adopts different rules for functions. see stackoverflow.com/a/26936047/3804019
Perfect explanation, also for the link you added in the comment. Thanks.
0

This seems to be a bug when writing function with multiple parameters without external parameter names. Normally in the way how you declared your function you should expect to use it as following:

listManager.addUser("random name", "random surname", 0)

but currently only the first parameter works as expected. Strangely if you use shorthand external parameter names and declare function as follow:

func addUser(#name: String, #surname: String, #age: Int){
    userList.append(User(name: name, surname: surname, age: age))
}

you get a warning saying that every parameter after the first one is already a keyword argument name so this is definitely a bug

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.