Home>

In addition to ViewController.swift, suppose you created another swift file.
What kind of processing is necessary to extract the variable described in the other Swift file and handle it with ViewController?

  • Answer # 1

    Nothing special is necessary.

    class Hoge {
        var hogehoge = 0
    }
    // ViewController.swift
    let hoge = Hoge ()
    print (hoge.hogehoge) // =>0
    hoge.hogehoge = 123456
    print (hoge.hogehoge) // =>123456
    // create another instance
    let hoge2 = Hoge ()
    print (hoge2.hogehoge) // =>0