gpt4 book ai didi

iOS 发送电子邮件或短信时出错

转载 作者:行者123 更新时间:2023-11-30 14:10:29 24 4
gpt4 key购买 nike

我尝试从 iOS 应用发送短信和电子邮件,但遇到了一些问题。

我从教程中获取了代码,例如短信代码:

func sendMessage() {
let prefs = NSUserDefaults.standardUserDefaults()
var message = MFMessageComposeViewController()

// Contacts
var phoneNumbers = [String]()
var phoneNumberNb: Int = prefs.objectForKey("phonenumberssaved")!.count
for(var i = 0; i < phoneNumberNb; i++) {
phoneNumbers.append(prefs.objectForKey("phonenumberssaved")!.objectAtIndex(i) as! String)
}

println(phoneNumbers)

message.recipients = phoneNumbers
message.body = self.messageText
message.messageComposeDelegate = self

self.presentViewController(message, animated: false, completion: nil)
}

我打印了我的数据(它们似乎没问题),但是当我尝试发送它时,应用程序崩溃并显示

'fatal error: unexpectedly found nil while unwrapping an Optional value' and it show me a strange file with a line highlighted : '0x4b5164 <+64>: trap'...

最佳答案

使用条件展开。另外,您不需要处理每个数组元素:

func sendMessage() {
let prefs = NSUserDefaults.standardUserDefaults()
let message = MFMessageComposeViewController()

// Contacts
if let phoneNumbers = prefs.objectForKey("phonenumberssaved") as? [String] {
println(phoneNumbers)

message.recipients = phoneNumbers
message.body = "some body"
message.messageComposeDelegate = self

self.presentViewController(message, animated: false, completion: nil)
}
}

关于iOS 发送电子邮件或短信时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31829469/

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