将变量保存到代码中的CoreData实体 - Objective-C / XCode [英] Saving a variable to a CoreData entity in code - Objective-C / XCode

查看:321
本文介绍了将变量保存到代码中的CoreData实体 - Objective-C / XCode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我的工作
1.生成分数
2.移动到排行榜视图控制器并将分数传递给新的视图控制器
3.将分数添加到临时数组中。



现在,我在CoreData中创建了一个Scores实体,但我从来没有使用CoreData,所以我有点困惑如何使用它。





理想情况下,我想做的不是保存 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 文件。

  #importLeaderboardViewController.h 

@interface LeaderboardViewController()

@end

@implementation LeaderboardViewController

{
NSMutableArray * scores ;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil ];
if(self){
//自定义初始化
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];

//将scoreToBeAdded int转换为字符串

NSNumber * anumber = [NSNumber numberWithInteger:_scoreToBeAdded];

NSString * scoreToAddAsString = [anumber stringValue];

// [scores addObject:scoreToAddAsString]; //这是我在数组中添加分数的地方,但我想摆脱这个解决方案,所以我可以坚持数据。

// TODO:将scoreToAddAsString添加到CoreData中的Scores实体。

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


解决方案

添加



>

在您的 LeaderboardViewController.m 文件中添加 #importCoreDataAppDelegate.h $ b

So, I have a very simple app going in order to learn Obj-C, where a user generates a score and then I want to post that score to some sort of "user-leaderboard".

What I have working 1. Generate score 2. Move to a leaderboard view controller and pass score to the new view controller 3. Add the score to a temporary array.

Now, I've created a Scores entity in CoreData, but I've never used CoreData before so I'm a bit confused as to how to use it.

Ideally, what I'd like to do is instead of saving the scoreToBeAddedString to the array, add it to the Scores entity.

I tried to use this call, but I got a bunch of errors.

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

Errors: 1. Use of undeclared identifier: 'CoreDataAppDelegate 2. Use of undeclared identifier: 'appDelegate

Here is my LeaderboardViewController.m file.

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

解决方案

Add #import <CoreData/CoreData.h> in your LeaderboardViewController.h file.

Add #import "CoreDataAppDelegate.h" in your LeaderboardViewController.m file.

这篇关于将变量保存到代码中的CoreData实体 - Objective-C / XCode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆