Converting data between the swift

1.swift Development - NSDictionary and NSData system conversion, Dictionary and Data Huzhuan

https://blog.csdn.net/SuperMageHuang/article/details/54963236

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

Use a label to display multiple lines

private func setData(showStr: String) {

        DispatchQueue.main.async {

            Self ! .leftTipLabel .text = NSLocalizedString ( "available", comment: "") + "\ n \ n" + NSLocalizedString ( " minimum transfer amount", comment: "") + "\ n \ n" + NSLocalizedString ( " fee ", comment:" ") + " \ n \ n "+ NSLocalizedString (" arrival quantity ", 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

        was 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

                

            }

        }

        // fee "\ (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()]))

        

        // number of arrival // "\ (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 ( "commission rate", comment: "") + " \ ( Self .feeS)" + "," + NSLocalizedString ( "single no more than", comment: "") + " \ ( Self .firstFee) "+" \ ( Self ! .currentCurrency) "+ NSLocalizedString (" how much fee ", comment:" ")

        }

        

        self.view.layoutIfNeeded()

    }

 

Guess you like

Origin www.cnblogs.com/sundaysme/p/11600689.html