I have here a piece of code, and i can't seem to figure out why it doesn't work.
The viewController is supposed to check whether a switch is turned on or off.
class ViewControllerFirst: UIViewController {
@IBAction func friendFunc(){
if friendSwitch.on{
friendOn = true
} else {
friendOn = false
}
}
func returnFriend() -> Bool{
return friendOn
}
}
And if the switch is turned on, an Array should be added to tempArray.
import Foundation
struct DareBook {
let fview = ViewControllerFirst()
let dareArrayFriend = [""]
func randomDare() -> String{
var tempArray = [""]
if ViewControllerFirst().returnFriend() == true{
tempArray += dareArrayFriend
}
var unsignedArrayCount = UInt32(tempArray.count)
var unsignedRandomNumber = arc4random_uniform(unsignedArrayCount)
var randomNumber = Int(unsignedRandomNumber)
return tempArray[randomNumber]
}
}
i'm not getting any errormessages when i build, but it singles out this line:
func returnFriend() -> Bool{
看起来您每次检查布尔值时都实例化一个新的视图控制器,( ViewControllerFirst()
似乎至少应该是fview
,我不认为fview是您想要的实际ViewController),所以似乎我认为这永远是错误的。
You are creating a new view controller when you call the function. That is probably not what you want.
func randomDare() -> String{
var tempArray = [""]
// if ViewControllerFirst().returnFriend() == true{ <--- this line can't be right
if fview.returnFriend() == true{
tempArray += dareArrayFriend
}
var unsignedArrayCount = UInt32(tempArray.count)
var unsignedRandomNumber = arc4random_uniform(unsignedArrayCount)
var randomNumber = Int(unsignedRandomNumber)
return tempArray[randomNumber]
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.