Giorgio Antonioli
Giorgio Antonioli

Reputation: 16224

Add custom object to array with custom method Swift issue

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

Upvotes: 0

Views: 729

Answers (2)

rintaro
rintaro

Reputation: 51911

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
    // ...
}

Upvotes: 1

Zell B.
Zell B.

Reputation: 10296

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

Upvotes: 0

Related Questions