Swift Version 2 Bool [closed]

喜欢而已 提交于 2019-12-13 09:46:40

问题


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.
}

回答1:


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



来源:https://stackoverflow.com/questions/35331267/swift-version-2-bool

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!