연락처와 이름이 같은 2 개의 번호를 저장하려는 연락처가 있습니다.IOS - 동시에 여러 textField 입력에서 CoreData에 데이터를 저장하는 방법
내 viewController (textField1,textField2,textField3,textField4
)에 4 개의 textField가 있습니다. textField 1과 2에는 첫 번째 사람의 이름과 번호가 입력되고 textField 3과 4에는 두 번째 사람의 이름과 번호가 입력됩니다. 이제 saveButton을 클릭하면 네 개의 textField에 모두 입력 된 데이터가 'Contacts'라는 엔터티에 저장됩니다. 즉, 두 사람의 이름과 수를 엔티티에 저장해야합니다.
다음 코드를 사용하면 textField3
및 textField4
만 입력 한 데이터를 저장합니다. saveButton을 클릭하면
@IBAction func saveButton(_ sender: Any)
{
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
let phoneContactsObject = NSEntityDescription.insertNewObject(forEntityName: "Contacts", into: context)
phoneContactsObject.setValue(textField1.text, forKey: "name")
phoneContactsObject.setValue(textField2.text, forKey: "number")
phoneContactsObject.setValue(textField3.text, forKey: "name")
phoneContactsObject.setValue(textField4.text, forKey: "number")
do
{
try context.save()
print("saved")
}
catch
{
}
}
어떻게, 같은 시간에, 기업에 네 개의 텍스트 필드에 입력 된 데이터를 저장할 수 있습니다.
당신은 같은 개체에 두 번 이름과 번호의 값을 설정하는 엑스 코드 8.2, 스위프트 3.0
항상 고유 한 키 이름을 사용해야합니다. –