Predicates for relationship

How do I pull an objects relationships? I was using SQL style and doing it by string comparison, but I think this is wrong.

I’ve got a Team Object:

extension Team {

@NSManaged var logo: String?
@NSManaged var name: String
@NSManaged var no_of_periods: NSNumber
@NSManaged var no_of_players: NSNumber
@NSManaged var period_length: NSNumber
@NSManaged var sport: String
@NSManaged var players: NSSet?


A player Object:

extension Player {

@NSManaged var name: String
@NSManaged var team: String
@NSManaged var jersey: NSNumber
@NSManaged var playerTeam: Team?


So when someone clicks on a particular team in my tableView it takes them to that team and a list of players associated with that team:

class PlayerViewController: UITableViewController, PlayerDetailViewControllerDelegate {

var managedObjectContext: NSManagedObjectContext!

var currentTeam: Team!

lazy var fetchedResultsController: NSFetchedResultsController = {
    let fetchRequest = NSFetchRequest()
    let predicate = NSPredicate(format: self.currentTeam.players = // What should go here? )         
    fetchRequest.predicate = predicate
    let entity = NSEntityDescription.entityForName("Player", inManagedObjectContext: self.managedObjectContext)
    fetchRequest.entity = entity
    let sortDescriptor = NSSortDescriptor(key: "name", ascending: true)
    fetchRequest.sortDescriptors = [sortDescriptor]
    fetchRequest.fetchBatchSize = 20

How do I do this, keep getting confused.

I also had this question, it can be deleted, but I don’t have the right permissions to do so :frowning:

What do you mean by “pull” an object’s relationships?

I think you are trying to list the Player objects where Team = whatever was selected. If that is the case:

let predicate = NSPredicate(format: “ == %@”, selectedTeamVariable)

I think youre confused because you seem to already have a current or selected team variable: self.currentTeam. If that is the case, you want to fetch all Player objects whose = userSelectedTeam.

Thanks @marciokoko

I needed NSPredicate(format: "playerTeam = %@", self.currentTeam) to get it to work