1

I would like to create an NSObject class that I can use the instance of and save to its variables and later pass its data elsewhere (NSManagedObject). Do I need to do anything else besides creating a new Object-C Class that inherits from NSObject. Create Variables in .h and synthesize in .m.

i.e.: my .h file:

#import <Foundation/Foundation.h>
@interface MyDataClass : NSObject

@property (nonatomic, retain) NSNumber *variable1
@property (nonatomic, retain) NSString *variable2
@property (nonatomic, retain) NSDate *variable3

@end

.m file:

#import "MyDataClass.h"

@implementation MyDataClass

@synthesize variable1, variable2, variable3

@end

I would like to be able do the following in some SomeViewController:

@property (nonatomic, strong) MyDataClass *newDataClass

@systhesize newDataClass;


newDataClass.variable1 = @"123457890";
newDataClass.variable2 = @"This is the new Variable";
newDataClass.variable3 = [NSDate date];

Is there anything else I need to do to initialize each variable when an instance of this class is created? Am I missing anything?

user1107173
  • 10,334
  • 16
  • 72
  • 117
  • I am not sure what exactly you want to do. Do you want to create a custom NSManagedObjectClass which you would use to store your data? – robbartoszewski May 10 '13 at 00:40

1 Answers1

2

That's all that you need for a custom object which you want to use to store data.

Of course when you go to use it (in SomeViewController), you need to actually create an instance of your class before you start setting the variables:

self.newDataClass = [[MyDataClass alloc] init];

I would also use self.newDataClass instead of just newDataClass unless you have a reason not to.

lnafziger
  • 25,760
  • 8
  • 60
  • 101
  • Thank you! Last question, once I'm done with this instance and want to delete/clear it, should I just do newDataclass = NULL; ? – user1107173 May 10 '13 at 01:02
  • 1
    What's the advantage over self.newDataClass vs using the synthesized property newDataClass directly? – powerj1984 May 10 '13 at 01:10
  • 1
    Actually I would go a step further and get in the habit of using auto-sythesized underscore variables along with the "new" helper which does the same thing as alloc ]init] as well. _newDataClass = [MyDataClass new]; – Mark McCorkle May 10 '13 at 01:14
  • Yes, since this is a `strong` declared property, you should just set it to `nil` when you are done using it and it will release the object. Using `self.` invokes the setter/getter (as appropriate) and makes it easy in the future if you need to change the default behavior. For instance, if you need to update another variable whenever this one is changed, you can add it one place (in a custom setter) and this will happen automatically instead of having to find every place that you updated it and adding it there. – lnafziger May 10 '13 at 01:38
  • For some more specific details about using `self.`, see this question: http://stackoverflow.com/q/4142177/937822 (especially the most upvoted answer, rather than the accepted answer). – lnafziger May 10 '13 at 01:39