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 02-16-2011, 06:16 PM   #1 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 4
zip184 is on a distinguished road
Default Header file confusion

When I was beginning to teach myself objective c a few months ago (I'm still pretty new) I noticed in some examples of header files that had a few properties that referred to instance variables that were not defined in the @interface declaration block. Here's an example:

@interface MyClass : NSObject
{
float valueA;
}

@property (nonatomic) float valueA;
@property (nonatomic) float valueB;

@end

As you see, valueB has nothing defined in the interface declaration block but there's no compile warnings. The app I've been coding hasn't really defined anything between those brackets and everything seems to work fine. So what's the deal? Do properties automatically define instance variables that aren't there? Will not declaring instance variables in both places have a different effect? I can't find any information about this, perhaps because I don't know what to search for. Can someone clear my confusion?
zip184 is offline   Reply With Quote
Old 02-16-2011, 06:33 PM   #2 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

Did you @synthesize them in the implementation? @property doesn't actually do anything, it only describes how the accessor methods should be synthesized.
baja_yu is offline   Reply With Quote
Old 02-16-2011, 06:44 PM   #3 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 4
zip184 is on a distinguished road
Default

Quote:
Originally Posted by baja_yu View Post
Did you @synthesize them in the implementation? @property doesn't actually do anything, it only describes how the accessor methods should be synthesized.
Yes I did synthesize them. They work fine.
zip184 is offline   Reply With Quote
Old 02-16-2011, 06:55 PM   #4 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

More info is here: Loading…

Says:

Quote:
For the legacy runtimes, instance variables must already be declared in the @interface block of the current class. If an instance variable of the same name and compatible type as the property exists, it is used—otherwise, you get a compiler error.

For the modern runtimes (see Runtime Versions and Platforms in Objective-C Runtime Programming Guide), instance variables are synthesized as needed. If an instance variable of the same name already exists, it is used.
So, if an iVar doesn't exist synthesize will create it. For the sake of clarity I'd still prefer to declare it upfront.
baja_yu 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: 362
11 members and 351 guests
cpsclicker, dre, Error404, gmarro, jeroenkeij, Kirkout, Music Man, PavelMik, teebee74, whitey99
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,666
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, cpsclicker
Powered by vBadvanced CMPS v3.1.0

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