Why locationManger is not declared as property?


#1

@interface WhereamiViewController : UIViewController
{
CLLocationManager *locationManager;
}
@end

why we have not declared locationManager as property? Something like -

@interface WhereamiViewController : UIViewController
@property (nonatomic, weak) CLLocationManager *locationManager;
@end


#2

No other object will need to access the WhereamiViewController’s locationManager, so it doesn’t need an accessor. Some people just property everything because they are comfortable with it, but this is kind of dangerous: if someone else comes to edit your code, they will believe that accessing the locationManager is safe when it really isn’t (the WhereamiViewController maintains tight control over it for a reason).

Additionally, you wouldn’t declare the property as weak because the WhereamiViewCOntroller is the only object that has a reference to it and if that reference were weak, the locationManager would be immediately destroyed.


#3

[quote=“JoeConway”]No other object will need to access the WhereamiViewController’s locationManager, so it doesn’t need an accessor. Some people just property everything because they are comfortable with it, but this is kind of dangerous: if someone else comes to edit your code, they will believe that accessing the locationManager is safe when it really isn’t (the WhereamiViewController maintains tight control over it for a reason).

Additionally, you wouldn’t declare the property as weak because the WhereamiViewCOntroller is the only object that has a reference to it and if that reference were weak, the locationManager would be immediately destroyed.[/quote]
Understand now ! Thanks


#4

I just came here with that exact question in mind. You conclude Chapter 3 by arming us with @property, but one of the first things we do is throw the curly braces behind the @interface.

Good information, I hope it warrants a couple of paragraphs in the 4th edition! (Great book btw!)

Thanks!
Mark


#5

It is also possible to declare a private @property inside the implementation file. This is also an option if you prefer to use the properties getter and setter internally while keeping it private. Wouldn’t this work as well? The iOS Stanford class, Paul always uses properties, and just declares them privately if they are not to be shared with other objects.


#6

How do you declare a private property in the implementation file? I have not found anything that explains how to do that?


#7

This is an example of how you declare a private property in the implementation file. This code is an example from Paul H Stanford CS193p Winter 2013 class available on iTunes University. I recommend this course combined with the Big Nerd Ranch Guide iOS Programming. The lectures really help speed up the learning process once you have the text in your mind.

#import “Deck.h”
@interface Deck()
//So we put the @property declaration here in the implementation file.
@property (strong, nonatomic) NSMutableArray *cards; // of Card
@end

@implementation Deck

@implementation file.
- (void)addCard:(Card *)card atTop:(BOOL)atTop
{
// finish your method here.
}
@end


#8

Yah the class combined with the book makes for the best way to learn iOS!