Swift 3代码更新

时间:2016-11-16 13:45:45

标签: json swift syntax

class SignUp: UIViewController {

    @IBOutlet weak var buttonNameTxt: UITextField! @IBOutlet weak var buttonEmailTxt: UITextField! @IBOutlet weak var buttonPwdTxt: UITextField!

    override func viewDidLoad() { super.viewDidLoad()

    }

    @IBAction func buttonSignIn(_ sender: UIButton){

        let usermainname:NSString = buttonNameTxt.text! as NSString let username:NSString = buttonEmailTxt.text! as NSString let password:NSString = buttonPwdTxt.text! as NSString

        let myURL = NSURL(string: "https:\mev.com/api/login")
        let request = NSMutableURLRequest(url: myURL as! URL) request.httpMethod = "Post"

        let postString = "name = (usermainname) email = (username) &passwod = (password) " request.httpBody = postString.data(using: String.Encoding.utf8)

        let task = URLSession.shared.dataTask(with: request as URLRequest) { data , response , error in

            do {

                let err: NSError? let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary

                print(json as Any)

                if let parseJSON = json { let resultValue = parseJSON["status"] as? String print ("result:(resultValue)")

                    var isUserRegister:Bool = false if resultValue == "success" {isUserRegister = true}

                    var messageToDisplay:String = parseJSON["massage"] as! String if isUserRegister {

                        messageToDisplay = parseJSON["massage"] as! String

                    }

                }

            } catch {

                print("error")
            }
        }
    }
}

这是错误

  

2016-11-16 12:37:16.328505 MevicsPromo [12011:692688] [MC]系统   systemgroup.com.apple.configurationprofiles路径的组容器   是   /Users/HardBuf/Library/Developer/CoreSimulator/Devices/E3B4C6A4-A49D-4363-89D5-31931EA513A8/data/Containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles   2016-11-16 12:37:16.328782 MevicsPromo [12011:692688] [MC]阅读   私人有效用户设置。 2016-11-16 12:37:18.276331   MevicsPromo [12011:692798] 0x60000014e4f0复制匹配资产回复:   XPC_TYPE_DICTIONARY {count = 1,事务:0,凭证= 0x0,   contents =“结果”=> :29} 2016-11-16 12:37:18.277030   MevicsPromo [12011:692798] 0x608000357b80守护程序配置查询   回复:XPC_TYPE_DICTIONARY {count = 2,事务:0,凭证= 0x0,   contents =“Dictionary”=> {count = 1,事务:0,凭证= 0x0,   contents =“ServerURL”=> {count = 3,transaction:0,voucher = 0x0,   contents =“com.apple.CFURL.magic”=>   C3853DCC-9776-4114-B6C1-FD9F51944A6D“com.apple.CFURL.string”=> {   length = 30,contents =“https:\ mesu.apple.com/assets/”}   “com.apple.CFURL.base”=> :null-object}}“结果”=> :0}   2016-11-16 12:37:18.277318 MevicsPromo [12011:692798]   http:\ ru.stackoverflow.com/editing-help [MobileAssetError:29]无法使用   从https:\ mesu.apple.com/assets/复制资产信息以获取资产   输入com.apple.MobileAsset.TextInput.SpellChecker

1 个答案:

答案 0 :(得分:0)

我使用Swift 3语法修复了你的代码。

class SignUp: UIViewController {

@IBOutlet weak var buttonNameTxt: UITextField!
@IBOutlet weak var buttonEmailTxt: UITextField!
@IBOutlet weak var buttonPwdTxt: UITextField!

override func viewDidLoad() {
    super.viewDidLoad()
}

@IBAction func buttonSignIn(_ sender: UIButton) {

    let usermainname = buttonNameTxt.text!
    let username = buttonEmailTxt.text!
    let password = buttonPwdTxt.text!

    let myURL = URL(string: "https://mev.com/api/login")
    let request = NSMutableURLRequest(url: myURL!)
    request.httpMethod = "Post"

    let postString = "name = \(usermainname) email = \(username) &passwod = \(password)"
    request.httpBody = postString.data(using: String.Encoding.utf8)

    let task = URLSession.shared.dataTask(with: request as URLRequest) { data , response , error in

        do {
            if let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary {

                let resultValue = json["status"] as? String
                print("result: \(resultValue)")

                if resultValue == "success", let messageToDisplay = json["massage"] as? String {
                    print("\(messageToDisplay)")
                }
            }

        } catch {

            print("error")
        }
    }
    task.resume()
}
}