当罢工达到3时,我似乎无法写下if语句以使出局上升? 当有人击中3次罢工时,有人可以租借帮助并迅速实现这一目标
//
// ViewController.swift
// helloWordDemo
//
// Created by Developer on 6/8/14.
// Copyright (c) 2014 AECApps. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBOutlet var labelDispaly : UILabel = nil
// dispaly Strikes
var counter = 1
@IBAction func buttonPressed(sender : AnyObject) {
labelDispaly.text = "Strikes \(counter++)"
}
//button to add strikes
@IBOutlet var OutsDispaly : UILabel = nil
var outsCounter = 1
//outs dispaly
@IBAction func outsButtonPressed(sender : AnyObject) {
OutsDispaly.text = "Outs \(outsCounter++)"
}
//button to add outs
if counter = 3 {
outsCounter ++
}
}
答案 0 :(得分:1)
使用属性观察者:
var counter = 1 {
didSet {
if counter == 3 {
self.outsCounter++
}
}
}
每当计数器发生变化时,都会调用didSet。
(另请注意,等于运算符为==
。=
用于分配。)