class FirstViewController : UiViewController {
      example("First Value","Second Value")

      func example(_ firstParameter: String, _ secondParameter: String) {
         var one = self.ValueOne[0].first
         var two = self.ValueTwo[0].second
         var three = ShareClass.Variable.value
         var four = keychain.get('name')
         var five = self.variableNameFive
         var six = firstParameter
         var seven = secondParameter
      }
    }
  class SecondViewController : UiViewController {
    example("Random String","Random Value String")

    func example(_ firstParameter: String, _ secondParameter: String) {
       var one = self.ValueOne[0].first
       var two = self.ValueTwo[0].second
       var three = ShareClass.Variable.value
       var four = keychain.get('name')
       var five = self.variableNameFive
       var six = firstParameter
       var seven = secondParameter
     }
    }
 class ThirdViewController : UiViewController {
    example("Dynamic String","Dynamic Value Type")

    func example(_ firstParameter: String, _ secondParameter: String) {
       var one = self.ValueOne[0].first
       var two = self.ValueTwo[0].second
       var three = ShareClass.Variable.value
       var four = keychain.get('name')
       var five = self.variableNameFive
       var six = firstParameter
       var seven = secondParameter
     }
    }
class FourthViewController : UiViewController {
    example("Complicated String","Complicated String Variable")

    func example(_ firstParameter: String, _ secondParameter: String) {
       var one = self.ValueOne[0].first
       var two = self.ValueTwo[0].second
       var three = ShareClass.Variable.value
       var four = keychain.get('name')
       var five = self.variableNameFive
       var six = firstParameter
       var seven = secondParameter
     }
    }

在上面的代码中,我编写了该函数,该函数作为该特定视图控制器的变量值。

因为它是在各种viewcontroller中重复的相同代码。
我试图将其作为单个函数example()写入struct或class文件中,并在其他viewcontroller中调用它。

旧方法遵循UIViewController扩展文件。有什么最好的方法来实现这一点?

最佳答案

您应该扩展您的UIViewController类。
您可以从所有ViewController调用此示例方法。

extension UIViewController {
func example(_ firstParameter: String, _ secondParameter: String) {
    var one = self.ValueOne[0].first
    var two = self.ValueTwo[0].second
    var three = ShareClass.Variable.value
    var four = keychain.get('name')
    var five = self.variableNameFive
    var six = firstParameter
    var seven = secondParameter
}

或者如Vipin所说,您可以创建一个自定义类。
class CustomClass: UIViewController {
    func example(_ firstParameter: String, _ secondParameter: String) {
        var one = self.ValueOne[0].first
        var two = self.ValueTwo[0].second
        var three = ShareClass.Variable.value
        var four = keychain.get('name')
        var five = self.variableNameFive
        var six = firstParameter
        var seven = secondParameter
    }
}

之后,您可以像这样使用ViewController:
class CustomViewController: CustomClass {
   //If you want to add VC specific actions to this method, you can override it
    override func example(_ firstParameter: String, _ secondParameter: String) {
        super.example(firstParameter, secondParameter)
        print("something more just for this VC")
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        example("First Param", "Second param")
    }
}

10-08 07:32