首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Swift中创建设置对象

如何在Swift中创建设置对象
EN

Stack Overflow用户
提问于 2016-11-03 15:01:02
回答 1查看 667关注 0票数 1

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

代码语言:javascript
复制
struct PTSettings {
    static var aUserSettings: String()
}

可以方便地在应用程序周围访问,如:PTSettings.aUserSetting = "Foo"

我在这里挣扎的是,我希望这个结构能够观察来自UIScreen的NotificationCenter通知。当屏幕连接时,PTSettings初始化外部屏幕,给它分配一个视图,显示一个横幅让用户知道等等.

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

下面是我目前的情况。

代码语言:javascript
复制
/// 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()方法中每次添加观察者时都会抱怨,如下所示:

“选择器”的参数是指不暴露于目标C的静态方法'handleScreenDidConnectNotification(notification:)‘。

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

@objc只能用于类的成员、@objc协议和类的具体扩展。

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-11-03 15:07:34

我是不是完全放弃了这种方法?

你真的完全疯了。区分目标-C(和可可)与Swift。Swift结构是一个纯粹的Swift功能。您正在尝试使用Cocoa特性,即通过选择器调用方法,并使用Swift特性,结构。你不能这么做。Swift结构缺乏目标-C自省功能,允许选择器工作。这就是为什么您只能为Objective类的方法,即继承自NSObject的类(就像过去所做的那样)创建一个选择器。

但是,还有添加通知中心观察者的另一种形式,在这里使用尾随闭包/函数,而不是选择器。你可以试一试(尽管我没有保证)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40404848

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档