This is an extremely basic question. When I initialize an object like so…
UIView *view;
if (!view) {
view = [[UIView alloc] init];
}
// Do something with view
I get no compiler errors, but I notice Xcode doesn’t highlight UIView like it normally does. Am I using a bad practice to initialize?
I see two problems with that code:
viewmight be 0, a pointer to something else, or a garbage value.Are you actually declaring this as an instance variable somewhere else?
As far as highlighting, which “UIView” isn’t highlighted? Both? I’d try reloading the file or restarting Xcode.
To fix the two issues I named, replace that with this code:
That raises another issue: under ARC, your newly allocated view will be deallocated when the variable goes out of scope unless you store it in an instance variable or the framework (e.g. superview) stores it somewhere else.
If you’re using manual retain–release instead of ARC, you’ll leak a UIView every time that method is called.