在我的应用程序中,我将锻炼内容存储为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完成相同的工作?
答案 0 :(得分:0)
您可以像下面一样更新块声明
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"])
}
希望这会有所帮助!