尝试在 iPhone 上复制文本时应用程序崩溃

App crashes when trying to copy text on iPhone

我编写了一个“加密应用程序”,以简单的方式加密文本。这是代码:

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)

所以我可以快速将加密文本插入另一个应用程序(例如 WhatsApp)我使用此代码(简化):

let pasteboard: UIPasteboard = UIPasteboard.general
pasteboard.string! = "Hello World!"

代码在模拟器中运行:加密文本被复制,我可以双击粘贴它(见图)。

Paste encrypted text with a double click

但是当我 运行 我的手机上的应用程序 phone (iPhone 8),应用程序此时崩溃了!

有没有人知道解决方案或至少知道为什么?

此处的解决方案是不强制解包代码中的可选变量。
我强烈建议阅读 Swift here.

中有关 Optionals 的更多信息

let input: String = encryptTextField.text!
UITextField 中的文本可以为 nil。数据类型为 String optional(String?)

使用 if let 语句来安全地解包可选值,而不是上面的方法!

if let input = encryptTextField.text{
   // Your logic/code to process input as String goes here
}

同样,您可以删除此处完成的强制展开("!")。
1。 encryptTextField.text! = ""。只需使用 encryptTextField.text = ""
2。 pasteboard.string! = value 您可以删除此处完成的强制展开 ("!")。
3。 pasteboard.string! = "Hello World!"

基本上只有在您确定可选变量持有的值不为零时才强制解包变量!

强制展开包含 nil 值的可选变量会使您的应用程序崩溃!