【问题标题】:Creating onDoneBlock with multiple strings使用多个字符串创建 onDoneBlock
【发布时间】:2019-02-16 19:25:05
【问题描述】:

在我的应用程序中,我将锻炼存储为 NSDictionary(练习、集、代表、公斤和笔记),但我想将每个人存储为字符串。我有一个自己的课程来创建锻炼(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?

【问题讨论】:

  • 你在哪里定义你的完成块?
  • @CodeChanger var onDoneBlock : ((NSDictionary) -> Void)?if let callback = self.onDoneBlock { 在一个名为ExerciseCreator.swift 的类中。用法(最后一个代码)在我的主视图控制器中(HomeViewController.swift)

标签: ios arrays swift xcode nsdictionary


【解决方案1】:

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

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"])
}

希望这会有所帮助!

【讨论】:

  • 感谢您的贡献,我的问题有误。我提到我想传递多个字符串,而不是数组。现在更新问题。
  • 根据我的回答,您可以在该块中传递字符串数组,因为我使用 对象制作它,这样您就可以在其中传递字符串数组。
猜你喜欢
  • 2016-01-11
  • 1970-01-01
  • 1970-01-01
  • 2015-09-13
  • 2019-12-29
  • 2012-03-12
  • 2021-06-26
  • 1970-01-01
  • 2016-07-09
相关资源
最近更新 更多