ios - 在代码中将变量保存到 CoreData 实体 - Objective-C/XCode

标签 ios objective-c xcode core-data

所以,我有一个非常简单的应用程序来学习 Obj-C,其中用户生成一个分数,然后我想将该分数发布到某种“用户排行榜”。

我正在做的事情 1. 生成分数 2.移动到排行榜 View Controller 并将分数传递给新的 View Controller 3. 将分数添加到临时数组中。

现在,我已经在 CoreData 中创建了一个 Scores 实体,但我以前从未使用过 CoreData,所以我对如何使用它有点困惑。

I've added the "Scores" entity to my CoreData file here

理想情况下,我想要做的不是将 scoreToBeAddedString 保存到数组中,而是将其添加到 Scores 实体中。

我尝试使用此调用,但出现了一堆错误。

CoreDataAppDelegate *appDelegate =
    [[UIApplication sharedApplication] delegate];

   NSManagedObjectContext *context =
    [appDelegate managedObjectContext];
   NSManagedObject *newScore;
   newScore = [NSEntityDescription
       insertNewObjectForEntityForName:@"Scores"
       inManagedObjectContext:context];
   [newScore setValue: _scoreToBeAddedAsString forKey:@"score"];
   NSError *error;
   [context save:&error];

错误: 1. 使用未声明的标识符:'CoreDataAppDelegate 2. 使用未声明的标识符:'appDelegate

这是我的 LeaderboardViewController.m 文件。

#import "LeaderboardViewController.h"

@interface LeaderboardViewController ()

@end

@implementation LeaderboardViewController

{
    NSMutableArray *scores;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    //Convert the scoreToBeAdded int to a string 

    NSNumber *anumber = [NSNumber numberWithInteger:_scoreToBeAdded];

    NSString *scoreToAddAsString = [anumber stringValue];

    //[scores addObject:scoreToAddAsString]; //This is where I was adding the score to the array, but I want to get rid of this solution, so I can persist the data. 

    //TODO: Add the scoreToAddAsString to the "Scores" entity in CoreData. 

    NSLog(@"Leaderboard loaded w new score to add %i", _scoreToBeAdded);
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [scores count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"SimpleTableCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }

    cell.textLabel.text = [scores objectAtIndex:indexPath.row];
    return cell;
}

最佳答案

添加#import <CoreData/CoreData.h>在你的LeaderboardViewController.h文件。

添加#import "CoreDataAppDelegate.h"在你的LeaderboardViewController.m文件。

关于ios - 在代码中将变量保存到 CoreData 实体 - Objective-C/XCode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29470631/

相关文章:

ios - 这种设计模式称为什么,其中方法是实例方法而不是类方法?

ios - 通过 AVMetadataMachineReadableCodeObject 从 Aztec 条码读取原始字节会产生意外结果

ios - 如何使 iOS 7 中 UISearchBar 顶部的区域(在 UITableView 上时)透明?

ios - Core Plot 1.0 我的触摸注释不起作用

ios - 使用 Xcode 中的 UI 测试在 UIWebView 中加载的网页中键入文本时出现问题

ios - 关闭 SFSafariViewController 上的半透明条

swift - NSFetchedResultsController 谓词消除多个属性的重复项

ios - 具有常量值的映射在简单闭包中失败

iphone - CGAffineTransformMakeRotation错误

iOS 旧应用程序屏幕尺寸