'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) {
}
}
내가 optional잘못하지 않으면이 오류와 관련된 키워드 가 표시되지 않습니다 .
오류는 개선 될 수 있지만 첫 번째 버전의 문제점 delegate은 기본값이없는 멤버 변수 입니다. Swift의 모든 변수는 항상 값을 가져야합니다. 즉, 가지고 있지 않은 이니셜 라이저에서 설정해야하거나 기본값을 인라인으로 제공 할 수 있습니다.
선택 사항으로 만들면, nil기본적으로 값을 지정하거나 명시 적으로 값을 제공하거나 초기화 할 필요가 없습니다.
Swift Programming Language 상태는 다음과 같습니다.
클래스와 구조체는 해당 클래스 나 구조체의 인스턴스가 만들어 질 때까지 저장된 모든 속성을 적절한 초기 값으로 설정해야합니다. 저장된 특성은 불확실한 상태로 둘 수 없습니다.
이니셜 라이저 내에서 저장된 속성의 초기 값을 설정하거나 속성 정의의 일부로 기본 속성 값을 할당하면됩니다.
따라서 다음과 같이 작성할 수 있습니다.
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 또는 let이있는 경우에도 나타납니다.
예를 들어
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
}
This issue usually appears when one of your variables has no value or when you forget to add "!" to force this variable to store nil until it is set.
In your case the problem is here:
var delegate: AppDelegate
It should be defined as var delegate: AppDelegate! to make it an optional that stores nil and do not unwrap the variable until the value is used.
It is sad that Xcode highlights the whole class as an error instead of highlighting the particular line of code that caused it, so it takes a while to figure it out.
if you lost a "!" in your code ,like this code below, you'll also get this error.
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)
}
}
Replace var appDelegate : AppDelegate? with let appDelegate = UIApplication.sharedApplication().delegate as hinted on the second commented line in viewDidLoad().
The keyword "optional" refers exactly to the use of ?, see this for more details.
I use Xcode 7 and Swift 2. Last, I had made:
class ViewController: UIViewController{ var time: NSTimer //error this here }
Then I fix: class ViewController: UIViewController {
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)
}
}
For me was a declaration incomplete. For example:
var isInverted: Bool
Instead the correct way:
var isInverted: Bool = false
참고URL : https://stackoverflow.com/questions/25811622/class-viewcontroller-has-no-initializers-in-swift
'Programming' 카테고리의 다른 글
| Boto3을 사용하여 S3 객체를 문자열로 열기 (0) | 2020.07.13 |
|---|---|
| Windows 또는 Linux를 감지합니까? (0) | 2020.07.13 |
| 일정 시간이 지나면 웹 사이트 리디렉션 (0) | 2020.07.13 |
| AJAX를 통해 배열을 mvc 동작으로 전달 (0) | 2020.07.13 |
| 요소가 존재할 때까지 기능 대기 (0) | 2020.07.13 |