Performing attribute calculations between objects in master data

I am new to xcode / ios etc. and a little difficult to solve the problem. It seems I don’t know how to get and perform attribute calculations between objects in the main data. Here is an example of what I'm trying to do:

My project is a football application that has a relationship with each other. Season to the game (The season has a bunch of games. The season has attributes such as the team name and datestart. The game has attributes such as passing yards, yards, etc.

How could I figure out something like full passage of yards for the whole season (adding all the games passing yards together during this season)?

Thank you for your time.

+4
source share
2 answers

The managed object for the Season is related to the name hasGames (or as you called the link to the games). When you have an object managed by the Season, you can just access hasGames (it's NSSet ), play all the games there and do whatever you want with the data, for example, adding some values ​​up.

EDIT

Example:

If I take your Relationships β†’ Season β†’ Game career, and suppose the relationship is called Career.hasSeasons and Season.hasGames, it will look something like this.

int passingYards = 0; Career* myCareer = [... fetchedresult for the career you are looking for ...]; for(Season* season in myCareer.hasSeasons) { for(Game* game in season.hasGames) { careerPassingYards += [game.passingYards intValue]; } } 
+1
source

I ended up finding this code on the stack. After setting up my fetchedResultsController correctly, I can use this code with a button or where ever it was necessary to calculate the total spaces for all games in my season.

 int totalpassingyards = 0; for (NSManagedObject *object in [self.fetchedResultsController fetchedObjects]) { NSNumber *objectTotalPassingYardsNumber = [object valueForKey:@"passingyards"]; int objectTotalPassingYards = [objectTotalPassingYardsNumber intValue]; totalpassingyards = totalpassingyards + objectTotalPassingYards; } NSLog(@"Subtotal: %i", totalpassingyards); 

My next step is to figure out how to calculate the total gaps for all seasons on my career list. This is how my relationship with coredata is set up.

 Career > Season > Game (career stats) (season stats) (game stats) career stats = sum of season stats season stats = sum of game stats game stats = that individual games stats 

I guess this will consist of creating two fetchedResultsControllers that have games, and one for spending seasons and then doing a certain type of nested loop to go through all of them. I will keep everyone informed.

If someone has a better way to do this, let me know, as I said that I am very new to this, so it really comes down to traces and mistakes, and although this may work, I understand that this is not the best way to do it's lol.

0
source

Source: https://habr.com/ru/post/1383014/


All Articles