没有参数的函数在调用错误中缺少参数#1的参数。迅速 [英] Missing argument for parameter #1 in call error for function with no params. Swift

查看:632
本文介绍了没有参数的函数在调用错误中缺少参数#1的参数。迅速的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用xcode 6 beta 6,对于没有参数的函数,我得到了这个奇怪的错误。

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

这是函数

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
}

这是电话

var all = StudentList.allStudents()

这是一个错误或我在这里做错了什么?

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

推荐答案

假设 StudentList 是一个类,即

class StudentList {

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

然后是这样的表达式

var all = StudentList.allStudents() 

将抛出所述异常,因为 allStudents 应用于类而不是类的实例。 allStudents 函数需要一个 self 参数(对实例的引用)。它解释了错误信息。

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.

如果你这样做将会解决

var all = StudentList().allStudents()

这篇关于没有参数的函数在调用错误中缺少参数#1的参数。迅速的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆