问题
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