简单的问题,我认为......
如何在以下字符串中添加值? scoreValue = "8.68,2.815,5,7.975,2.695,7.285,3.625,7.36,8.335,2.485";
背景信息。该字符串从CoreData实体返回(称为" ScoreData"),并放入NSDictionary的实例中,如下所示:
NSDictionary *getScoreValues = [self.scoreEntityResultsForValues objectAtIndex:[indexPath row]];
NSLog(@"getScoreValues returns: %@", getScoreValues); // This is what getScoreValues returns for each of the scoreValue's in the CoreData entity.
这是价值最初发布到实体的方式
//Call the ScoreData entity
ScoreData *newEntry = [NSEntityDescription insertNewObjectForEntityForName:@"ScoreData" inManagedObjectContext:self.managedObjectContext];
//Join the value returned from the NSArray by a "," and post them as a string to the ScoreData entity
NSString *stringForScoreEntity = [[CEWStoreVars sharedScoreArray].sharedScoreArrayProperty componentsJoinedByString:@","];
newEntry.scoreValue = stringForScoreEntity;
我将值作为字符串发布到实体的原因是因为我无法让实体接受浮点值,我希望以后能够提取每个单独的分数。但是,如果有更好的方法,我可以接受建议。另外,如果您需要更多代码来破译正在发生的事情,请与我们联系!
提前致谢!
修改
对于任何感兴趣的人,这就是代码的外观:
//Load the Key and KeyValue into Dictionary
NSDictionary *getScoreValues = [self.scoreEntityResultsForValues objectAtIndex:[indexPath row]];
//Convert the dictionary keyValue to a string
NSString *convertDictKeyToString = [getScoreValues objectForKey:@"scoreValue"];
//Remove the "," in the string.
NSArray *items = [convertDictKeyToString componentsSeparatedByString:@","];
//Add them up to get the sum
double total = 0.0;
for (NSString *string in items)
{
total += [string floatValue];
}
答案 0 :(得分:2)
NSArray *items = [scoreValue componentsSeparatedByString:@","]; //scoreValue is a string
double total = 0.0;
for (NSString *string in items)
{
total += [string floatValue];
}