[:en]Swift: set global variables[:]

[:en]Global variables can be used throughout the entire app and must be set before viewDidload and can’t use “let” to define variable agains:

import UIKit

class MainMenuViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    var element1: [String] = []
    var picture1 = []
    
    
    // Check user level
    func checkuserlevel() {
        let userlevel = NSUserDefaults.standardUserDefaults().stringForKey("level")!.stringByReplacingOccurrencesOfString(" ", withString: "");
        if userlevel == "all" {
            element1 = ["A","B","C"]
            picture1 = ["p1","p2","p3"]
         }
        else if userlevel == "admin" {
            element1 = ["A","B"]
            picture1 = ["p1","p2"]
        }
            
        else if userlevel == "teacher" {
            element1 = ["C"]
            picture1 = ["p3"]
         }
        else {
            element1 = ["C"]
            picture1 = ["p3"]
        }
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

    }
    

[:]

Print Friendly, PDF & Email
Scroll to Top