斯威夫特2版布尔 [英] Swift Version 2 Bool

查看:204
本文介绍了斯威夫特2版布尔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在与创造Swift2一个应用程序的麻烦。结果
该应用程序有哪些变化,每0.4秒,露出了狼跑狼的形象。

不过我在斯威夫特2布尔的错误,我无法修复。结果
我也有一个声明void函数的问题。结果
任何帮助将是AP preciated。

  @IBAction FUNC startRunnng(发件人:UIButton的)
    {
        tmrRun = NSTimer.scheduledTimerWithTimeInterval(0.4,目标:自我,选择更新,用户信息:无,重复:真)        btnGo.userInteractionEnabled = NO;
        btnStop.userInteractionEnabled = YES;
        sliSpeed​​.userInteractionEnabled = NO;
    }    @IBAction FUNC stopRunnng(发件人:UIButton的)
    {
        tmrRun无效()
        btnGo.userInteractionEnabled = YES;
        btnStop.userInteractionEnabled = NO;
        sliSpeed​​.userInteractionEnabled = YES;
    }    无效takeaBound
    {
        字符串* imageName = [的NSString stringWithFormat:@狼%d.png图]; self.imvWolf.image = [UIImage的imageNamed:imageName];
            PIC + = 1;
            如果(PIC == 8)
            峰= 0;
    }覆盖FUNC viewDidLoad中(){
    super.viewDidLoad()
    峰= 0;
    //装载视图,通常从笔尖后做任何额外的设置。
}


解决方案

在斯威夫特布尔值真正 NO 在Objective C中使用

因此​​,在您 stopRunning 为实例方法,你应该写:

  @IBAction FUNC stopRunnng(发件人:UIButton的)
{
    tmrRun无效()
    btnGo.userInteractionEnabled =真
    btnStop.userInteractionEnabled = FALSE
    sliSpeed​​.userInteractionEnabled =真
}

(旁注,你不需要; 斯威夫特其一)

关于无效的功能。在斯威夫特你写的返回类型的方法声明之后,开始用 - > 。像这样:

  FUNC takeABound(parametersWouldGoHere) -  GT; ()

对于无效的方法,你可以写()虚空或者,你会经常做,不要't在所有写任何东西。

  FUNC takeABound(parametersWouldGoHere)

由于关于<一章中说,在雨燕程序设计语言 href=\"https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Functions.html#//apple_ref/doc/uid/TP40014097-CH10-ID158\"相对=nofollow>功能


  

由于它不需要返回值,函数的定义不包括返回箭头( - >)或返回类型。


您可以阅读更多关于功能,布尔和<一个类似href=\"https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/index.html\"相对=nofollow>雨燕程序设计语言,有一个很好的一章叫阿斯威夫特之旅,将你介绍很多基本的东西。

希望帮助

I'm having trouble with creating an app in Swift2.
The App has an image of a wolf which changes every 0.4 seconds to reveal a running wolf.

However I have Bool errors in Swift 2 that I cannot fix.
I also have issues with declaring a void function.
Any help would be appreciated.

    @IBAction func startRunnng(sender: UIButton)
    {
        tmrRun = NSTimer.scheduledTimerWithTimeInterval(0.4, target: self, selector: "update", userInfo: nil, repeats: true)

        btnGo.userInteractionEnabled = NO;
        btnStop.userInteractionEnabled = YES;
        sliSpeed.userInteractionEnabled = NO;
    }

    @IBAction func stopRunnng(sender: UIButton)
    {
        tmrRun invalidate()
        btnGo.userInteractionEnabled = YES;
        btnStop.userInteractionEnabled = NO;
        sliSpeed.userInteractionEnabled = YES;
    }

    void takeaBound
    {
        String *imageName = [NSString stringWithFormat:@"wolf%d.png", pic];self.imvWolf.image = [UIImage imageNamed:imageName];
            pic += 1;
            if (pic == 8)
            pic = 0;
    }

override func viewDidLoad() {
    super.viewDidLoad()
    pic = 0;
    // Do any additional setup after loading the view, typically from a nib.
}

解决方案

Boolean values in Swift are true and false, YES and NO is used in Objective C.

So in your stopRunning method for instance, you should write:

@IBAction func stopRunnng(sender: UIButton)
{
    tmrRun invalidate()
    btnGo.userInteractionEnabled = true
    btnStop.userInteractionEnabled = false
    sliSpeed.userInteractionEnabled = true
}

(sidenote, you don't need the ; in Swift either)

About the void function. In Swift you write the return type AFTER your method declaration, starting with a ->. Like so:

func takeABound(parametersWouldGoHere) -> () 

For a void method you can write () or Void or, as you'll often do, don't write anything at all.

func takeABound(parametersWouldGoHere)

As it says in "The Swift Programming Language" in the chapter about functions

Because it does not need to return a value, the function’s definition does not include the return arrow (->) or a return type.

You can read more about functions, booleans and the like in "The Swift Programming Language", there is a nice chapter called "A Swift Tour" that will introduce you to many of the basic things.

Hope that helps

这篇关于斯威夫特2版布尔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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