Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.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
Swift 无法使用单例类返回其属性_Swift - Fatal编程技术网

Swift 无法使用单例类返回其属性

Swift 无法使用单例类返回其属性,swift,Swift,大家好,我有以下代码 class DeviceListViewController { private var currentPeripheral:BLEPeripheral? public class var sharedInstance: DeviceListViewController { struct Singleton { static let instance = DeviceListViewController()

大家好,我有以下代码

class DeviceListViewController {
    private var currentPeripheral:BLEPeripheral?

    public class var sharedInstance: DeviceListViewController {
        struct Singleton {
            static let instance = DeviceListViewController()
        }
        return Singleton.instance
    }

    func getCurrentPeripheral() -> BLEPeripheral? {
        return currentPeripheral
    }

}
但是当我在下面的另一个类中使用这个时

DeviceListViewController.getCurrentPeripheral() 
它给我一个错误,告诉我以下 在类型“DeviceListViewController”上使用实例成员“GetCurrentPeripal”;您的意思是使用“DeviceListViewController”类型的值吗

我不知道我在这里做错了什么。对如何解决这个问题有什么建议吗

p、 xcode的自动完成功能以某种方式将ViewController作为参数传递给它

DeviceListViewController.getCurrentPeripheral(DeviceListViewController) 

但是它会导致相同的错误

您需要对共享实例而不是类调用该方法

DeviceListViewController.sharedInstance.getCurrentPeripheral() 

您需要对共享实例而不是类调用该方法

DeviceListViewController.sharedInstance.getCurrentPeripheral() 

要避免使用MyClass.sharedInstance.method(),可以使用以下方法:

class DeviceListViewController {
    private var currentPeripheral:BLEPeripheral?

    public class var sharedInstance: DeviceListViewController {
        struct Singleton {
            static let instance = DeviceListViewController()
        }
        return Singleton.instance
    }

    class func getCurrentPeripheral() -> BLEPeripheral? {
        return DeviceListViewController.sharedInstance.currentPeripheral
    }

}

现在您可以使用
MyClass.method()

为了避免使用
MyClass.sharedInstance.method()
,您可以使用以下方法:

class DeviceListViewController {
    private var currentPeripheral:BLEPeripheral?

    public class var sharedInstance: DeviceListViewController {
        struct Singleton {
            static let instance = DeviceListViewController()
        }
        return Singleton.instance
    }

    class func getCurrentPeripheral() -> BLEPeripheral? {
        return DeviceListViewController.sharedInstance.currentPeripheral
    }

}

现在您可以使用
MyClass.method()

谢谢您的帮助!但我是否需要始终使用共享实例?例如,当我在Devicelistviewcontroller中连接设备时,我应该执行Devicelistviewcontroller.SharedInstance.connectPeripheral而不是connectPeripheral吗?在Devicelistviewcontroller中连接设备的确切含义是什么?如果控制器是UI视图层次结构的一部分,那么您根本不需要共享实例。它是通过共享实例调用自己的属性还是像往常一样?因为如果我使用共享实例获取CurrentPeripal,即使我知道有连接的东西,它也会返回nil。从类内部调用方法时,不需要调用共享实例。但是考虑到共享实例可能与接口生成器中设计的实例不同。我需要做的是:DeVielistVistualMeCurr.SydDistCurn.CurrutEngult= BLUTALL(外设:外围设备,委托:自我)。不是把它分配给CurrentPeripal:)谢谢你的帮助我把你的答案标记为正确谢谢你的帮助!但我是否需要始终使用共享实例?例如,当我在Devicelistviewcontroller中连接设备时,我应该执行Devicelistviewcontroller.SharedInstance.connectPeripheral而不是connectPeripheral吗?在Devicelistviewcontroller中连接设备的确切含义是什么?如果控制器是UI视图层次结构的一部分,那么您根本不需要共享实例。它是通过共享实例调用自己的属性还是像往常一样?因为如果我使用共享实例获取CurrentPeripal,即使我知道有连接的东西,它也会返回nil。从类内部调用方法时,不需要调用共享实例。但是考虑到共享实例可能与接口生成器中设计的实例不同。我需要做的是:DeVielistVistualMeCurror .SyrdSncas.CurrutEngule= BLUTALL(外设:外围设备,委托:自身),而不是将其分配给CurrnEngutur::谢谢您的帮助,我将您的答案标记为正确的。