Halie Park
Halie Park

Reputation: 1

Cannot invoke the initializer for type '(name: String, age: Int)' with no argument

Just start to learn Swift 3.0... follow typing of the tutorial, but with this code snippet just keep getting following error:

var Student = (name: String, age: Int)()       

Cannot invoke the initializer for type '(name: String, age: Int)' with no argument

What's the problem with this line?

Upvotes: 0

Views: 415

Answers (3)

dfrib
dfrib

Reputation: 73176

(name: String, age: Int) in this context is a type, not a call

(Note that I've renamed your property from Student to student, as the Swift convention is to use CamelCase naming for types; camelCase is used for instances)

The following statement

var student = (name: String, age: Int)()
            /* ^^^^^^^^^^^^^^^^^^^^^^^ ^-fails to instantiate the tuple members
                           \ (String, Int) tuple type  */

declares, inline, a named tuple type (named members) of type (String, Int), but no values are given to the two tuple members in the initializer call, (), that follows the inline type declaration of the tuple.

A valid call would be:

var student = (name: String, age: Int)("David", 17) 
print(type(of: student)) // (String, Int)

Whereafter the named members of the tuple can be accessed as

print(student.name) // David
print(student.age)  // 17

The possibly tricky part above is realizing that (name: String, age: Int) is a type (and not a call!), which means the paranthesis that follows is in fact an attempted call to an initializer of that type (where here, that type happens to have been declared inline with the initializer call). In your example, no initializer exist for the (String, Int) type that takes zero arguments, which explains the spot-on error message:

Cannot invoke the initializer for type '(name: String, age: Int)' with no argument

You may compare this to a custom Student type with only these two members:

struct Student {
    let name: String
    let age: Int
}

var student = Student(name: "David", age: 17) 
           /* ^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^-initializer call for the type 
                 \ type 'Student'                */
print(type(of: student)) // Student

print(student.name) // David
print(student.age)  // 17

Upvotes: 1

Alexander Luna
Alexander Luna

Reputation: 5439

You are creating a variable that calls your function parameters. I don't know what exactly you want to do but:

(name: String, age: Int)() 

The parenthesis at the end are invoking or initiating "(name: String, age: Int)". The problem is the parameters are not defined and so the compiler doesn't know what to do.

func student(name: String, age: Int){
   // put your code here
}

This is one way to fix it. You can call that function like this:

student(name: "John", age: 13)

Upvotes: 0

vadian
vadian

Reputation: 285069

An initializer must be called on the type and the parameters must be values rather than types e.g.

let student = Student(name: "Foo", age: 20)

Upvotes: 0

Related Questions