Ted
Ted

Reputation: 3885

clarifying on properties in objective C

Sorry for the simple question.
When I see a definition of a property inside the h file, but outside of the class @interface scope, what does it mean ?

@property (nonatomic, readonly) RMMapContents *mapContents;

Here is the code:

@class RootViewController;
@class RMMapContents;

@interface MapTestbedAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;

    //MAIN VIEW
    //==============

    RootViewController *rootViewController;


    // NETWORK DATA
    // =============

    NSMutableArray  *photoTitles;         // Titles of images
    NSMutableArray  *photoSmallImageData; // Image data (thumbnail)
    NSMutableArray  *photoURLsLargeImage; // URL to larger image
    NSMutableData *receivedData;
    NSURLConnection *theConnection;
    NSURLRequest *request;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet RootViewController *rootViewController;
@property (nonatomic, readonly) RMMapContents *mapContents;


@end

Inside a function I see this line:

- (void)foo:(xyz *)abc{
  ..
  RMMapContents *mapContents = [self mapContents];
  ..
}

So, taking it from C++, the mapContents seem like it is not a global scope var (after all, that's why they call them properties, right?), but isn't defining the same name again inside the function weird a bit?

I hope someone can clarify a little here.
Thanks!

Upvotes: 2

Views: 136

Answers (4)

user523234
user523234

Reputation: 14834

Look for a method in your class with a name mapContents that will return a initialization to your RMMapContents class.

Basically this line RMMapContents *mapContents = [self mapContents]; says that initializing an instance of RMMapContents called mapContens using the method mapContents.

Upvotes: 0

Shyam Bhat
Shyam Bhat

Reputation: 1600

The scope of the @interface block extends upto the @end keyword and is not restricted to the braces {}.

So the @property declaration lies very much inside the scope of the @interface and like cli_hlt rightly answered, it acts like a substitute to setter and getter methods for the mapContents property.

so a property named mapContents, would have setters and getters which look like this :

- (void)setMapContents; //setter

- (RMMapContents *)mapContents; //getter

and would can be accessed from within the class using these methods:

[self setMapContents:newContents];

AND

RMMapContents *contents = [self mapContents];

Upvotes: 2

cli_hlt
cli_hlt

Reputation: 7164

Well, a property is not just a variable. A property is a variable plus its setter and getter methods. A property is usually said to be backed by a variable, which usually(but not always) has the same name as the property itself.

So there are basically three scenarios:

  1. The developer has redefined the backing variable, look for something like:@synthesize mapContents=mapContents_, at the beginning of the implementation -> no problem here.

  2. The compiler defined the variable to be something you don't now but not equal to mapContents - > no problem.

  3. The property backing variable is indeed called "mapContents", so then the local definition hides the global definition (look for a compiler warning here). But by calling [self mapContents] you will not access the global variable but call the getter, which in turn will access the class variable (because then the local mapContents is out of scope)

Hope this helps.

Upvotes: 1

Mil0R3
Mil0R3

Reputation: 3956

global var mapContents is readonly,in foo function , create a new pointer,then you can change the value of inner var.

Upvotes: 0

Related Questions