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

查看:120
本文介绍了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...

解决方案

不知道为什么,总感觉你这样做是anti-pattern的。

不过提供一个古老的思路:把储存结果的变量当指针传进去。

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

改成:

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

这篇关于ios - swift3中字符串转成方法的时候,怎么接收返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆