Ios 该应用程序在我的手机上运行,但在任何其他设备上都无法运行

Ios 该应用程序在我的手机上运行,但在任何其他设备上都无法运行,ios,swift,Ios,Swift,在提交应用程序之前,我花了几天的时间试图解决这个问题,结果就是这样。我正在开发一个应用程序,它使用UserDefaults通过按下按钮保存来自不同视图控制器的字符串,并在下一个视图控制器中将其转换为表视图。然而,由于一些未知的原因,该应用程序在iPhoneX(和iPhoneXR)上运行得非常好,但它不会保存字符串,每当我在不同型号的iPhone上运行该应用程序时,它都不会对按下按钮做出反应。这是我的密码: //FirstViewController @IBAction func buttonIs

在提交应用程序之前,我花了几天的时间试图解决这个问题,结果就是这样。我正在开发一个应用程序,它使用UserDefaults通过按下按钮保存来自不同视图控制器的字符串,并在下一个视图控制器中将其转换为表视图。然而,由于一些未知的原因,该应用程序在iPhoneX(和iPhoneXR)上运行得非常好,但它不会保存字符串,每当我在不同型号的iPhone上运行该应用程序时,它都不会对按下按钮做出反应。这是我的密码:

//FirstViewController

@IBAction func buttonIsPressed(_ sender: Any) {
    if var items = UserDefaults.standard.object(forKey: "items") as? [String]{
        var newitems = textField.text!.components(separatedBy: CharacterSet(charactersIn: ",  []()\n.:"))
        print(items)
        if newitems.contains(""){
            newitems.removeAll { $0 == ""}
            items.append(contentsOf: newitems)
            UserDefaults.standard.set(items, forKey: "items")
        }else{
            let newitems = textField.text!.components(separatedBy: CharacterSet(charactersIn: ",  []()\n.:"))
            UserDefaults.standard.set(newitems, forKey: "items")
        }

        textField.text = ""

        }
}
//第二视图控制器

var scannedText: String = "Detected text can be edited here." {
    didSet {
        textView.text = scannedText
        let str = scannedText.uppercased()
        let allergens = UserDefaults.standard.array(forKey: "items") as! [String]
        let string = str.components(separatedBy: CharacterSet(charactersIn: ", []()\n.:"))
        print(string)
        for allergen in allergens{
            if string.contains(String(Substring(allergen))) == true {
                print("I found the string \(allergen)")
                allegenLabel.text = "Not safe"
                allegenLabel.alpha = 1  //Make the label visible
                allegenLabel.textColor = .red

                // let attributedString = allergen.highlight([allergen], this: .red)
                // textView.attributedText = attributedString
                allergensFound.append(allergen)
                print(allergensFound)

            }
            if string.contains(String(Substring(allergen))) == false {
                allegenLabel.text = "Safe"
                allegenLabel.alpha = 1 //Make the label visible
                allegenLabel.textColor = UIColor.colorGreen
                //                    table.reloadData()
            }
        }

    }
}
如果您需要代码中的进一步说明,请询问我。谢谢


编辑:更确切地说,我正在创建一个代码,在字符串数组中找到一个字符串,然后用AllegenLabel.text通知用户。整个应用程序分为不同的视图控制器。用户使用FirstViewController中的文本字段输入字符串,然后必须在SecondViewController中的不同字符串数组中找到该字符串。由于某些原因,UserDefault成功地在我的手机上存储和检索字符串,但在不同的设备上不起作用。例如,每当我在FirstViewController中单击save按钮时,必须在控制台中存储并显示一个字符串,但由于未知原因,它无法工作。抱歉,如果我不清楚

我想你可以试试:

UserDefaults.standard.synchronize()

也许…

请更清楚地说明您(用户)做了什么,以及结果会发生什么或不会发生什么。更确切地说,我正在创建一个代码,在字符串数组中找到一个字符串,然后用AllegenLabel.text通知用户。整个应用程序分为不同的视图控制器。用户使用FirstViewController中的文本字段输入字符串,然后必须在SecondViewControl中的不同字符串数组中找到该字符串。每当我在FirstViewController中单击save按钮时,必须在控制台中存储并显示一个字符串,但由于某些未知原因,它在其他设备上不起作用,但在我的手机上起作用。对不起,我不清楚