Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the automatic property synthesize in xcode 4.4 work?

I'm new to Objective-C and XCode, but I was happy to see that XCode 4.4 automatically synthesizes my properties for me, now. I figure this means that I no longer have to type out @synthesize for my properties, and that I get to access them using self.propertyName = @"hi";, for example.

I'm trying to re-write some example code so that I can understand it better, but this code implements a custom getter method. In the example code, the property is manually synthesized, as @synthesize managedObjectContext = __managedObjectContext;. The custom getter looks like this:

- (NSManagedObjectContext *)managedObjectContext {
    if (__managedObjectContext != nil) {
        return __managedObjectContext;
    }
    
    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (coordinator != nil) {
        __managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
        [__managedObjectContext setPersistentStoreCoordinator:coordinator];
    }
    
    return __managedObjectContext;
}

In this person's code, I see he's just using his manually synthesized accessor to both get and set. I figured in my code, I could just replace the __managedObjectContext with self.managedObjectContext, but nope. If I do this, I get an error telling me that I am trying to assign to a readonly property. This makes sense, because that property is defined as readonly, by this other coder.

@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;

So, I figure something about how he's manually synthesizing his property means that if he uses that specified setter, it allows him to set a readonly property somehow.

If I manually synthesize the property, like in the code I am referencing, everything goes back to working, but that's not making use of the new automatic synthesize. If I remove the readonly, I can set this property, as expected, but I feel like I'm not understanding why he has it as readonly, so I bet I'm breaking something there.

So, am I misusing the new automatic synthesize? How do I set this using the setter, if the automatic synthesize is not creating it for me, because of readonly?

like image 510
Ryan Avatar asked Aug 01 '12 04:08

Ryan


People also ask

Which is the default for synthesized properties?

Default Synthesis Of Properties Clang provides support for autosynthesis of declared properties. Using this feature, clang provides default synthesis of those properties not declared @dynamic and not having user provided backing getter and setter methods.

What is synthesize in OBJC?

@synthesize tells the compiler to take care of the accessor methods creation i.e it will generate the methods based on property description. It will also generate an instance variable to be used which you can specify as above, as a convention it starts with _(underscore)+propertyName.

Why @property and @synthesize do not exist for a property in Swift?

Swift provides no differentiation between properties and instance variables (i.e, the underlying store for a property). To define a property, you simply declare a variable in the context of a class. A swift class is simply a ClassName.

What is synthesized in Swift?

Swift 4.1 also introduced synthesized support for the Hashable protocol, which means it will generate a hashValue property for conforming types automatically. Hashable was always annoying to implement because you need to return a unique (or at least mostly unique) hash for every object.


1 Answers

When XCode auto-synthesizes, it simulates the following...

@synthesize foo = _foo;

So, you can access the data appropriately with self.foo or object.foo.

However, in the implementation of accessor methods (and initializers and dealloc), you should use the iVar directly.

Note, those iVars have two underscores. And, they are being manipulated in the accessor method. Use _managedObjectContext, and you should be good to go.

like image 95
Jody Hagins Avatar answered Oct 21 '22 12:10

Jody Hagins