gpt4 book ai didi

ios - 有没有办法强制 Swift 中的 TextField 中的文本是非可选的?

转载 作者:搜寻专家 更新时间:2023-11-01 06:25:20 26 4
gpt4 key购买 nike

我正在制作一个应用程序,该应用程序从 alertController 中的文本字段获取输入,然后存储到 CoreData 中并最终显示在 tableView 中。

目前的计划是通过将 textField.text 组合在一起来创建一个字符串来创建 CSV,例如

let string CSV = textField.text + " ," + textField2.text + " ," + ...

但是,当我尝试这样做时,他们说我不能强制展开一个可选值。

我试着放一个“!”在 textField.text 等中,但它们仍然被视为可选值。文本字段在应用程序中使用时必须始终填写,但我找不到一种方法来提供强制应用程序用户填写值的错误。

这是我的 View Controller :

import UIKit
import CoreData

class ViewController: UITableViewController {

var alarmItems: [NSManagedObject] = []
let cellId = "cellId"

override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
navigationController?.navigationBar.barTintColor = UIColor(red: 21/255, green: 101/255, blue: 192/255, alpha: 1)
navigationController?.navigationBar.tintColor = .white
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Add Alarm", style: .plain, target: self, action: #selector(addAlarmItem))
tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
let managedContext = appDelegate.persistentContainer.viewContext
let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "AlarmItems")
do {
alarmItems = try managedContext.fetch(fetchRequest)
} catch let err as NSError {
print("Failed to fetch items", err)
}
}

//now to figure out how to add multiple text fields

@objc func addAlarmItem(_ sender: AnyObject) {
print("this works")
let alertController = UIAlertController(title: "Add New Item", message: "Please fill in the blanks", preferredStyle: .alert)
let saveAction = UIAlertAction(title: "Save", style: .default) { [unowned self] action in
guard let textField = alertController.textFields!.first, let itemToAdd = textField.text else { return }

//guard let textField2 = alertController.textFields?.first, let itemToAdd2 = textField2.text else { return } //tried this to get the input of a second textField

/*

one thing I tried:
let textField1 = alertController.textFields[0]
let textField1String = textField1.text!

let textField2 = alertController.textFields![1]
let textField2String = textField2.text
let itemToAdd = textField1String + textField2String
*/



//var textField1String = textField1.text
//let textField2 = alertController.textFields![1]
//var textField2String = textField2.text


self.save(itemToAdd)
//self.save(itemToAdd2)
self.tableView.reloadData()
}
let cancelAction = UIAlertAction(title: "Cancel", style: .destructive, handler: nil)
//alertController.addTextField(configurationHandler: nil)
alertController.addTextField { (textField) in
textField.placeholder = "First"
}
alertController.addTextField { (textField) in
textField.placeholder = "Second"
}

//let combinedString = UITextField[0] + " ," + UITextField[1]
alertController.addAction(saveAction)
alertController.addAction(cancelAction)
present(alertController, animated: true, completion: nil)
}

func save(_ itemName: String) {
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
let managedContext = appDelegate.persistentContainer.viewContext
let entity = NSEntityDescription.entity(forEntityName: "AlarmItems", in: managedContext)!
let item = NSManagedObject(entity: entity, insertInto: managedContext)
item.setValue(itemName, forKey: "alarmAttributes")

do {
try managedContext.save()
alarmItems.append(item)

} catch let err as NSError {
print("Failed to save an item", err)
}
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return alarmItems.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
let alarmItem = alarmItems[indexPath.row]
cell.textLabel?.text = alarmItem.value(forKeyPath: "alarmAttributes") as? String
return cell
}
}

最佳答案

您可以使用 compactMap 轻松地将可选数组转换为非可选数组。

let myStrings: [String] = alert.textfields!.compactMap { $0.text }
let myText = myStrings.joined(separator: ", ")

要了解有关 compactMap 的更多信息,请转到 https://developer.apple.com/documentation/swift/sequence/2950916-compactmap

关于ios - 有没有办法强制 Swift 中的 TextField 中的文本是非可选的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56590795/

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