使用多个字符串创建 onDoneBlock

Creating onDoneBlock with multiple strings

在我的应用程序中,我将锻炼存储为 NSDictionary(练习、组、次数、公斤和笔记),但我想将每个人存储为一个字符串。我有自己的 class 用于创建锻炼 (ExerciseCreator.swift)。 onDoneBlock 是这样的:var onDoneBlock : ((NSDictionary) -> Void)?

现在代码是这样使用的:

//Creating a dictionary from the exercise values
var exerciseDictionary: NSDictionary!

exerciseDictionary =  ["Exercise" : exerciseNameTextField.text!, "Sets" : String(format: "%d", setCount), "Reps" : String(format: "%d", repsCount), "KG" : String(format: "%d", kgCount)]

//Closing modal and refreshing tableview on parent view controller
if let callback = self.onDoneBlock {
       callback (exerciseDictionary)
}

在我的主视图控制器中,代码如下:

let exerciseCreator = ExerciseCreator()
        exerciseCreator.viewReady = {() -> Void in}
        exerciseCreator.onDoneBlock = {(dict) -> Void in
            self.dismiss(animated: true, completion: {
                self.exercisesArray.add(dict)
                self.tableView.reloadData()
                print("Exercises: \(self.exercisesArray)")
            })
        }

如何使用字符串而不是 NSDictionary 来完成相同的操作?

您可以像下面这样更新您的块声明

var onDoneBlock : ((Array<Any>) -> Void)?

同时更新参数名称如下

self.onDoneBlock = {(arrayVal) -> Void in
      self.dismiss(animated: true, completion: {
          self.exercisesArray.add(arrayVal)
          self.tableView.reloadData()
          print("Exercises: \(self.exercisesArray)")
      })
}

并像这样传递数组

//Closing modal and refreshing tableview on parent view controller
if let callback = self.onDoneBlock {
      callback (arrayObject)
}

编辑:

对于像这样的字符串数组传递值

if let callback = self.onDoneBlock {
     callback (["Test1","Test2","Test3"])
}

希望这对您有所帮助!