gpt4 book ai didi

ios - DidSelect 后,如何返回整个核心数据记录? ( swift )

转载 作者:行者123 更新时间:2023-11-29 02:33:58 24 4
gpt4 key购买 nike

我可以在选择时访问核心数据记录的属性,但不能访问整个记录。我想要整个东西,所以我可以将它存储为一个变量,然后保存另一个与这个实体有关系的实体。我唯一能想到的就是用一个狭窄的谓词触发另一个提取 - 但那不可能是这样......可以吗?

func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
if (tableView == tableViewTeams) {
let currentTeam = teamResultController.objectAtIndexPath(indexPath) as Teams
forwardResultController = forwardResultControllerFunc(myQuery: currentTeam.teamName)
forwardResultController.delegate = self
forwardResultController.performFetch(nil)
defenderResultController = defenderResultControllerFunc(myTeam: currentTeam.teamName)
defenderResultController.delegate = self
defenderResultController.performFetch(nil)
tableView1.reloadData()
tableView2.reloadData()
}
//…other tableviews
}

func createPlayer() {
let playerEntityDescripition = NSEntityDescription.entityForName("Players", inManagedObjectContext: managedObjectContext!)
let player = Players(entity: playerEntityDescripition!, insertIntoManagedObjectContext: managedObjectContext)
let teamDict:NSDictionary = selectedTeam.lastObject as NSDictionary
var teamID = teamDict.objectForKey("teamName") as String
let predicate:NSPredicate = NSPredicate(format: "teamName == '\(teamID)'")!
let results:NSArray = SwiftCoreDataHelper.fetchEntities(NSStringFromClass(Teams), withPredicate: predicate, managedObjectContext: moc)
let currentTeam:Teams = results.lastObject as Teams
player.firstName = playerFirstName.text
player.lastName = playerLastName.text
player.type = playerPosition.text
player.number = playerNumber.text
player.team = currentTeam
managedObjectContext?.save(nil)
}

//one of the fetch requests
func fetchRequest(#myQuery: String, myQuery2: String, myQuery3: String) -> NSFetchRequest {
let fetchRequest = NSFetchRequest(entityName: "Players")
let sortDescriptor = NSSortDescriptor(key: "firstName", ascending: true)
let filter = NSPredicate(format: "team.teamName = %@ AND (type = %@ OR type = %@ )", "\(myQuery)", "\(myQuery2)", "\(myQuery3)")
fetchRequest.sortDescriptors = [sortDescriptor]
fetchRequest.predicate = filter
println(filter!)
return fetchRequest
}

最佳答案

您的团队实体以复数形式称为 Teams。为什么?令人困惑。
您的实体 Players 也是如此。

在您的代码中,您在函数内部编写函数。 (didSelectRow 的括号永远不会关闭。)为什么?令人困惑。

如果您想在从团队表中选择一个团队时创建一个玩家,只需确保您需要的变量在相同的范围中。如果您有一个定义 currentTeam 的方法,则在另一种方法中将不为人所知。

您可以简单地更改您的 createPlayer() 方法以返回播放器(添加 -> Player 到签名并在最后返回播放器 return player) 并从表选择回调中调用它。

var newPlayer = createPlayer()
newPlayer.team = currentTeam

关于ios - DidSelect 后,如何返回整个核心数据记录? ( swift ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26572312/

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