Missing argument for parameter #1 in call error for function with no params. Swift
Asked Answered
C

2

24

I am using xcode 6 beta 6 and I get this weird error for a function that has no params.

Here is the function

func allStudents ()-> [String]{
    var appDel:AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
    var context:NSManagedObjectContext = appDel.managedObjectContext!
    var request = NSFetchRequest(entityName: "Student")
    request.returnsObjectsAsFaults = false
    //Set error to nil for now
    //TODO: Give an actual error.
    var result:NSArray = context.executeFetchRequest(request, error: nil)

    var students:[String]!
    for child in result{
        var fullname:String = child.valueForKey("firstName") as String + " "
        fullname += child.valueForKey("middleName") as String + " "
        fullname += child.valueForKey("lastName") as String
        students.append(fullname)
    }


    return students
}

and here is the call

var all = StudentList.allStudents()

Is this a bug or am I doing something wrong here?

Catabasis answered 21/8, 2014 at 21:6 Comment(3)
What is StudentList? Is it your class or is it a variable (in which case it should have a lower case s - studentList)Gracia
Are you sure that's the line it's crashing on? A simple version of this in playground works fine. ALthough you do have one obvious error: var students:[String]! never gets a value, it's initialized to nil. You probably want var students:[String] = [].Procathedral
Its not an error from the program crashing its xcode telling me its wrong. And yes StudentList is my class.Catabasis
T
51

Assuming StudentList is a class, i.e.

class StudentList {

    func allStudents ()-> [String]{
      ....
    }
}

Then an expression like this

var all = StudentList.allStudents() 

will throw the said exception, because allStudents is applied to a class instead of an instance of the class. The allStudents function is expecting a self parameter (a reference to the instance). It explains the error message.

This will be resolved if you do

var all = StudentList().allStudents()
Tachycardia answered 22/8, 2014 at 1:27 Comment(1)
or it need to be class/static methodImprecation
S
13

Swift has Instance Methods and Type Methods. An instance method is a method that is called from a particular instance of a class. A Type Method is a static method that is called from the class itself.

Instance Methods

An instance method would look something like this:

class StudentList {

    func allStudents() -> [String] {
      ....
    }
}

In order for the allStudents method to be called, the StudentsList class needs to be initialized first.

let list = StudentsList() // initialize the class
let all = list.allStudents() // call a method on the class instance

Trying to call an instance method on the class itself gives an error.

Type Methods

Type Methods are static methods that belong to the class, not an instance of the class. As was alluded to in the comments to @AnthodyKong's answer, a Type Method can be created by using the class or static keywords before func. Classes are passed by reference and Structs are passed by value, so these are known as reference type and value type. Here are what they would look like:

Reference Type

class StudentList {

    class func allStudents() -> [String] {
      ....
    }
}

Value Type

struct StudentList {

    static func allStudents() -> [String] {
      ....
    }
}

Call with

let all = StudentList.allStudents()

Because allStudents is a Type Method, the class (or struct) doesn't need to be initialized first.

See also

Sanmicheli answered 25/7, 2015 at 19:41 Comment(3)
This is great but they really need to update the text of the warning that you get. Why doesn't it just say that the function should be a class function and offer to fix it for you...?Suprematism
Indeed. If someone ever invents a programming language that gives intelligent error messages, I'm learning it.Sanmicheli
How about we write an API for clang where, before it shows you an error message, first it searches stack overflow and uses a machine learning algorithm to find the most likely solution to the problem, then just provides you a link to the question :DSuprematism

© 2022 - 2024 — McMap. All rights reserved.