Reputation: 968
I'm been trying to understand some of the nuances of the iOS system and one item that isn't clear to me. I was building a class to encapsulate methods to access the calendar, which included creating the events, event store singleton, and an async fetch operation.
There was a mixture of class variables and instance variables but I had a hard time getting that to work.
My question is this.
Lets look at this project.
http://developer.apple.com/library/ios/#samplecode/SimpleDrillDown
The Play class encapsulates info about a play.
@interface Play : NSObject
@property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) NSArray *characters;
@property (nonatomic, strong) NSString *genre;
@property (nonatomic, strong) NSDate *date;
@end
#import "Play.h"
@implementation Play
@synthesize title, characters, genre, date;
@end
Because of the synthesize, there is no dealloc method, if I understand that correctly.
Now, there are no other methods defined.
In the sample project's Details Controller, the object isn't initialized at all, but the properties are accessible by reference, which confused me. If I try to write methods in the Play, the methods can't change the values in the properties. For example, if you tried to merge the Data Controller class with the Play class, I noticed that methods can't set and get values from the properties.
For example, if I add into the Play class:
(void)createSamplePlay
{
self.title = @"Play 1";
self.characters = [NSArray arrayWithObject:[NSString stringWithFormat:@"Character 1"]];
self.genre = @"Genre 1";
self.date = [NSDate date];
}
Then I call the method inside Details Controller, nothing actually gets changed. If I move the method to inside the Details Controller, declare a static typed property Play *play
, then calling the method [self createSamplePlay];
and changing to self
to play
, then of course, the methods works.
I didn't know if there was a fundamental misunderstanding in my understanding or it is just the code above is write and I got error in my actual and much longer project.
In the meantime, I'm going to keep reading the Obj-C PDFs from developer.apple.com.
Sorry that was a bad example of my final intent.
Let say I wanted to write a method that does this:
-(void)fillInMissingGenre
{
if([self.title caseInsensitiveCompare:@"Romeo and Juliet"] == 0){
self.genre = @"Tragedy";
}
}
In the sample code, the Play
is instanced by a Data Controller, which is returned to the Root Controller, then passed to a Detail Controller, through the Detail Controller's Play property. Both the Root and the Detail should be able to call Play's methods, unless one of the previous entities have decided to dereference the object.
I'm starting to suspect this is the cause of my problems, since sometimes the Play
object returns nil for everything and then sometimes I get a Exec_Bad_Access. If I only had learned to use that debugger. . .
Upvotes: 0
Views: 294
Reputation: 135548
Because of the synthesize, there is no dealloc method, if I understand that correctly.
That's wrong. There is no dealloc
because this code is written for ARC (Automatic Reference Counting). It has nothing to do with @synthesize
.
Other than that, I think you are missing the difference between a class and an object. The properties are defined on the Play
class, but to actually use them (to store data), you need to create an object or instance of the class. You do that with:
Play *myPlay = [[Play alloc] init];
Now you can use the object myPlay
(or more exact, myPlay
is a pointer to an object) to do stuff like `myPlay.title = @"Play 1";
If you don't create the instance, it won't work:
Play *myPlayNoInstance;
myPlayNoInstance.title = @"Play 2"; // doesn't do anything
Because here, myPlayNoInstance
is a pointer that does not point to an allocated object.
Upvotes: 3
Reputation: 726639
If I understand your intent correctly, the createSamplePlay
is a factory method. If this assumption is correct, you should declare it in the Play
class (notice the +
- it's an indicator of a class method), and rewrite the implementation like this:
+ (id) createSamplePlay
{
self = [[Play alloc] init];
self.title = @"Play 1";
self.characters = [NSArray arrayWithObject:[NSString stringWithFormat:@"Character 1"]];
self.genre = @"Genre 1";
self.date = [NSDate date];
return self;
}
Later you can use it in your code:
Play *sample = [Play createSamplePlay];
Because of the synthesize, there is no dealloc method, if I understand that correctly.
No, dealloc is not there because of Automatic Reference Counting (the presence of strong
modifier tells you that you're in the ARC land).
Upvotes: 1