저는 스위프트의 초보자이며 xCode는 제 용어가 정확하지 않을 경우 사과드립니다.'String?'유형의 값을 할당 할 수 없습니다. 'Double'error를 입력하십시오.
저는 CoreData를 배우기 시작했으며 사용자가 '위치'를 만들 수있는 기본 기능을 생성하려고합니다.
name (type = string), latitude (type = double) 및 longitude (type = double) 속성을 사용하여 데이터 모델을 설정했습니다.
사람들이 이름, 위도 및 경도를 입력 할 수 있도록 설정되어있는 다른 컨트롤러와 연결된 Segment와 함께 TableViewController를 설정했습니다.
내가 알 수있는 한, 위도 및 경도 콘센트에 연결된 텍스트 필드를 읽는 두 줄의 코드를 제외한 모든 설정이 올바르게 설정되어 있습니다. 이 코드는 AddLocationController에 포함되어 있습니다.
도움이 될 것입니다.
@IBOutlet var nameTextField:UITextField!
@IBOutlet var latitudeTextField:UITextField!
@IBOutlet var longitudeTextField:UITextField!
@IBAction func save(sender: AnyObject) {
if let appDelegate = (UIApplication.shared.delegate as? AppDelegate) {
location = LocationMO(context: appDelegate.persistentContainer.viewContext)
location.name = nameTextField.text
// This is where the error occurs
location.latitude = latitudeTextField.text
location.longitude = longitudeTextField.text
print("saving data to context ...")
appDelegate.saveContext()
}
dismiss(animated: true, completion: nil)
}
}
이 경우 텍스트를 double로 변환 할 수 없으면 충돌이 발생합니다. 이 경우 let 문을 사용하는 것이 좋습니다. –
감사합니다. 이것은 트릭을했다. – joshlorschy
언제나 환영합니다 @ joshlorschy –