When you do:
@property (nonatomic, retain) NSString *aString;
...followed by:
@synthesize aString;
...your class gets three basic things:
- An instance-variable called
aString
that you can reference directly within your class.
- A getter function with a signature
- (NSString*) aString;
that returns the instance variable.
- A setter function with a signature
- (void) setAString: (NSString*) value;
that retains the parameter that is passed in and assigns it to the instance variable (and releases any previously retained value, if one exists);
Implementation-wise, these generated methods and fields might look like:
//in the .h file
@interface MyClass {
NSString* aString;
}
- (NSString*) aString;
- (void) setAString: (NSString*) value;
@end
//in the .m file
@implementation MyClass
- (NSString*) aString {
return aString;
}
- (void) setAString: (NSString*) value {
if (aString != value) {
[aString release];
aString = [value retain];
}
}
@end
So to answer the rest of your questions:
self.aString = @"My New String"; // This will use the setter? Yes, and it will retain the string.
aString = @"My new String"; //This will not use the setter and retain the string, right? Right.
aString = [@"My new string" retain]; //This will not use the setter, but it will retain the string? Right again.
[self setAString:@"My new string"]; //This will also use the setter? Yep.
As for which are correct, they all are, depending upon what you want and assuming that you understand how the behavior differs between using each one, particularly with respect to releasing/retaining property values. For instance, this code will leak memory:
self.aString = @"Value 1"; //go through the setter, which retains the string
aString = @"Value 2"; //bypass the setter, the 'Value 1' string will not be released
...and this equivalent code will not:
self.aString = @"Value 1"; //go through the setter, which retains the string
self.aString = @"Value 2"; //go through the setter again to release the first string