I am using xcode 6 beta 6 and I get this weird error for a function that has no params.
我正在使用xcode 6 beta 6,对于没有参数的函数,我得到了这个奇怪的错误。
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?
这是一个错误还是我在这里做错了什么?
2 个解决方案
#1
51
Assuming StudentList
is a class, i.e.
假设StudentList是一个类,即
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.
将抛出所述异常,因为allStudents应用于类而不是类的实例。 allStudents函数期望一个self参数(对实例的引用)。它解释了错误消息。
This will be resolved if you do
如果你这样做,这将得到解决
var all = StudentList().allStudents()
#2
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.
Swift有实例方法和类型方法。实例方法是从类的特定实例调用的方法。 Type Method是一个从类本身调用的静态方法。
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.
为了调用allStudents方法,需要首先初始化StudentsList类。
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:
类型方法是属于类的静态方法,而不是类的实例。正如@ AnthodyKong的回答中提到的那样,可以通过在func之前使用类或静态关键字来创建类型方法。类通过引用传递,Structs按值传递,因此这些类称为引用类型和值类型。这是他们的样子:
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.
因为allStudents是Type方法,所以不需要首先初始化类(或结构)。
See also
也可以看看
- Method documentation
- 方法文档
- Instance Methods and Type Methods in Swift
- Swift中的实例方法和类型方法
#1
51
Assuming StudentList
is a class, i.e.
假设StudentList是一个类,即
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.
将抛出所述异常,因为allStudents应用于类而不是类的实例。 allStudents函数期望一个self参数(对实例的引用)。它解释了错误消息。
This will be resolved if you do
如果你这样做,这将得到解决
var all = StudentList().allStudents()
#2
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.
Swift有实例方法和类型方法。实例方法是从类的特定实例调用的方法。 Type Method是一个从类本身调用的静态方法。
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.
为了调用allStudents方法,需要首先初始化StudentsList类。
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:
类型方法是属于类的静态方法,而不是类的实例。正如@ AnthodyKong的回答中提到的那样,可以通过在func之前使用类或静态关键字来创建类型方法。类通过引用传递,Structs按值传递,因此这些类称为引用类型和值类型。这是他们的样子:
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.
因为allStudents是Type方法,所以不需要首先初始化类(或结构)。
See also
也可以看看
- Method documentation
- 方法文档
- Instance Methods and Type Methods in Swift
- Swift中的实例方法和类型方法