简体   繁体   English

对核心数据中的实体之间的属性执行计算

[英]Performing calculations on attributes between entities in core data

I am new to xcode/ios etc... and having a little trouble figuring out a problem. 我是xcode / ios等的新手......并且在解决问题时遇到了一些麻烦。 I cant seem to figure out how I would retrieve and perform calculations on attributes between entities in core data. 我似乎无法弄清楚如何检索和执行核心数据中实体之间的属性计算。 Here is an example of what I am trying to do: 这是我想要做的一个例子:

My project is a football app that has a one to many relationship. 我的项目是一个拥有一对多关系的足球应用程序。 Season to Game (Season has a bunch of games. Season has attributes like teamname and datestart. Game has attributes like passing yards, running yards etc.. 赛季到赛季(赛季有很多比赛。赛季有像teamname和datestart这样的属性。比赛有像传球码,跑码等属性。

How would I be able to calculate something like total passing yards for the whole season (adding all the games passing yards together for that season)? 我如何计算整个赛季的总传球码数(将所有比赛的传球码加在一起)?

Thanks for your time. 谢谢你的时间。

You managed object for Season has a relationship named hasGames (or whatever you named the relationship to the games). Season的管理对象有一个名为hasGames的关系(或任何你命名为游戏的关系)。 When you have a Season managed object, you can just access the hasGames relationship (it's an NSSet ), loop through all the games in there and do whatever you want with the data, eg adding some values up. 当你有一个季节管理对象时,你可以只访问hasGames关系(它是一个NSSet ),遍历那里的所有游戏,并用数据做任何你想做的事情,例如添加一些值。

EDIT : 编辑

Example: 例:

If I take your Relationships Career -> Season -> Game, and suppose the relationships are called Career.hasSeasons and Season.hasGames, it would look something like this 如果我接受你的关系事业 - >季节 - >游戏,并假设这些关系被称为Career.hasSeasons和Season.hasGames,它看起来像这样

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];
  }
}

I ended up finding this code here on stack. 我最终在堆栈上找到了这段代码。 After properly setting up my fetchedResultsController I am able to use this code in a button or where ever needed to calculate the total passing yards for all of the games in my season. 在正确设置了fetchedResultsController之后,我可以在一个按钮中使用此代码,或者在需要的地方使用这个代码来计算我赛季中所有游戏的总传球码数。

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 passing yards for all of the seasons in my career list. 我的下一步是弄清楚如何计算我职业生涯列表中所有赛季的总传球码数。 Here is how my coredata relationships are set up. 以下是我的coredata关系的设置方式。

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 am guessing this will consist of creating two fetchedResultsControllers one holding the games and one for holding the seasons and then doing some type of nested loop to get through all them. 我猜这将包括创建两个fetchedResultsControllers,一个持有游戏,一个用于持有季节,然后做一些类型的嵌套循环来完成它们。 I will keep everyone updated. 我会让每个人都知道。

If anyone has a better way to do this let me know like I said I am very new to this so everything really comes down to trail and error and although it might work I understand it might not be the best way to do it lol. 如果有人有更好的方法这样做让我知道,就像我说我对这个很新,所以一切都归结为追踪和错误,虽然它可能有效但我知道它可能不是最好的方法来做它大声笑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM