gpt4 book ai didi

ios - 消息显示为空白?

转载 作者:行者123 更新时间:2023-11-30 13:50:50 25 4
gpt4 key购买 nike

我有一些代码,可以从文本框中获取文本并通过 POST 消息将其发送到服务器,然后服务器将其发送到电子邮件地址。

class ViewController: UIViewController {
@IBOutlet weak var emailText: UITextView!


override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

func sendEmail() {

let url = NSURL(string:"http://www.fakewebsite.com/email.php")
let cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringLocalCacheData
var request = NSMutableURLRequest(URL: url!, cachePolicy: cachePolicy, timeoutInterval: 2.0)
request.HTTPMethod = "POST"

// set Content-Type in HTTP header
let boundaryConstant = "----------V2ymHFg03esomerandomstuffhbqgZCaKO6jy";
let contentType = "multipart/form-data; boundary=" + boundaryConstant
NSURLProtocol.setProperty(contentType, forKey: "Content-Type", inRequest: request)

// set data
var dataString = emailText.text
let requestBodyData = (dataString as NSString).dataUsingEncoding(NSUTF8StringEncoding)
request.HTTPBody = requestBodyData

// set content length
//NSURLProtocol.setProperty(requestBodyData.length, forKey: "Content-Length", inRequest: request)

var response: NSURLResponse? = nil
var error: NSError? = nil
let reply = NSURLConnection.sendSynchronousRequest(request, returningResponse:&response, error:&error)

let results = NSString(data:reply!, encoding:NSUTF8StringEncoding)
println("API Response: \(results)")
}

@IBAction func sendButton(sender: AnyObject) {
sendEmail()
println(emailText.text)
}
}

按下按钮时会收到电子邮件,但电子邮件中没有任何内容,只是空白。是的,文本字段中有文本。你能看出我哪里可能出错了吗?

感谢任何帮助,谢谢。

最佳答案

您的文字无法显示的原因有多种:

--> Storyboard和 ViewController 之间的映射以某种方式被切断。尝试重新连接它们。

--> 你可以尝试专门使用“self”

var dataString = self.emailText.text;

--> 您可以使用 NSData 提供的以下初始化程序:

NSData(base64String:String, options:NSDataBase64DecodingOptions)

--> 放置一个调试点或编写一个简单的打印语句来显示当前数据的长度:

let requestBodyData = (dataString as NSString).dataUsingEncoding(NSUTF8StringEncoding)
//if this print says length is 0 then you dont seem to a valid link between the storyboard and the ViewController
print(requestBodyData.length)
request.HTTPBody = requestBodyData

关于ios - 消息显示为空白?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34304024/

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