search

Home  >  Q&A  >  body text

ios - swift3中字符串转成方法的时候,怎么接收返回值?

核心代码如下

var tweakEntries = [(title : String, array : [Array<String>])]()

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.tableFooterView = UIView()
    navigationItem.title = "设置"
    //tableView.separatorStyle = .none

    let timeSettingArr = [
        ["工作时间间隔","workTime","setWorkTime"],
        ["休息时间间隔","relaxTime","setRelaxTime"],
    ]
    let _ = [
        ["通知标题","",""],
        ["通知语","",""]
    ]
    let _ = [
        ["显示背景图片","displayStatus","setDisplayStatus"]
    ]
    tweakEntries = [
        ("时间设置",timeSettingArr),
        //("通知设置",localNoti),
        //("其他设置",otherSettingArr)
    ]
}



override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let section = indexPath.section
    let row = indexPath.row
    let identifier  = "SettingCell"
    var cell = tableView.dequeueReusableCell(withIdentifier: identifier)
    if (cell == nil) {
        cell = SettingCell.init(style: .subtitle, reuseIdentifier: identifier)
    }
    cell?.textLabel?.text = tweakEntries[section].array[row][0]
    let methodName = tweakEntries[section].array[row][1];
    print("\(methodName)")
    let method : Selector = Selector(methodName)
    let detailText = perform(method)
    //let temp : CGFloat = detailText as! CGFloat
    //cell?.detailTextLabel?.text = "\(temp)"


    return cell!
}

func workTime() -> CGFloat {
    return 11.0
}

怎么接收 workTime () -> CGFloat 这个方法的返回值
看到perform 的返回值是 Unmanaged<AnyObject>!,不用 perform 的话还有其他相同功能的方法么?
public func perform(_ aSelector: Selector!) -> Unmanaged<AnyObject>!

下面是全部代码
code: https://github.com/CoderYLZha...

http://stackoverflow.com/ques...

阿神阿神2771 days ago356

reply all(1)I'll reply

  • 黄舟

    黄舟2017-04-18 09:52:09

    I don’t know why, but I always feel that what you do is anti-pattern.

    But here is an old idea: pass the variable storing the result as a pointer.

    func workTime() -> CGFloat {
       let temp : CGFloat
       /* 具体的操作 */
       return temp
    }

    Changed to:

    func workTime(inout result: CGFloat) -> void {
       let temp : CGFloat
       /* 具体的操作 */
       result = temp
    }

    reply
    0
  • Cancelreply