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
@propertyand@synthesizeprovides the getter and setter (accessors) methods rather than you having to write it out yourself. The declaration is made with@propertyand it is implemented with@synthesize.So in the main program when you create a new class object (Assuming your class is called
MyClasswithMyClass.m,MyClass.h), you are able to access your string variablemyStringusing the dot operator. If your object is calledNewObject, then you can access the string inside the main program withNewObject.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)deallocyou also need[super dealloc]inside there to release the variables of the superclass. You don’t need to releaseMyStringinviewDidUnloadas you have done it in the-(void)deallocmethod.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.