首页 > 解决方案 > 尝试在 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 = " !§$%&/()=?´`#+'*-_.:,;<>^°@€{}≠¿|][¢¶“¡≤≈ç√∫~µ@€öäüÜÖÄ&quot;
        
        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!"

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

双击粘贴加密文本

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

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

标签: iosswiftstringuipasteboard

解决方案


这里的解决方案是不要强制解开代码中的可选变量。 我强烈建议在这里
阅读更多关于 Swift 中的 Optionals 的信息。

let input: String = encryptTextField.text!
UITextField 中的文本可以为零。数据类型是 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 的情况下才强制解包变量!

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


推荐阅读