给另一个对象一个 NSManagedObject

发布于 2024-09-03 08:46:19 字数 1153 浏览 3 评论 0原文

好吧,我的代码遇到了问题。我所做的是 UIButton 的子类化,这样我就可以给它一些与我的代码相关的更多信息。我已经能够创建按钮并且它们工作得很好。卡皮切。

然而,我希望我的子类保留的内容之一是对 NSMangedObject 的引用。我的头文件中有这段代码:

@interface ButtonSubclass : UIButton {
    NSManagedObjectContext *context;
    NSManagedObject *player;
}

@property (nonatomic, retain) NSManagedObject *player;
@property (nonatomic, retain) NSManagedObjectContext *context;

- (id)initWithFrame:(CGRect)frame andTitle:(NSString*)title;
//- (void)setPlayer:(NSManagedObject *)aPlayer;

@end

如您所见,它有一个指向我希望它保存的 NSMangedobject 的实例变量(以及上下文)。但对于我的一生,我无法让它保存 NSManagedObject。我在实现文件中运行两个 @synthesize 方法。

@synthesize context;
@synthesize player;

所以我不确定我做错了什么。这就是我创建按钮的方式:

ButtonSubclass *playerButton = [[ButtonSubclass alloc] initWithFrame:frame andTitle:@"20"]; //works

        playerButton.context = self.context; //works
        playerButton.player = [players objectAtIndex:i]; //FAILS

我之前已经初始化了玩家数组,我在其中获取对象。另一个奇怪的事情是,当它到达代码中的这个位置时,应用程序崩溃(woot)并且控制台输出停止。它不会给我任何错误,也不会通知我应用程序已崩溃。它只是……停止了。所以我什至不知道导致代码崩溃的错误是什么,除了它与设置“player”变量的那一行有关。想法和想法?我喜欢你的智慧!

Alright, so I'm running into an issue with my code. What I have done is subclassed UIButton so I can give it some more infomormation that pertain to my code. I have been able to create the buttons and they work great. Capiche.

However, one of the things I want my subclass to hold is a reference to a NSMangedObject. I have this code in my header file:

@interface ButtonSubclass : UIButton {
    NSManagedObjectContext *context;
    NSManagedObject *player;
}

@property (nonatomic, retain) NSManagedObject *player;
@property (nonatomic, retain) NSManagedObjectContext *context;

- (id)initWithFrame:(CGRect)frame andTitle:(NSString*)title;
//- (void)setPlayer:(NSManagedObject *)aPlayer;

@end

As you can see, it has a instance variable to the NSMangedobject I want it to hold (as well as the Context). But for the life of me, I can't get it to hold that NSManagedObject. I run both the @synthesize methods in the Implementation file.

@synthesize context;
@synthesize player;

So I'm not sure what I'm doing wrong. This is how I create my button:

ButtonSubclass *playerButton = [[ButtonSubclass alloc] initWithFrame:frame andTitle:@"20"]; //works

        playerButton.context = self.context; //works
        playerButton.player = [players objectAtIndex:i]; //FAILS

And I have initilaized the players array earlier, where I get the objects. Another weird thing is that when it gets to this spot in the code, the app crashes (woot) and the the console output stops. It doesn't give me any error, and notification at all that the app has crashed. It just... stops. So I don't even know what the error is that is crashing the code, besides it has to do with that line up there setting the "player" variable. Thoughts and ideas? I'd love your wisdom!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦冥 2024-09-10 08:46:19

您所拥有的代码没有任何特殊原因不能工作。您可以将 NSManageObject 实例分配为属性。崩溃的最简单解释是数组越界错误。当您没有从控制台收到错误时,通常是因为调试器崩溃了。这通常是由于某种递归导致调试器堆栈溢出而引起的。

然而,将数据逻辑放在像按钮这样的视图元素中是非常非常糟糕的做法。这完全打破了 MVC 设计模式,并使您的代码脆弱且难以维护。 UI 元素应该是“哑的”,并且只了解如何显示传递给它们的任何数据,它们不应该参与实际跟踪数据本身。这分别是控制器和数据模型的功能。

理想情况下,“玩家”对象应通过数据模型进行跟踪,然后控制器将其链接到相应的 UI 按钮或其他 UI 元素。将玩家的跟踪放入数据模型使得设计灵活,易于扩展、重用和维护。

There is no particular reason why the code you have shouldn't work. You can assign a NSManageObject instance as an attribute. The simplest explanation for the crash is an out of bounds error for the array. When you don't get an error from the consoles its usually because the debugger has crashed. That is usually caused by having some kind of recursion that causes the debugger stack to overflow.

However, putting data logic in a view element like a button is very, very poor practice. This completely breaks the MVC design pattern and will make your code fragile and hard to maintain. UI elements should be "dumb" and only understand how to display any data handed to them, they shouldn't be involved in actually tracking the data itself. That is the function of the controller and the data model respectively.

Ideally, the "player" object should be tracked by the data model which the controller then links to the appropriate UI button or other UI element. Putting the tracking of the player in the data model makes the design flexible which makes it easy to expand, reuse and maintain.

漫漫岁月 2024-09-10 08:46:19

首先尝试添加一些调试代码,

ButtonSubclass *playerButton = [[ButtonSubclass alloc] initWithFrame:frame andTitle:@"20"]; //works
playerButton.context = self.context; //works
NSLog(@"total players: %d", players.count);
NSLog(@"players: %@", [players objectAtIndex:i]);
playerButton.player = [players objectAtIndex:i]; //FAILS

这可能会揭示错误

Try adding some debug code first

ButtonSubclass *playerButton = [[ButtonSubclass alloc] initWithFrame:frame andTitle:@"20"]; //works
playerButton.context = self.context; //works
NSLog(@"total players: %d", players.count);
NSLog(@"players: %@", [players objectAtIndex:i]);
playerButton.player = [players objectAtIndex:i]; //FAILS

this probably reveals the error

风柔一江水 2024-09-10 08:46:19

您不能仅通过将其声明为属性并添加 @synthesize 来创建托管对象上下文。托管对象上下文是由应用程序委托创建的,您可以这样调用它:

[[NSApp delegate] managedObjectContext];

我不知道如何初始化玩家数组,但我怀疑它不包含您认为包含的对象: NSManagedObjectContext 仅具有以下方法返回 NSSet,虽然

[[[NSApp delegate] managedObjectModel] entities];

返回一个数组,但它包含 NSEntityDescription 的实例。我建议您首先查看应用程序的应用程序委托 (AppDelegate.m) 的代码,以了解有关核心数据存储入口点的更多信息。

You can't create the managed object context simply by declaring it a property and adding @synthesize. The managed object context is created by the application delegate and you call it like this:

[[NSApp delegate] managedObjectContext];

I have no idea how you initialize the players array, but I have a suspicion that it does not contain the objects you think it does: NSManagedObjectContext only has methods that return NSSet's and while

[[[NSApp delegate] managedObjectModel] entities];

does return an array, it contains instances of NSEntityDescription. I suggest you start by looking at the code of your application's application delegate (AppDelegate.m) to learn more about the entry points to the Core Data store.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文