tra*_*uan 120 swift xcode6 swift-playground
当我这样做时,从编译器获得投诉
class ViewController: UIViewController {
var delegate : AppDelegate
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//self.appDelegate = UIApplication.sharedApplication().delegate;
}
@IBAction func getData(sender : AnyObject) {
}
@IBAction func LogOut(sender : AnyObject) {
}
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我只是添加?在AppDelegate的末尾,如下所示,错误消失了.
class ViewController: UIViewController {
var delegate : AppDelegate?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//self.appDelegate = UIApplication.sharedApplication().delegate;
}
@IBAction func getData(sender : AnyObject) {
}
@IBAction func LogOut(sender : AnyObject) {
}
}
Run Code Online (Sandbox Code Playgroud)
optional除非我错了,否则我看不到与此错误相关的关键字.
dre*_*wag 231
错误可能会有所改善,但第一个版本的问题是你有一个成员变量,delegate它没有默认值.Swift中的所有变量必须始终具有值.这意味着您必须在初始化程序中将其设置为您没有的,或者您可以在线提供默认值.
当您将其设为可选项时nil,默认情况下允许它,无需显式为其赋值或初始化它.
Ima*_*tit 45
Swift编程语言指出:
在创建该类或结构的实例时,类和结构必须将其所有存储的属性设置为适当的初始值.存储的属性不能保留在不确定的状态.
您可以在初始化程序中为存储的属性设置初始值,也可以通过将默认属性值指定为属性定义的一部分来设置初始值.
因此,你可以写:
class myClass {
var delegate: AppDelegate //non-optional variable
init() {
delegate = UIApplication.sharedApplication().delegate as AppDelegate
}
}
Run Code Online (Sandbox Code Playgroud)
要么:
class myClass {
var delegate = UIApplication.sharedApplication().delegate as AppDelegate //non-optional variable
init() {
println("Hello")
}
}
Run Code Online (Sandbox Code Playgroud)
要么:
class myClass {
var delegate : AppDelegate! //implicitly unwrapped optional variable set to nil when class is initialized
init() {
println("Hello")
}
func myMethod() {
delegate = UIApplication.sharedApplication().delegate as AppDelegate
}
}
Run Code Online (Sandbox Code Playgroud)
但你不能写下面的内容:
class myClass {
var delegate : AppDelegate //non-optional variable
init() {
println("Hello")
}
func myMethod() {
//too late to assign delegate as an non-optional variable
delegate = UIApplication.sharedApplication().delegate as AppDelegate
}
}
Run Code Online (Sandbox Code Playgroud)
小智 22
有时,如果你有一个尚未初始化的var或let,也会出现此错误.
例如
class ViewController: UIViewController {
var x: Double
// or
var y: String
// or
let z: Int
}
Run Code Online (Sandbox Code Playgroud)
根据您的变量应该执行的操作,您可以将var类型设置为可选,或者使用如下所示的值对其进行初始化
class ViewController: UIViewCOntroller {
// Set an initial value for the variable
var x: Double = 0
// or an optional String
var y: String?
// or
let z: Int = 2
}
Run Code Online (Sandbox Code Playgroud)
Iva*_*n V 12
当您的某个变量没有值或忘记添加"!"时,通常会出现此问题 强制此变量存储nil直到设置为止.
在你的情况下,问题出在这里:
var delegate: AppDelegate
Run Code Online (Sandbox Code Playgroud)
应将其定义为var delegate: AppDelegate!使其成为存储nil的可选项,并且在使用该值之前不会解包该变量.
令人遗憾的是,Xcode将整个类强调为错误,而不是突出显示导致它的特定代码行,因此需要一段时间才能弄明白.
小智 10
如果你输了"!" 在您的代码中,如下面的代码,您也会收到此错误.
import UIKit
class MemeDetailViewController : UIViewController {
@IBOutlet weak var memeImage: UIImageView!
var meme:Meme! // lost"!"
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.memeImage!.image = meme.memedImage
}
override func viewDidDisappear(animated: Bool) {
super.viewDidDisappear(animated)
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
123218 次 |
| 最近记录: |