Swift 2.1:扩展 NSObject 时出错
Swift 2.1 : Getting error while extends NSObject
我已经创建了用于测试可失败初始化程序的示例应用程序。当我扩展 NSObject 时,出现以下错误。
1) Property 'self.userName' not initialized at super.init call.
2)
Immutable value 'self.userDetails' may only be initialized once.
3)
Immutable value 'self.userName' may only be initialized once.
请找到下面的代码和截图。
class User: NSObject {
let userName: String!
let userDetails: [String]?
init?(dictionary: NSDictionary) {
super.init()
if let value = dictionary["user_name"] as? String {
self.userName = value
}
else {
return nil
}
self.userDetails = dictionary["user_Details"] as? Array
}
}
截图
所有属性必须在 super.init()
之前初始化
super.init()
之后,可失败初始化程序必须返回 Nil。这个限制should be removed in Swift 2.2
正确的实施方式是:
class User: NSObject {
let userName: String!
let userDetails: [String]?
init?(dictionary: NSDictionary) {
if let value = dictionary["user_name"] as? String {
self.userName = value
} else {
self.userName = nil
}
self.userDetails = dictionary["user_Details"] as? Array
super.init()
if userName == nil {
return nil
}
else if userDetails == nil {
return nil
}
}
}
import Foundation
let dictionary = ["user_name": "user", "user_Details":[1,2,3]]
class User: NSObject {
var userName: String?
var userDetails: [String]?
init?(dictionary: NSDictionary) {
super.init()
if let value = dictionary["user_name"] as? String {
self.userName = value
}
else {
return nil
}
self.userDetails = dictionary["user_Details"] as? Array
}
}
let c = User(dictionary: dictionary)
dump(c)
/*
▿ User
▿ Some: User #0
- super: <__lldb_expr_31.User: 0x7fe372f15860>
▿ userName: user
- Some: user
- userDetails: nil
*/
我已经创建了用于测试可失败初始化程序的示例应用程序。当我扩展 NSObject 时,出现以下错误。
1) Property 'self.userName' not initialized at super.init call.
2) Immutable value 'self.userDetails' may only be initialized once.
3) Immutable value 'self.userName' may only be initialized once.
请找到下面的代码和截图。
class User: NSObject {
let userName: String!
let userDetails: [String]?
init?(dictionary: NSDictionary) {
super.init()
if let value = dictionary["user_name"] as? String {
self.userName = value
}
else {
return nil
}
self.userDetails = dictionary["user_Details"] as? Array
}
}
截图
所有属性必须在 super.init()
super.init()
之后,可失败初始化程序必须返回 Nil。这个限制should be removed in Swift 2.2
正确的实施方式是:
class User: NSObject {
let userName: String!
let userDetails: [String]?
init?(dictionary: NSDictionary) {
if let value = dictionary["user_name"] as? String {
self.userName = value
} else {
self.userName = nil
}
self.userDetails = dictionary["user_Details"] as? Array
super.init()
if userName == nil {
return nil
}
else if userDetails == nil {
return nil
}
}
}
import Foundation
let dictionary = ["user_name": "user", "user_Details":[1,2,3]]
class User: NSObject {
var userName: String?
var userDetails: [String]?
init?(dictionary: NSDictionary) {
super.init()
if let value = dictionary["user_name"] as? String {
self.userName = value
}
else {
return nil
}
self.userDetails = dictionary["user_Details"] as? Array
}
}
let c = User(dictionary: dictionary)
dump(c)
/*
▿ User
▿ Some: User #0
- super: <__lldb_expr_31.User: 0x7fe372f15860>
▿ userName: user
- Some: user
- userDetails: nil
*/