swift中的全局变量不变



我为我的应用程序创建了几个全局变量来加载图像和字符串。然而,当我试图更改值时,它们保持不变。调用didSelectRowAtIndexPath方法后,该值不会更改,它仍然是"初始"的。

import UIKit
var testString = "initial"
class SubjectsViewController: UIViewController {
.....
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
        imageFiles[indexPath.row].getDataInBackgroundWithBlock { imageData, error in
            if (error == nil) {
                selectedPhoto = UIImage(data: imageData)!
                println("Should change variables")
                testString = "testing"
                println("no error in fetching image")
            } else {
                println("There is an error getting image")
            }
        }
        self.performSegueWithIdentifier("detailSegue", sender: self)
    }

创建一个包含应用程序的所有全局变量的类

 class GlobalVariables : NSObject {
       var testString = "initial"
    }
    var global = GlobalVariables()

所以现在在你的课堂上使用代码作为

 if (error == nil) {
                selectedPhoto = UIImage(data: imageData)!
                println("Should change variables")
// Global variable as now this will work and its value will be not be remove until the application is close , and change until y             
                global.testString = "testing"
                println("no error in fetching image")

相关内容

  • 没有找到相关文章

最新更新