'ViewController' 클래스에 빠른 초기화자가 없습니다.
이 작업을 수행할 때 컴파일러로부터 불만 사항 수신
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) {
}
}
하지만 아래와 같이 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) {
}
}
제가 틀리지 않는 한 이 오류와 관련된 키워드는 보이지 않습니다.
오류는 개선될 수 있지만 첫 번째 버전의 문제는 멤버 변수가 있다는 것입니다.delegate
, 기본값이 없습니다.Swift의 모든 변수는 항상 값을 가져야 합니다.즉, 사용자가 없는 이니셜라이저에서 설정해야 하거나 기본값 인라인을 제공할 수 있습니다.
선택 사항으로 설정하면 다음과 같이 설정할 수 있습니다.nil
기본적으로 값을 명시적으로 지정하거나 초기화할 필요가 없습니다.
스위프트 프로그래밍 언어는 다음과 같이 말합니다.
클래스 및 구조물은 해당 클래스 또는 구조물의 인스턴스를 생성할 때까지 저장된 모든 속성을 적절한 초기 값으로 설정해야 합니다.저장된 속성은 불확정 상태로 둘 수 없습니다.
초기화기 내에 저장된 속성의 초기값을 설정하거나 속성 정의의 일부로 기본 속성 값을 할당할 수 있습니다.
따라서 다음과 같이 쓸 수 있습니다.
class myClass {
var delegate: AppDelegate //non-optional variable
init() {
delegate = UIApplication.sharedApplication().delegate as AppDelegate
}
}
또는:
class myClass {
var delegate = UIApplication.sharedApplication().delegate as AppDelegate //non-optional variable
init() {
println("Hello")
}
}
또는:
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
}
}
그러나 다음을 쓸 수는 없습니다.
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
}
}
이 오류는 초기화되지 않은 var 또는 allet이 있는 경우에도 나타납니다.
예를들면
class ViewController: UIViewController {
var x: Double
// or
var y: String
// or
let z: Int
}
변수가 수행하는 작업에 따라 해당 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
}
이 문제는 일반적으로 변수 중 하나에 값이 없거나 "!"를 추가하여 이 변수가 설정될 때까지 강제로 0을 저장할 때 나타납니다.
문제는 여기에 있습니다.
var delegate: AppDelegate
다음과 같이 정의해야 합니다.var delegate: AppDelegate!
값을 사용할 때까지 변수의 포장을 풀지 않고 0을 저장하는 옵션으로 지정합니다.
Xcode가 원인이 된 특정 코드 라인을 강조하는 대신 전체 클래스를 오류로 강조하여 파악하는 데 시간이 걸리는 것은 슬픈 일입니다.
코드에서 "!"를 잃어버렸다면, 아래의 이 코드와 같이 이 오류도 발생할 것입니다.
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)
}
}
교체하다var appDelegate : AppDelegate?
와 함께let appDelegate = UIApplication.sharedApplication().delegate
두번째 주석에 암시된 바와 같이.viewDidLoad()
.
은 "option" 의 .?
, 자세한 내용은 이 문서를 참조하십시오.
저는 Xcode 7과 Swift 2를 사용합니다.마지막으로, 나는 이렇게.
class ViewController:UIViewController {var time: NSTimer //여기서 오류 발생}
그런 다음 수정합니다: class ViewController:UI 보기 컨트롤러 {
var time: NSTimer!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewWillAppear(animated: Bool) {
//self.movetoHome()
time = NSTimer.scheduledTimerWithTimeInterval(5.0, target: self, selector: #selector(ViewController.movetoHome), userInfo: nil, repeats: false)
//performSegueWithIdentifier("MoveToHome", sender: self)
//presentViewController(<#T##viewControllerToPresent: UIViewController##UIViewController#>, animated: <#T##Bool#>, completion: <#T##(() -> Void)?##(() -> Void)?##() -> Void#>)
}
func movetoHome(){
performSegueWithIdentifier("MoveToHome", sender: self)
}
}
제게 있어서는 불완전한 선언이었습니다.예를 들어,
var isInverted: Bool
대신 올바른 방법:
var isInverted: Bool = false
언급URL : https://stackoverflow.com/questions/25811622/class-viewcontroller-has-no-initializers-in-swift
'programing' 카테고리의 다른 글
부울 값을 만드는 방법은? (0) | 2023.10.05 |
---|---|
스크립트 내에서 리디렉션을 사용하면 유니코드 출력이 생성됩니다.싱글바이트 ASCII 텍스트를 내보내는 방법은? (0) | 2023.10.05 |
C에서 STRCT를 사용할 때 명명 규칙 (0) | 2023.10.05 |
"본래 스레드 세이프(thread-safe)"란 무엇을 의미합니까? (0) | 2023.10.05 |
일치하지 않는 익명 정의() 모듈 (0) | 2023.10.05 |