不加班小技巧 2 映射状态交互

我们平时需要做的很多重复性工作例如服务器的返回状态对应不同的弹窗提示,封装弹窗提示是非常必要的,首先给出弹窗警告、弹窗确认、弹窗输入获取返回字符串、弹窗选择等:

// 弹窗警告
func alert(_ title: String?, _ message: String?) {
    DispatchQueue.main.async {
        let current = UIViewController.current()
        let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "确定", style: .default, handler: nil))
        current?.present(alert, animated: true, completion: nil)
    }
}

//弹窗确认
func confirm(_ title: String?, _ message: String?, choose: ( @escaping (_ confirm: Bool) -> ())) {
    let current = UIViewController.current()
    let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
    alert.addAction(UIAlertAction(title: "取消", style: .cancel, handler: { (action) in
        choose(false)
    }))
    alert.addAction(UIAlertAction(title: "确定", style: .default, handler: { (action) in
        choose(true)
    }))
    current?.present(alert, animated: true, completion: nil)
}

//弹窗确认
func alert(_ title: String?, _ message: String?, click: ( @escaping () -> ())) {
    let current = UIViewController.current()
    let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
    alert.addAction(UIAlertAction(title: "确定", style: .default, handler: { (action) in
        click()
    }))
    current?.present(alert, animated: true, completion: nil)
}

//弹窗输入 secret input 是密码输入类型,两种输入类型的结果会拼接在返回的contents里,按序取
func alertInput(_ title: String?, _ message: String?, inputPlaceholders: [String]?, secretInputPlaceholders: [String]?, userResponse: @escaping ((_ entry: Bool, _ inputContents: [String]) -> ())) {
    let current = UIViewController.current()
    let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)

    if inputPlaceholders != nil {
        for placeholder in inputPlaceholders! {
            alert.addTextField { (tf) in
                tf.placeholder = placeholder
            }
        }
    }
    if secretInputPlaceholders != nil {
        for placeholder in secretInputPlaceholders! {
            alert.addTextField { (tf) in
                tf.placeholder = placeholder
                tf.isSecureTextEntry = true
            }
        }
    }

    alert.addAction(UIAlertAction(title: "取消", style: .cancel, handler: { (action) in
        userResponse(false, [])
    }))
    alert.addAction(UIAlertAction(title: "确定", style: .default, handler: { (action) in
        var contents = [String]()
        for tf in alert.textFields ?? [] {
            contents.append(tf.text ?? "")
        }
        userResponse(true, contents)
    }))
    current?.present(alert, animated: true, completion: nil)
}

// 弹窗选择
func alertSheet(_ title: String?, _ message: String?, list: [String], result: @escaping ((_ index: Int) -> ())) {
    let current = UIViewController.current()
    let alert = UIAlertController(title: title, message: message, preferredStyle: .actionSheet)
    var index = 0
    for title in list {
        let action = UIAlertAction(title: title, style: .default, handler: { (action) in
            let tag = action.info["tag"] as! Int
            result(tag)
        })
        action.info = ["tag": index]
        alert.addAction(action)
        index += 1
    }
    alert.addAction(UIAlertAction(title: "取消", style: .cancel, handler: nil))
    current?.present(alert, animated: true, completion: nil)
}

使用封装好的alert()方法后,我们可以这样区分状态并弹窗:

			guard status >= 0 else {
                var promt = ""
                switch status {
                case -1://-1表示操作失败
                    promt = "操作失败"
                    break
                case -2://-2表示手机号已注册
                    promt = "手机号已注册"
                    break
                case -3://-3表示客户端未登陆
                    promt = "客户端未登陆"
                    break
                case -4://-4表示验证超时
                    promt = "验证超时"
                    break
                case -5://-5表示验证码不正确
                    promt = "验证码不正确"
                    break
                default: break
                }
                alert(promt, nil)
                return
            }

但是用起来还是特别繁琐,所以我们继续封装状态的区分:

func switchStatusAndPromt(_ value: Any, _ status: [Any], _ promt: [String]) -> String {
    var switchPromt = ""
    let value = String(format: "%@", value as? NSObject ?? NSObject())
    var index = 0
    for i in status.indices {
        let item = status[i]
        let compare = String(format: "%@", item as? NSObject ?? NSObject())
        if compare == value {
            index = i
        }
    }
    switchPromt = promt[index]
    return switchPromt
}

这样我们就可以根据数据获得预设的状态值和提示的映射,使用起来是这样的:

let status = JSON(statusString).intValue
guard status >= 0 else {
	alert(switchStatusAndPromt(status, [-1, -2, -3, -4, -5], ["操作失败", "手机号已注册", "客户端未登陆", "验证超时", "验证码不正确"]), nil)
	return
}

效果:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值