如何在Swift中创建设置对象

如何在Swift中创建设置对象,swift,struct,notificationcenter,Swift,Struct,Notificationcenter,我试图在Swift-3中创建一个对象,该对象将保存各种基本用户设置,这些设置可以在整个应用程序中轻松访问。我目前将此设置作为名为PTSettings的结构。它是这样实现的: struct PTSettings { static var aUserSettings: String() } 可以在应用程序中轻松访问,如:PTSettings.auserset=“Foo” 我在这里遇到的困难是,我想让这个结构观察来自通知中心的UIScreen通知。当屏幕连接时,PTSettings初始化外部

我试图在Swift-3中创建一个对象,该对象将保存各种基本用户设置,这些设置可以在整个应用程序中轻松访问。我目前将此设置作为名为
PTSettings
的结构。它是这样实现的:

struct PTSettings {
    static var aUserSettings: String()
}
可以在应用程序中轻松访问,如:
PTSettings.auserset=“Foo”

我在这里遇到的困难是,我想让这个结构观察来自
通知中心的UIScreen通知。当屏幕连接时,
PTSettings
初始化外部屏幕,为其分配视图,显示横幅以让用户知道等

我熟悉在UIViewController上执行所有这些任务;但是,我不擅长使用结构。我的希望是,当应用程序加载时,结构将被初始化,在初始化过程中,结构将观察
NotificationCenter
,同时检查在加载应用程序之前是否连接了屏幕

下面是我目前拥有的

/// Struct containing various user-generate data such as color, messages and other settings.
struct PTSettings {


    // External UI
    //
    static var externalScreen: UIScreen!
    //
    static var externalWindow: UIWindow!
    //
    static var extDisplay: PTExternalDisplayVC?


    init () {
        // Receive notifications if a screen is connected or disconnected
        //
        let center = NotificationCenter.default
        center.addObserver(self, selector: #selector(PTSettings.handleScreenDidConnectNotification(notification:)), name: NSNotification.Name.UIScreenDidConnect, object: nil)
        center.addObserver(self, selector: #selector(PTSettings.handleScreenDidDisconnectNotification(notification:)), name: NSNotification.Name.UIScreenDidDisconnect, object: nil)
        center.addObserver(self, selector: #selector(PTSettings.handleScreenModeDidChangeNotification), name: NSNotification.Name.UIScreenModeDidChange, object: nil)
    }




    // MARK: External Displays
    //
    static func initializeExternalScreen(externalScreen:UIScreen) {
        self.externalScreen = externalScreen

        externalScreen.overscanCompensation = UIScreenOverscanCompensation(rawValue: 3)!

        // Create a new window sized to the external screen's bounds
        self.externalWindow = UIWindow(frame: self.externalScreen.bounds)

        // Assign screen object to screen property of the new window
        self.externalWindow.screen = externalScreen

        // Load the clock view
        let viewForExternalScreen = self.storyboard?.instantiateViewController(withIdentifier: "ExternalDisplay") as! PTExternalDisplayVC
        viewForExternalScreen.view.frame = self.externalWindow.frame

        // Add the view to the window
        self.externalWindow.addSubview(viewForExternalScreen.view)

        // Create a reference to the viewForExternalScreen
        self.extDisplay = viewForExternalScreen

        // Make the window visible
        self.externalWindow.makeKeyAndVisible()
    }
    //
    static func handleScreenDidConnectNotification (notification:Notification) {
        if let screen = notification.object as? UIScreen {
            initializeExternalScreen(externalScreen: screen)
        }
    }
    //
    static func handleScreenDidDisconnectNotification (notification:Notification) {
        if externalWindow != nil {
            externalWindow.isHidden = true
            externalWindow = nil
            displayConnectedLabel.text = "No Display"
            displayConnectedLabel.textColor = CustomColors.Red.color
            JSSAlertView().warning(self, title: "External Display Disconnected.")
        }
    }
    //
    static func handleScreenModeDidChangeNotification () {
        let screen = UIScreen.screens[1]
        initializeExternalScreen(externalScreen: screen)
    }

}
编译器抱怨在
init()
方法中每次添加观察者,如下所示:

“#选择器”的参数引用了未向Objective-C公开的静态方法“HandleScreendedConnectionNotification(notification:)”

但是,当在方法之前添加
@objc
时,它会抱怨:

@objc只能与类、@objc协议和类的具体扩展的成员一起使用

我怎样才能达到预期的结果,而我是否完全偏离了这种方法

我完全偏离了这种方式吗

你真的完全疯了。区分Objective-C(和Cocoa)和Swift。Swift结构是一种纯粹的Swift功能。您正在尝试使用Cocoa特性,即通过选择器调用方法,并使用Swift特性和struct。你不能那样做。Swift结构缺少Objective-C内省功能,使选择器能够工作。这就是为什么您只能为Objective-C类的方法创建选择器的原因,即从NSObject继承的类(正如您过去所做的那样)

但是,添加NotificationCenter观察者是一个很好的选择,您可以使用一个尾随闭包/函数,而不是选择器。你可以试试(尽管我不能保证任何事情)

我完全偏离了这种方式吗

你真的完全疯了。区分Objective-C(和Cocoa)和Swift。Swift结构是一种纯粹的Swift功能。您正在尝试使用Cocoa特性,即通过选择器调用方法,并使用Swift特性和struct。你不能那样做。Swift结构缺少Objective-C内省功能,使选择器能够工作。这就是为什么您只能为Objective-C类的方法创建选择器的原因,即从NSObject继承的类(正如您过去所做的那样)

但是,添加NotificationCenter观察者是一个很好的选择,您可以使用一个尾随闭包/函数,而不是选择器。你可以试试(尽管我不能保证任何事情)