【问题标题】:Integer not changing when i call a setter method from another class当我从另一个类调用 setter 方法时,整数不会改变
【发布时间】:2014-06-29 10:18:37
【问题描述】:

我需要改变一个类中的一个整数。然而,每次我尝试这个时,它都会暂时改变变量,这很奇怪。我已经在这个测试应用程序中复制了这个问题。

   import UIKit

 class ViewController: UIViewController {

var theapples = 0

override func viewDidLoad() {
    super.viewDidLoad()

    let battles = someclass(frame: CGRectMake(0, 0, 90, 90))
    self.view.addSubview(battles)

     let thetimer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector("apples"), userInfo: nil, repeats: true)


}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func apples(){

++theapples
    println("\(theapples)")

}

func santa(){

theapples = 2
    println(" this is the number \(theapples)")

}
}

这是我的一些课程代码

  import UIKit

  class someclass: UIView {

init(frame: CGRect) {
    super.init(frame:frame)

self.backgroundColor = UIColor.blackColor()




}

override func touchesBegan(touches: NSSet!, withEvent event: UIEvent!) {

let thepear = ViewController()
    thepear.santa()

self.removeFromSuperview()

}

 }

很抱歉,代码不堪重负,但这应该做的是每一秒它应该记录一个比上一个大一的数字。当我点击我的自定义视图时,它应该将整数 theapples 设置为 2,但它只是暂时这样做。这是日志的样子:

【问题讨论】:

  • 是否要求问题中的代码格式正确?缩进很糟糕,你甚至不关心删除空行。
  • 对不起...我个人觉得这更容易阅读。

标签: ios class integer swift


【解决方案1】:

let thepear = ViewController() 每次调用时都会创建一个新的(=不同的)ViewController 实例

【讨论】:

    猜你喜欢
    • 2015-10-14
    • 2019-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-06
    • 2016-08-03
    • 1970-01-01
    相关资源
    最近更新 更多