具有实例变量的单例类和Swift中的方法(iOS)

时间:2017-03-14 14:39:44

标签: ios swift

所以,我正在创建一个Singleton类,如下所示,我在这个类中需要很少的实例变量,这样任何团队成员都可以访问实例变量并获取值。为此,我需要在开始时将这些实例变量初始化为某个值。 但我得到一个编译错误,说“在调用中缺少参数'doValueExists'参数”。

我到底做错了什么?

class ABC_Util {

    private var doesValueExists: Bool
    private var arrValues: NSMutableArray?

    class var sharedInstance: ABC_Util {
        struct ABC_UtilSingleton {
            static let instance = ABC_Util()
        }

        return ABC_UtilSingleton.instance
    }

    init(doesValueExists: Bool, arrValues: NSMutableArray?) {
        self.doesValueExists = self.checkValueExists()
        self.arrValues = self.getArrayOfValues()
    }

    //method
    internal func checkValueExists() -> Bool {
    }

    //method
    internal func getArrayOfValues() -> NSMutableArray? {
    }

}

4 个答案:

答案 0 :(得分:1)

您对ABC_Util的初始化程序声明为:

init(doesValueExists:Bool, arrValues:NSMutableArray?) {

因此你不能说

static let instance = ABC_Util()

表达式ABC_Util()对应于没有参数的初始化程序,并且您没有这样的初始化程序。你必须说:

static let instance = ABC_Util(doesValueExists:someBool, arrValues:someArray)

(当然有适当的值)。

答案 1 :(得分:0)

您必须使用初始化程序才能初始化变量。

class ABC_Util {

    private var doesValueExists:Bool
    private var arrValues:NSMutableArray?


    class var sharedInstance: ABC_Util 
    {
        struct ABC_UtilSingleton 
        {
            static let instance = ABC_Util(doesValueExists: true, arrValues: nil)
        }

        return ABC_UtilSingleton.instance
    }

    init(doesValueExists:Bool, arrValues:NSMutableArray?) {

        self.doesValueExists = doesValueExists
        self.arrValues = arrValues
    }

    //method
    internal func checkValueExists()-> Bool
    {
        return true
    }

    //method
    internal func getArrayOfValues()-> NSMutableArray?
    {
        return nil
    }
}

我建议您将单身声明更改为建议的语法

static let sharedInstance: ABC_Util = ABC_Util(doesValueExists: true, arrValues: nil)

答案 2 :(得分:0)

您可以使用如下。

类ABC_Util {

private var doesValueExists:Bool = false
private var arrValues:NSMutableArray?

class var sharedInstance: ABC_Util {

    struct ABC_UtilSingleton {

        static let instance = ABC_Util(doesValueExists: false, arrValues: ["a", "b", "c"])
    }

    return ABC_UtilSingleton.instance
}
init(doesValueExists:Bool, arrValues:NSMutableArray?) {

    self.doesValueExists = self.checkValueExists()
    self.arrValues = self.getArrayOfValues()
}
//method
internal func checkValueExists()-> Bool{

    return self.doesValueExists
}
//method
internal func getArrayOfValues()-> NSMutableArray?{

return arrValues
}

}

答案 3 :(得分:0)

我得到了解决方案,当我尝试这个时,工作得很好!

类ABC_Util {

 var doesValueExists:Bool = false
 var arrValues:NSMutableArray? = nil


class var sharedInstance: ABC_Util 
{
    struct ABC_UtilSingleton 
    {
        static let instance = ABC_Util()
    }

    return ABC_UtilSingleton.instance
}

init() {

    self.doesValueExists = self.checkValueExists()
    self.arrValues = self.getArrayOfValues()
}

//method
internal func checkValueExists()-> Bool
{
    //return true/false
}

//method
internal func getArrayOfValues()-> NSMutableArray?
{
    //return array/nil
}

}

相关问题