Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 01-27-2012, 04:49 AM   #1 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 97
iphone.savvy is on a distinguished road
Default DOT-NOTATION Confusing me

HI EXPERTS,

I have a class A in which i have declared some variable 'var' in the header file and have property for that Variable.
In Class B i have an instance of Class A and through which i am calling 'var' .
Calling variable like below,
aClassObject.var , it gives me an error , but in many ways i noticed variable calling using Dot.

And When i call as follows,
[aObject var] No ERROR and NO WARNING.
This is confusing me call the messages using square brackets but it is also calling variable.
Can anyone of you just put some light on this?
iphone.savvy is offline   Reply With Quote
Old 01-27-2012, 06:29 AM   #2 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 97
iphone.savvy is on a distinguished road
Default

When we can call the messages with the dot notation.
Ex:
@interface A:NSObject
{
NSString *text;
}
-(void)initialise;
@end

@implementation A
-(void)initialise {
text = @"Hello Viewer";
}

...Now inside the method of class B
(void)doSomething
{
A *aObj = [[A alloc]init];
aObj.initialise; //Works Fine
[aObj initialise]; // Works Fine

So why can't we create an instance of A like below,
A *aObj = A.alloc.init;

Why? Any Thoughts?
iphone.savvy is offline   Reply With Quote
Old 01-27-2012, 06:35 AM   #3 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by iphone.savvy View Post
HI EXPERTS,

I have a class A in which i have declared some variable 'var' in the header file and have property for that Variable.
In Class B i have an instance of Class A and through which i am calling 'var' .
Calling variable like below,
aClassObject.var , it gives me an error , but in many ways i noticed variable calling using Dot.

And When i call as follows,
[aObject var] No ERROR and NO WARNING.
This is confusing me call the messages using square brackets but it is also calling variable.
Can anyone of you just put some light on this?
You need to read up on properties.

When you declare an @property in your header, you are telling the system that you will create a setter and a getter method that can be used to read or write values to the instance variable that is used to save that property's variable.

When you add an @synthesize statement, the compiler generates a setter and a getter method for your property:

the getter

Code:
- (type) var;
The setter:

Code:
- (void) setVar;
When you use code like this:

Code:
anotherVar = anObject.var;
The compiler recognizes this as a property reference. If you haven't declared a property, the compiler generates an error.


The statement

Code:
anotherVar = [anObject var];
is an explicit call to the getter method.

The compiler will generate a WARNING (not an error) if it can't find that method. That's because method calls are resolved at runtime. If there is a method with that name at runtime, the code will work. If not, it will crash.

In order for class A to refer to a property of class B, the .m file for class A needs to #import the header of class B. My guess is that you forgot that step, and that you actually got a warning when you first compiled your code. If you select clean from the project menu and rebuild, I bet you'll see a warning about an unknown method for that call.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 405
10 members and 395 guests
Atatator, condor304, FrankWeller, imac74, MAMN84, mraalex, n00b, PowerGoofy, tim0504, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,674
Threads: 94,123
Posts: 402,908
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Atatator
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 05:57 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0