Swift Xcode 6棒球计数器 [英] Swift Xcode 6 baseball Counter

查看:89
本文介绍了Swift Xcode 6棒球计数器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当罢工达到3时,我似乎无法写出if语句到底是否会上升?
可以有人租借帮助,并且当有人点击3次罢工时,这种情况很快就会上升

I can't seem to write the if statement in the end to make the outs go up when the strikes hit 3? can someone lease help and make it so in swift the outs will go up when someone hits 3 strikes

//
//  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 ++
   }
}


推荐答案

使用财产观察员:

var counter = 1 {
didSet {
    if counter == 3 {
        self.outsCounter++
    }
}
}

每当计数器变了,didSet将被调用。

Whenever counter gets changed, didSet will be called.

(另请注意,相等运算符是 == = 用于转让。)

(Also note that the equality operator is ==. = is for assignment.)

这篇关于Swift Xcode 6棒球计数器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆