35
19

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

swift初心者:「Class 'ViewController' has no initializers」の対処方法

Last updated at Posted at 2018-02-19

はじめに

初歩的なエラーかと思いますが、初心者のため備忘録を含め記録として残したいと思います。

①Class 'ViewController' has no initializers

ViewController
import UIkit

class ViewController: UIViewController {

    var sum: Int 
}

上記のようなコードを記入した際に発生するエラーです。

nilを許していただけないようです。

以下のどちらか一方の対策を行えば改善されると思われます。

①ViewController内にプロパティの初期値をオプショナル型で定義

ViewController
import UIkit

class ViewController: UIViewController {

    var sum: Int!
}

もしくは

②値を代入。

ViewController
import UIkit

class ViewController: UIViewController {

    var sum: Int = 0
}

以上のように実施することで、エラーが解消されるかと思います。

35
19
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
35
19

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?