gpt4 book ai didi

arrays - 使用 textFieldDidEndEditing 文本字段委托(delegate)查找数组中的 3 个元素

转载 作者:行者123 更新时间:2023-11-30 10:28:03 25 4
gpt4 key购买 nike

下面我的 swift 代码在文本字段 var EnterT 中使用 UITextFieldDelegate。 nameString 将核心数据实体转换为字符串数组,例如 [bob,bryan,jessica]。在 textFieldDidEndEditing 中,我希望用户能够输入任何数字,例如要打印的数组中的 3 和第三个元素,然后对数组进行排序。

enter image description here

import UIKit
import CoreData

class ViewController: UIViewController,UITextFieldDelegate {
@IBOutlet var labelName : UILabel!
@IBOutlet var enterT : UITextField!

let appDelegate = UIApplication.shared.delegate as! AppDelegate //Singlton instance
var context:NSManagedObjectContext!

override func viewDidLoad() {
super.viewDidLoad()

openDatabse()




}

func textFieldDidEndEditing(_ textField: UITextField) {
guard let index = Int(textField.text!) else {
// display an alert about invalid text
return
}
joke(at: index)
}

func joke(at index : Int) {
let fetchRequest = NSFetchRequest<Users>(entityName: "Users")
fetchRequest.predicate = NSPredicate(format: "idx == %d", Int32(index))
do {
if let user = try context.fetch(fetchRequest).first {
print(user.username)
}
} catch {
print("Could not fetch \(error) ")
}
}


func openDatabse()
{
context = appDelegate.persistentContainer.viewContext
let entity = NSEntityDescription.entity(forEntityName: "Users", in: context)
let newUser = NSManagedObject(entity: entity!, insertInto: context)
let newUser2 = NSManagedObject(entity: entity!, insertInto: context)
let newUser3 = NSManagedObject(entity: entity!, insertInto: context)
saveData(UserDBObj: newUser, UserDBObj2: newUser2, UserDBObj3: newUser3)

}

func saveData(UserDBObj:NSManagedObject,UserDBObj2:NSManagedObject,UserDBObj3:NSManagedObject)
{
UserDBObj.setValue("kim kardashian", forKey: "username")
UserDBObj2.setValue("jessica biel", forKey: "username")
UserDBObj3.setValue("Hailey Rienhart", forKey: "username")




print("Storing Data..")
do {
try context.save()
} catch {
print("Storing data Failed")
}

fetchData()
}

func fetchData()
{
print("Fetching Data..")
let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Users")
request.returnsObjectsAsFaults = false
do {
let result = try context.fetch(request)
for data in result as! [NSManagedObject] {
let userName = data.value(forKey: "username") as! String

print("User Name is : "+userName)
}
} catch {
print("Fetching data Failed")
}
}}

gif of what I am looking for

最佳答案

将输入的数字转换为Int。如果成功,则将整数传递给 joke 并获取与 idx 属性匹配的记录。

考虑索引从零开始。如果您想输入以 1 开头的数字,则必须递减索引 (joke(at: index - 1))

@IBOutlet var enterT : UITextField!

func textFieldDidEndEditing(_ textField: UITextField) {
guard let index = Int(textField.text!) else {
// display an alert about invalid text
return
}
joke(at: index)
}

func joke(at index : Int) {
let fetchRequest = NSFetchRequest<Users>(entityName: "Users")
fetchRequest.predicate = NSPredicate(format: "idx == %d", Int32(index))
do {
if let user = try context.fetch(fetchRequest).first {
print(user.username)
}
} catch {
print("Could not fetch \(error) ")
}
}

注意:我建议在核心数据模型中将 idxusername 定义为非可选。并确保所有 idx 值都是唯一的。

关于arrays - 使用 textFieldDidEndEditing 文本字段委托(delegate)查找数组中的 3 个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59720059/

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