이 코드는 인쇄 명령을 사용할 때 모든 가져 오기 결과를 얻을 수 있지만 결과를 UILabel 안에 넣고 표시하려고하면 마지막으로 삽입 된 값만 표시됩니다.
do {
let result = try managedContext.fetch(fetchRequest)
for data in result as! [NSManagedObject] {
print(data.value(forKey: "date") as! String)
print(data.value(forKey: "score") as! String)
var x = data.value(forKey: "date") as! String
var y = data.value(forKey: "score") as! String
//allScores.text = "Session started on \(x), points earned: \(y)"
}
} catch {
print("Failed")
}
이 작업을 수행하면
print(data.value(forKey: "date") as! String)
print(data.value(forKey: "score") as! String)
하지만 이것은 내 UILabel에 마지막으로 삽입 된 값만 표시하지 않습니다.
var x = data.value(forKey: "date") as! String
var y = data.value(forKey: "score") as! String
allScores.text = "Session started on \(x), points earned: \(y)"
이렇게 해봐
do {
let result = try managedContext.fetch(fetchRequest)
allScores.text = ""
for data in result as! [NSManagedObject] {
print(data.value(forKey: "date") as! String)
print(data.value(forKey: "score") as! String)
var x = data.value(forKey: "date") as! String
var y = data.value(forKey: "score") as! String
allScores.text = (allScores.text ?? "") + "Session started on \(x), points earned: \(y)"
}
} catch {
print("Failed")
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다