Saeed Rahmatolahi
Saeed Rahmatolahi

Reputation: 1338

Why I can't use variable that I defined in viewDidLoad in swift 3

I defined a variable in viewDidLoad But The problem is that I can't use that variable in the same View Controller outside of viewDidLoad.

Here is my code:

class ProfileSettingTableViewController: UITableViewController {

var userAccountMoney = String()

var headerList = ["account\(ProfileSettingTableViewController().userAccountMoney)" , "Help" , "" ]


override func viewDidLoad() {
    super.viewDidLoad()

    let userAccount = profileViewController().userMoney

    let numberFormatter = NumberFormatter()
    numberFormatter.locale = Locale(identifier: "fa")
    numberFormatter.numberStyle = NumberFormatter.Style.decimal
    numberFormatter.string(from: NSNumber(value: userAccount))
    userAccountMoney.append(numberFormatter.string(from: NSNumber(value: userAccount))!)

}

this code compiles, but even if I define for example let a = 1000 I can't reach that outside viewDidLoad

Upvotes: 0

Views: 602

Answers (3)

Nirav D
Nirav D

Reputation: 72410

The issue is you cannot access instance property when you initialized another another property outside any method because complier don't know which one initialized the first, so what you need to do is declare headerList simply set the account string only as first object of array and latter changed it with new value.

var userAccountMoney = String()
var headerList = ["account" , "Help" , "" ]

Now in viewDidLoad simply replace the first object with new value.

override func viewDidLoad() {
    super.viewDidLoad()

    let userAccount = profileViewController().userMoney

    let numberFormatter = NumberFormatter()
    numberFormatter.locale = Locale(identifier: "fa")
    numberFormatter.numberStyle = NumberFormatter.Style.decimal
    numberFormatter.string(from: NSNumber(value: userAccount))
    userAccountMoney = numberFormatter.string(from: NSNumber(value: userAccount))!
    self.headerList[0] = "account\(userAccountMoney)"
}

Upvotes: 1

SuryaKantSharma
SuryaKantSharma

Reputation: 1181

you can take this variable outside the function and take as instance variable.

    class VC: ViewController {
    var userAccountMoney = String()

    override func viewDidLoad() {
          super.viewDidLoad() 
          userAccountMoney = numberFormatter.string(from:
NSNumber(value: userAccount))!

                }

//access userAccountMoney anyWhere in the instance function of view Controller
      }

Upvotes: 0

Oleg Danu
Oleg Danu

Reputation: 4159

I guess you have to revise Swift documentation regarding properties.

In your case you have a local variable whose life-cycle ends up at the end of the current scope declaration. Which means, if you declared a variable within viewDidLoad function, it will die at the end of the function execution.

Update: Here comes a simple example of how to use it:

class MyViewController {
var userAccountMoney: String!

override func viewDidLoad() {
    super.viewDidLoad()
    userAccountMoney = numberFormatter.string(from: NSNumber(value: userAccount))!
}

func otherFunc() {
    var headerList = [String(describing:"Account \(userAccountMoney)")]
}
}

Upvotes: 0

Related Questions