1

In Objective c there is this computer generated .h files from coredata. I do not want to change that file.

However, I want every object to have a reference to some other object outside coredata

For example, in business core data object, I want a reference to cell data that represent that business core data object.

How would I do so?

user4951
  • 32,206
  • 53
  • 172
  • 282
  • See chucks comment on one of the answers. Changing the generated code files is pretty much expected of you. Why don't you want to do that, or why can't you do that? Look at all the template projects - you wouldn't get very far if you didn't change any of that "generated" code. – jrturton Jan 31 '12 at 06:30

5 Answers5

2

Why not inheritance?

NSManagedObject ->
  MONObjectWithAnIvar { var } ->
    MONModeledObject
justin
  • 104,054
  • 14
  • 179
  • 226
1

Inside your .m file you can do the following:

@interface YourClass() {
    @private:

    NSString *someVariable;
}
@end

@implementation YourClass

-(void)viewDidLoad {
    someVariable = @"asdf";
}

@end
shawnwall
  • 4,549
  • 1
  • 27
  • 38
  • I don't want to change the .m file either because that's automatically generated – user4951 Jan 30 '12 at 14:06
  • You could create a category for your class and add the variable there then. See http://stackoverflow.com/questions/4146183/instance-variables-for-objective-c-categories – shawnwall Jan 30 '12 at 15:22
  • 2
    @JimThio: Why does the fact that the file was automatically generated make you averse to changing it? It's not meant to be somehow sacrosanct. Xcode helpfully generates a lot of boilerplate for you *with the expectation that you will change it*. – Chuck Jan 31 '12 at 06:06
  • Oh really? And if I change the coredata again I got to reimplment the same change? Maybe I should just put the pointer in the coredata but coredata do not accept pointer type. – user4951 Jan 31 '12 at 09:20
1

Even if the @private keyword isn't absolutely necessary to make someVariable private in the answer of shawnwall above (because the private character is already obtained by being inside the empty category interface declaration inside the implementation file), your question seem to reveal a possible break of the MVC pattern. What could really a data object be concerned by something in a cell?

onekiloparsec
  • 2,013
  • 21
  • 32
  • Well, I used customCell in database. The customcell contains an arrow that points to the business. When somebody moves the iphone I want the arrow to update and rotate the image. That means I need a reference to the UIImage. – user4951 Jan 30 '12 at 14:07
1

I think you should reconsider your design, it is not possible to add instance variables to an existing class without modifying it.

It is possible to add methods to an existing class without modifying the original source using categories but it doesn´t sound like that is what you want to do.

AndersK
  • 35,813
  • 6
  • 60
  • 86
1

In obj-c you can't modify members of existing class externally. One workaround solution is to keep external dictionary of customCell linked with arrow. Then you can get arrow as object for key customCell.

brigadir
  • 6,874
  • 6
  • 46
  • 81