gpt4 book ai didi

ios - 从警报文本字段获取值

转载 作者:行者123 更新时间:2023-11-30 12:49:32 27 4
gpt4 key购买 nike

我目前正在尝试从 swift 3 中的警报框中获取值。

下面的代码用于提示警报并保存数据,但是,我在回调数据和操作它方面遇到了麻烦,所以它只是一个基本字符串。

 func presentAlert() {
let alertController = UIAlertController(title: "IP?", message: "Please input your unique key:", preferredStyle: .alert)

let confirmAction = UIAlertAction(title: "Confirm", style: .default) { (_) in
if let field = alertController.textFields?[0] {
// store it
UserDefaults.standard.set(field.text, forKey: "userIP")
UserDefaults.standard.synchronize()
} else {
// user did not fill field
print("no input given")
}
}

let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (_) in }

alertController.addTextField { (textField) in
textField.placeholder = "IP"
}

alertController.addAction(confirmAction)
alertController.addAction(cancelAction)

self.present(alertController, animated: true, completion: nil)
}

这里调用这个方法:

override func viewDidAppear(_ animated: Bool) {
presentAlert()
}

我试图调用它并将其分配在字符串之间,如下所示:

let url_to_unlock:String = "http://\(UserDefaults.standard.value(forKey: "userIP")):3000/unLock"

但是,这给了我输出:

http://Optional():3000/unLock

当我尝试打印它时。

任何朝着正确方向的插入都将不胜感激。添加的类:

class ViewController: UIViewController {

func presentAlert() {
let alertController = UIAlertController(title: "IP?", message: "Please input your unique key:", preferredStyle: .alert)

let confirmAction = UIAlertAction(title: "Confirm", style: .default) { (_) in
if let field = alertController.textFields?[0] {
// store your data
//this could be lock unique key name etc in future
UserDefaults.standard.set(field.text, forKey: "userIP")
UserDefaults.standard.synchronize()
} else {
// user did not fill field
print("no input given")
}
}

let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (_) in }

alertController.addTextField { (textField) in
textField.placeholder = "IP"
}

alertController.addAction(confirmAction)
alertController.addAction(cancelAction)

self.present(alertController, animated: true, completion: nil)
}
//view did appear for the alert
override func viewDidAppear(_ animated: Bool) {
presentAlert()
}

//to post to an /unLock it must be put in the URL


// let url_to_unlock:String = "http://\(UserDefaults.standard.value(forKey: "userIP")):3000/unLock"
//let url_to_lock:String = "http://\(textField):3000/Lock"

let url_to_unlock:String = "http://10.73.195.218:3000/unLock"
let url_to_lock:String = "http://10.73.195.218:3000/Lock"

override func viewDidLoad() {
super.viewDidLoad()

}
var Timestamp: String {
return "\(NSDate().timeIntervalSince1970 * 1000)"
}
func un_lock()
{
print(url_to_unlock)
let url:URL = URL(string: url_to_unlock)!
let session = URLSession.shared

let request = NSMutableURLRequest(url: url)
request.httpMethod = "POST"
request.cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringCacheData

let paramString = "data=unLocking at \(Timestamp)"
request.httpBody = paramString.data(using: String.Encoding.utf8)

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

guard let _:Data = data, let _:URLResponse = response , error == nil else {
print("error")
return
}
//for errors
let dataString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
print(dataString! )

})

task.resume()
}

func lock()
{
let url:URL = URL(string: url_to_lock)!
let session = URLSession.shared

let request = NSMutableURLRequest(url: url)
request.httpMethod = "POST"
request.cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringCacheData

let paramString = "data=Locking at \(Timestamp)"
request.httpBody = paramString.data(using: String.Encoding.utf8)

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

guard let _:Data = data, let _:URLResponse = response , error == nil else {
print("error")
return
}
//for errors
let dataString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
print(dataString! )

})

task.resume()

}

@IBAction func lock(_ sender: UIButton) {
lock()
}

@IBAction func unLock(_ sender: Any) {
un_lock()

}
}

谢谢。

最佳答案

此值是可选的:

 // let url_to_unlock:String = "http://\(UserDefaults.standard.value(forKey: "userIP")):3000/unLock"

尝试:

let url = UserDefaults.standard.value(forKey: "userIP")!

let url_to_unlock:String = "http://\(url):3000/unLock"

关于ios - 从警报文本字段获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41171025/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com