开发者

Use of [self.labelIBOutlet release] vs [labelIBOutlet release]

I have been playing a bit with the memory in order to be a good memory citizen on the iPhone SDK.

However I still struggle to understand the di开发者_StackOverflowfference between "self.something" and just "something".

As far as I understood, "self.something" means ask to the class for "something", but there is something wrong on my thought. Let's see the example:

I have worked with the memory releasing:

  1. [self.labelIBOUtlet release] -> It crash
  2. [labelIBOUtlet release] -> It doesn't.

Can anyone please explain me what is the reason?

Thank you!

EDIT:

This is the information I have set on the header file:

@interface viewController : UIViewController {
    UILabel * labelIBOutlet ;
}

@property (nonatomic,retain) IBOutlet UILabel * labelIBOutlet ;


You have to understand the meaning of "property" the use of "dot" is just a faster way to call "special methods" created just to "set" and "get" variable-property.

as example, you could have your own class/UIView which uses a subView:

in myView.h

@interface myView : UIView  {
    UIWebView *webView;
}

if you do just this you have not a "property", but just an ojbect... so in your myView.m you try to use the "dot" like this:

NSLog(@"%i", self.webView.frame.size.width);

then you get an error, you cannot do that, xCode says: error: accessing unknown 'webView' getter method

that just means that a the method "webView" doesn't exist... 'couse when you call "self.webView" you just call a method called "webView"... this method just return the pointer to your value. and when you call:

self.webView=someValue;

you are just calling the method "setWebView", a method that just set your object with someValue...

but so... where do those 2 invisible methods come from?

they are created by xCode itself if you tell it to use webView as a property... in our example, add some lines:

in myView.h

    @interface myView : UIView  {
        UIWebView *webView;
    }

@property (nonatomic, retain)  UIWebView *webView;

in myView.m

@implementation myView

@synthesize webView;

// ...

doing this xCode will add the 2 methods "webView" and "setWebView" for you, and now you can call:

NSLog(@"%i", self.webView.frame.size.width);

with no error...

and you can put value (of the right format, in this case a pointer to an existing UIWebView) just calling:

self.webView = aUIWebView;

and remember to release it, 'couse you used "retain" in :

@property (nonatomic, retain)  UIWebView *webView;

release it with:

[webView release];


Did you @synthesize labelIBOutlet in the implementation file?


If you do not have labelIBOUtlet in your object declared it will fall on calling self.labelIBOUtlet because it do not exist. self == is in the object which is declared in header file

hope it makes it clear

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜