Ios 试图在iPhone上复制文本时,应用程序崩溃
我编写了一个加密应用程序,用一种简单的方式对文本进行加密。代码如下:Ios 试图在iPhone上复制文本时,应用程序崩溃,ios,swift,string,uipasteboard,Ios,Swift,String,Uipasteboard,我编写了一个加密应用程序,用一种简单的方式对文本进行加密。代码如下: let smallLetters: String = "abcdefghijklmnopqrstuvwxyz" let bigLetters: String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" let input: String = encryptTextField.text! encryptTextField.text! = "&
let smallLetters: String = "abcdefghijklmnopqrstuvwxyz"
let bigLetters: String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
let input: String = encryptTextField.text!
encryptTextField.text! = ""
var value: String = ""
for c in input {
let otherChars: String = " !§$%&/()=?´`#+'*-_.:,;<>^°@€{}≠¿|][¢¶“¡≤≈ç√∫~µ@€öäüÜÖÄ"
for i in otherChars {
if c == i {
value += String(i)
print("i")
}
}
var dCount: Int = 0
var eCount: Int = 0
var dFound: Bool = false
var eFound: Bool = false
for d in smallLetters {
if !dFound {
if c == d {
dFound.toggle()
let y: Int = smallCount - dCount
var z: Int = 1
for i in smallLetters {
if y == z {
print("[\(c) -> \(i)]")
value += String(i)
}
z += 1
}
} else {
dCount += 1
}
}
}
for e in bigLetters {
if !eFound {
if c == e {
eFound.toggle()
let y: Int = bigCount - eCount
var z: Int = 1
for i in bigLetters {
if y == z {
print("[\(c) -> \(i)]")
value += String(i)
}
z += 1
}
} else {
eCount += 1
}
}
}
}
let maximumChars: Int = 15
var string: String = ""
var i: Int = 1
var b: Bool = false
for c in value {
if !b {
if i <= maximumChars {
string += String(c)
} else {
string += "..."
b = true
}
i += 1
}
}
let alert: UIAlertController = UIAlertController(title: "Encoded!", message: "Your input is now encrypted / decrypted.", preferredStyle: UIAlertController.Style.alert)
let cancel: UIAlertAction = UIAlertAction(title: "OK", style: UIAlertAction.Style.cancel, handler: { (alert: UIAlertAction!) in
self.encryptTextField.text! = value
print("OK")
self.encryptTextField.placeholder = "Enter a text to encrypt..."
})
let copy: UIAlertAction = UIAlertAction(title: "Copy!", style: UIAlertAction.Style.default, handler: { (alert: UIAlertAction!) in
print("Copy!")
let pasteboard: UIPasteboard = UIPasteboard.general
pasteboard.string! = value
self.encryptTextField.placeholder = "'" + string + "' was copied!"
})
alert.addAction(cancel)
alert.addAction(copy)
present(alert, animated: true, completion: nil)
代码在模拟器中工作:加密的文本被复制,我可以通过双击see picture将其粘贴
但当我在手机iPhone8上运行应用程序时,应用程序在这一点上崩溃了
有人知道解决方案或者至少知道原因吗?这里的解决方案是不强制展开代码中的可选变量。 我强烈建议您阅读更多有关Swift中Optionals的信息 让输入:String=encryptTextField.text! UITextField中的文本可以为零。数据类型是字符串optionalString 使用if-let语句来安全地展开选项,而不是上面提到的 如果让输入=encryptTextField.text{ //您处理字符串输入的逻辑/代码如下 } 同样,也可以移除强制展开!在这里完成。 1.encryptTextField.text!=。只需使用encryptTextField.text= 2.pasteboard.string!=值可以移除强制展开!在这里完成。 3.pasteboard.string!=你好,世界 基本上只有在您确信可选变量所持有的值不是零的情况下才强制展开变量
强制展开包含nil值的可选变量将使应用程序崩溃 异常/崩溃消息是什么?我不知道。我无法看到错误消息,因为我正在手机上运行应用程序。当手机与Mac保持连接时,错误消息是否显示在Xcode中?此时应用程序不会崩溃。它只有在我按下代码中下面AlertController中的复制按钮时才会崩溃是的,这是由于可选的pasteboard.string!=强制展开的值。您可以像pasteboard.string=value一样使用它:我列出了所有执行强制展开的位置,这样您也可以避免在这些位置发生崩溃。很抱歉,我没有正确理解您的答案。我修改了代码,并在iPhone上再次运行了应用程序。现在它开始工作了!感谢您的快速回复:
let pasteboard: UIPasteboard = UIPasteboard.general
pasteboard.string! = "Hello World!"