博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
swift中数据之间的转换
阅读量:5162 次
发布时间:2019-06-13

本文共 3649 字,大约阅读时间需要 12 分钟。

1.swift 开发 - NSDictionary与NSData互转、Dictionary与Data互转

1、NSDictionary转NSData

let data:NSData =NSKeyedArchiver.archivedData(withRootObject:dataDic) asNSData
2、NSData转NSDictionary
let dic =NSKeyedUnarchiver.unarchiveObject(with: dataas! Data)as! NSDictionary
3、NSDictionary转Data
let date =try! JSONSerialization.data(withJSONObject: dic, options: .prettyPrinted)
4、Data转Dictionary
let json =try! JSONSerialization.jsonObject(with: data, options: .mutableContainers)as! Dictionary<String,String>

2.(UITextField.text as NSSTring).floatValue

使用一个label 显示多行

private func setData(showStr: String) {

        DispatchQueue.main.async {

            self.leftTipLabel!.text = NSLocalizedString("可用", comment: "")+"\n\n"+NSLocalizedString("最小转帐量", comment: "")+"\n\n"+NSLocalizedString("手续费", comment: "")+"\n\n"+NSLocalizedString("到账数量", comment: "")

        }

        self.leftTipLabel?.numberOfLines = 0

        let attributedStrM : NSMutableAttributedString = NSMutableAttributedString()

        attributedStrM.append(NSAttributedString(string: available, attributes: [NSAttributedString.Key.foregroundColor: UIColor.hexadecimalColor(hexadecimal: "#F1B11B")]))

        attributedStrM.append(NSAttributedString(string: " \(dataDict["currency"].stringValue.uppercased())"+"\n\n", attributes: [NSAttributedString.Key.foregroundColor: ThemeColor.sharedInstance.getTextColor()]))

        attributedStrM.append(NSAttributedString(string: String(format: "%.3f", ((self.minWithdrawQuantity as String) as NSString).doubleValue), attributes: [NSAttributedString.Key.foregroundColor: UIColor.hexadecimalColor(hexadecimal: "#F1B11B")]))

        attributedStrM.append(NSAttributedString(string: NSLocalizedString("个",  comment: "")+"\n\n", attributes: [NSAttributedString.Key.foregroundColor:ThemeColor.sharedInstance.getGrayTextWhiteColor()]))

        var all:Double = 0

        var totalFees:Double = 0

        if self.moneyTextField?.text?.count == 0 {

            all = 0

            totalFees = 0

        }else if (self.moneyTextField!.text! as NSString).doubleValue < (self.minWithdrawQuantity as NSString).doubleValue{

            all = 0

            totalFees = 0

        }else{

            

            let inputf:Double = Double((showStr as NSString).doubleValue)

            let feesp:Double = Double((self.feeS as NSString).doubleValue) * inputf

            if feesp > Double((self.firstFee as NSString).doubleValue) {

                totalFees = Double((self.firstFee as NSString).doubleValue)

                all = (showStr as NSString).doubleValue - totalFees

                

            }else{

                totalFees = feesp

                all = Double((showStr as NSString).doubleValue) - totalFees

                

            }

        }

        //手续费  "\(totalFees)"

        attributedStrM.append(NSAttributedString(string: String(format: "%.3f", totalFees), attributes: [NSAttributedString.Key.foregroundColor: UIColor.hexadecimalColor(hexadecimal: "#F1B11B")]))

        attributedStrM.append(NSAttributedString(string: NSLocalizedString("个",  comment: "")+"\n\n", attributes: [NSAttributedString.Key.foregroundColor:ThemeColor.sharedInstance.getGrayTextWhiteColor()]))

        

        //到账数量  //"\(all - totalFees)"

        attributedStrM.append(NSAttributedString(string: String(format: "%.3f", all), attributes: [NSAttributedString.Key.foregroundColor: QBColor_Orange]))

        attributedStrM.append(NSAttributedString(string: " \(self.currentCurrency!)", attributes: [NSAttributedString.Key.foregroundColor:ThemeColor.sharedInstance.getGrayTextWhiteColor()]))

        

        

        self.rightTipLabel?.textAlignment = NSTextAlignment.right

        self.rightTipLabel!.attributedText = attributedStrM

        self.rightTipLabel?.numberOfLines = 0

        

        

        DispatchQueue.main.async {

            self.bottomTipLabel!.text = NSLocalizedString("手续费率", comment: "") + "\(self.feeS)" + ", " + NSLocalizedString("单笔不超过", comment: "") + "\(self.firstFee)" + "\(self.currentCurrency!)" + NSLocalizedString("多少手续费", comment: "")

        }

        

        self.view.layoutIfNeeded()

    }

 

转载于:https://www.cnblogs.com/sundaysme/p/11600689.html

你可能感兴趣的文章
bzoj4447 SCOI2015 小凸解密码 password
查看>>
解析Ceph: RBDCache 背后的世界
查看>>
qt安装遇到的错误
查看>>
Linux下which、whereis、locate、find 区别
查看>>
h.264加权预测
查看>>
as4 通过yum自动升级实现
查看>>
学习springMVC实例1——配置和跳转到HelloWorld
查看>>
寻找完美平方数
查看>>
初学反编译-.-
查看>>
防御式编程
查看>>
单线程并发的server端
查看>>
View可以设置tag携带数据
查看>>
individual reading task ---12061183 叶露婷
查看>>
delphi的消息对话框
查看>>
java:Apache Shiro 权限管理
查看>>
38.输出1到最大的N位数[Print 1 to max number of N bits]
查看>>
ZOJ - 2165 Red and Black
查看>>
objective c的注释规范
查看>>
FreeNas安装配置使用
查看>>
机器学习中的F1-score
查看>>