Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在Swift中将布尔从一个类返回到另一个类_Ios_Arrays_Xcode_Model View Controller_Swift - Fatal编程技术网

Ios 在Swift中将布尔从一个类返回到另一个类

Ios 在Swift中将布尔从一个类返回到另一个类,ios,arrays,xcode,model-view-controller,swift,Ios,Arrays,Xcode,Model View Controller,Swift,我这里有一段代码,我似乎不明白为什么它不起作用 viewController应该检查开关是打开还是关闭 class ViewControllerFirst: UIViewController { @IBAction func friendFunc(){ if friendSwitch.on{ friendOn = true } else { friendOn = false } } func returnFriend()

我这里有一段代码,我似乎不明白为什么它不起作用

viewController应该检查开关是打开还是关闭

class ViewControllerFirst: UIViewController {  


@IBAction func friendFunc(){
    if friendSwitch.on{
        friendOn = true
    }   else    {
        friendOn = false
    }
}

func returnFriend() -> Bool{
    return friendOn
    }
}
如果开关打开,则应将阵列添加到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]
    }
} 
我在构建时没有收到任何错误消息,但它挑出了这一行:

    func returnFriend() -> Bool{

每次检查该布尔值时,您似乎都在实例化一个新的视图控制器,(
ViewControllerFirst()
看起来至少应该是
fview
,我不认为fview是您想要的实际视图控制器)因此,在我看来,它总是错误的。

调用该函数时,您正在创建一个新的视图控制器。这可能不是你想要的

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]
    }
} 
func randomdar()->字符串{
var tempArray=[“”]
//如果ViewControllerFirst().returnFriend()==true{