简体   繁体   English

只有类方法和委托的Swift类?

[英]Swift class with only class methods and a delegate?

I want a class with all class methods. 我想要一个包含所有类方法的类。 I would like to use a delegate so my view controllers (conforming to the protocol) can call AlertFactory.passwordResetSucess() and display the alert. 我想使用委托,所以我的视图控制器(符合协议)可以调用AlertFactory.passwordResetSucess()并显示警报。

  1. Is there a way to make this this work? 有没有办法让这个工作? A way to use the delegate from this class? 一种使用这个类的委托的方法?

  2. Is this bad practice? 这是不好的做法吗? Bad form? 形式不好? Why? 为什么?

  3. What's a good way to make this happen? 实现这一目标的好方法是什么? There will be other class methods used in several views. 在几个视图中将使用其他类方法。

Thanks! 谢谢!

    protocol AlertFactoryDelegate 
    {
        func showAlert(alert: UIAlertController)
    }

    class AlertFactory: NSObject {

     let delegate: AlertFactoryDelegate!

     class func passwordResetSuccess() 
     {
         var alert = UIAlertController(title: "Success!", message: "Yay", preferredStyle: UIAlertControllerStyle.Alert)
         alert.addAction(UIAlertAction(title: "Continue", style: UIAlertActionStyle.Default, handler: nil))
         delegate.showAlert(alert)
     }
    }

You can set delegate to be static , and access it with AlertFactory.delegate 您可以将delegate设置为static ,并使用AlertFactory.delegate访问它

class AlertFactory: NSObject {
    static weak var delegate: AlertFactoryDelegate?

    class func passwordResetSuccess() {
        ...
        self.delegate?.showAlert(alert)
    }
}

To create a delegate: 要创建委托:

class SomeClass: AlertFactoryDelegate {
    ... // Implement everything the protocol requires
}

// Asssing a delegate
AlertFactory.delegate = SomeClass()
  1. You could use the SINGLETON command pattern, its a very common practice. 您可以使用SINGLETON命令模式,这是一种非常常见的做法。 You should read up on it, but basically means there is only 1 ever instance of it and you can call methods off of it when needed (or send things to it as needed). 你应该阅读它,但基本上意味着它只有1个实例,你可以在需要时调用它的方法(或根据需要发送它)。 Common examples are EventSystem objects or GlobalObserver objects, Factories, and ContextManagers. 常见示例是EventSystem对象或GlobalObserver对象,Factories和ContextManagers。

Here is information on it: https://sourcemaking.com/design_patterns/singleton 以下是有关它的信息: https//sourcemaking.com/design_patterns/singleton

  1. Using a SINGLETON has tradeoffs, but can be very useful in many situations. 使用SINGLETON需要权衡,但在许多情况下可能非常有用。 This seems like a good pattern for your problem. 这似乎是一个很好的问题模式。

  2. You need to initialize your delegate when your app starts. 您需要在应用启动时初始化您的delegate From then on, when another view controller needs to set that delegate you can assign it: (this assumes you made delegate public and its a SINGLETON) 从那时起,当另一个视图控制器需要设置该委托时,您可以分配它:(这假定您将委托设为公共,其为SINGLETON)

_myViewController.actionThatRequiresDelegate = AlertFactory.delegate

As mentioned in one of the other answers, using Swifts trailing syntax closure system is a great way to use anonymous functions and you could do your logic in there, or communicate with your delegate in there. 正如其他一个答案中所提到的,使用Swifts尾随语法闭包系统是一种使用匿名函数的好方法,您可以在那里执行逻辑,或者在那里与您的委托进行通信。

update 更新

Initialize in the AppDelegate.swift: 在AppDelegate.swift中初始化:

override init()
{
    AlertFactory.init()
}

Then in your AlertFactory 然后在你的AlertFactory中

public static AlertFactoryDelegate myDelegate

public static func init()
{
    myDelegate = new AlertFactoryDelegate()
}

You cannot set an instance variable on a class without instantiating the class first. 如果不首先实例化类,则无法在类上设置实例变量。 Is there a reason you don't want to instantiate the class, and just want to use class methods instead? 您是否有理由不想实例化该类,而只是想使用类方法? If you feel that you have good reason to use class methods over instance methods, then you have two potential options: 如果您觉得您有充分的理由在实例方法上使用类方法,那么您有两个可能的选择:

  1. Pass a delegate as a parameter to the class method. 将委托作为参数传递给类方法。
  2. Use Completion Blocks instead of Delegation. 使用完成块而不是委托。 Here's a decent example in swift. 这是一个很好的例子。

In my personal experience I find blocks to be preferable. 根据我的个人经验,我发现块更可取。

EDIT: I haven't used Swift that much lately, but as @Skrundz pointed out you can indeed use a static variable on a class in Swift. 编辑:我最近没有使用过Swift,但正如@Skrundz指出的那样,你确实可以在Swift中的类上使用静态变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM