2

So what i want to ask is below

Here is my header file

NSString *myString;

In the m. file

-(void)someMethod{
    myString = [NSString stringWithString = @"Hello"];
    NSLog(@"%@",myString);
}

-(void)dealloc{
    [myString release];
}
-(void)viewDidUnload{
    [myString release];
    myString=nil;
}

Ok now the other situation

In my header file

NSString *myString;
@property (nonatomic,retain) NSString *myString;

In the m. file

@synthesize myString;

-(void)someMethod{
    NSString *tempString = [[NSString alloc] initWithString:@"Hello"];
    self.myString = tempString;
    [tempString release];
    NSLog(@"%@",myString);
}

-(void)dealloc{
    [myString release];
}
-(void)viewDidUnload{
    [myString release];
    self.myString=nil;
}

I really need an idiot guide for this cause I do not understant it yet. Both works. Also am i using the release in dealloc and viewDidUnload correct?? Thank you in advance

0

2 Answers 2

2

Info about properties, not a definitive guide:

One advantage of @property is that with @synthesize they create setters and getters that handle the retain and releases (as applicable) and work the with or without ARC (with minor modifications).

properties no longer need their associated ivars to be declared, they will be automatically generated.

properties can be placed in the header file (.h) for public use or in the implementation file (.m) in a class extension for private use in the class.

@property and @synthesize statements have nothing to do with allowing "dot notation". "dot notation" can be viewed as a substitution for the bracket form of accessing a setters and getters (actually use is more general but use is best restricted to getter/setters).

[self myIvar] is equivalent to self.myIvar and [self setMyIvar:myValue] is equivalent to self.myIvar = myValue.

dot notation can be used on non-properties such as NSStrings: myString.length works fine and is reasonably acceptable usage.

dot notation has nothing to do with properties however Xcode will only offer auto completions for properties.

-2

@property and @synthesize provides the getter and setter (accessors) methods rather than you having to write it out yourself. The declaration is made with @property and it is implemented with @synthesize.

So in the main program when you create a new class object (Assuming your class is called MyClass with MyClass.m, MyClass.h), you are able to access your string variable myString using the dot operator. If your object is called NewObject, then you can access the string inside the main program with NewObject.MyString.

You can also use this to set a value for string (i.e. NewObject.MyString = OtherString). Very handy and time-saving. They both work because you are accessing the variables from within the class and so you wouldn't need to set the accessors.

For the -(void)dealloc you also need [super dealloc] inside there to release the variables of the superclass. You don't need to release MyString in viewDidUnload as you have done it in the -(void)dealloc method.

When you allocate memory in -(void)viewDidLoad, then you would need to release it in -(void)viewDidUnload, but you haven't here so it isn't needed.

2
  • @property and @synthesize statements have nothing to do with allowing "dot notation". "dot notation" can be viewed as a substitution for the bracket form of accessing a setters and getters (actually use is more general but use is best restricted to getter/setters). [self myIvar] is equivalent to self.myIvar and [self setMyIvar:myValue]` is equivalent to self.myIvar = myValue. dot notation can be used on non-properties such as NSStrings: myString.length works fine and is reasonably acceptable usage.
    – zaph
    Nov 26, 2011 at 14:12
  • @CocoaFu I guess I never realized that NSString didn't have a property called length, if so, then how does the dot-notation work? That could be very interesting if one could implement it for their own classes... Nov 26, 2011 at 14:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.