Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Feb 13, 2012, 11:57 AM   #1
chrono1081
macrumors 604
 
chrono1081's Avatar
 
Join Date: Jan 2008
Location: Isla Nublar
Confusion on instance variables being in the implementation file.

Hi guys,

I was referencing one of my Objective-C books* and noticed that there were instance variables defined in the implementation section of a class. I assume because the users in the class don't need to know about the instance variables, just the interface file but it leads me to two questions:

1. When did this start? I'm pretty sure its new as I've never seen it before. (Some internet research suggests its new in Xcode 4.2, but I can't find anything in Apple's docs about it).

2. If it's Xcode only, wouldn't this be a bad way to do things since some other IDE's support Objective-C, but not necessarily putting the ivars in the implementation section?

The book is the newest version (4.0) of Steve Kochan's programming in Objective-C. I have and read the 2.0 version and the classes do not have ivars in the implementation section, but I couldn't find in the new version where this difference, and the reasoning behind it is mentioned.
__________________
Mac Pro (2010): 3.33Ghz Intel Xeon (6 core) - 24 GB RAM - NVidia Quadro k5000
Macbook Air (2010): 2.13 Ghz Intel Core 2 Duo - 4GB RAM
chrono1081 is offline   0 Reply With Quote
Old Feb 13, 2012, 12:12 PM   #2
KnightWRX
macrumors Pentium
 
KnightWRX's Avatar
 
Join Date: Jan 2009
Location: Quebec, Canada
Maybe post some code of what you mean ? Are you sure these are "instance" variables ?

Usually, when doing this for methods, it's to make them "private". For variables, you can do so in the interface section using the @private directive, so no need to stick them in the implementation file.

However, I do use variables in implementation files, as a global symbol. These I use as "class variables", variables that survive and are accessible by all instances of the class or by class methods.
__________________
"What you leave behind is not what is engraved in stone monuments, but what is woven into the lives of others."
-- Pericles
KnightWRX is offline   0 Reply With Quote
Old Feb 13, 2012, 12:15 PM   #3
chrono1081
Thread Starter
macrumors 604
 
chrono1081's Avatar
 
Join Date: Jan 2008
Location: Isla Nublar
Here is some code straight from the book:

Code:
@interface Fraction : NSObject
{
    
}

-(void) print;
-(void) setNumerator: (int) n;
-(void) setDenominator: (int)n; 

@end
Code:
@implementation Fraction
{
    int numerator;
    int denominator; 
}

-(void)print
{
    NSLog(@"%i / %i", numerator, denominator);
}

-(void)setNumerator:(int)n
{
    numerator = n;
}

-(void)setDenominator:(int)n
{
    denominator = n;
}

@end
I did see in this version of the book it confirms that the change is new to Xcode 4.2, but that it will explain the changes later. I use this book only for reference so I'm not sure where it explains the changes (I haven't found it yet).
__________________
Mac Pro (2010): 3.33Ghz Intel Xeon (6 core) - 24 GB RAM - NVidia Quadro k5000
Macbook Air (2010): 2.13 Ghz Intel Core 2 Duo - 4GB RAM

Last edited by chrono1081; Feb 13, 2012 at 12:22 PM.
chrono1081 is offline   0 Reply With Quote
Old Feb 13, 2012, 01:09 PM   #4
Catfish_Man
macrumors 68030
 
Catfish_Man's Avatar
 
Join Date: Sep 2001
Location: Portland, OR
Send a message via AIM to Catfish_Man
This is a new feature of the most recent clang (aka Apple LLVM) compiler, specifically. It's useful to hide private state of classes.
Catfish_Man is offline   0 Reply With Quote
Old Feb 13, 2012, 03:56 PM   #5
kainjow
Moderator emeritus
 
kainjow's Avatar
 
Join Date: Jun 2000
I forgot about this feature. I like having only public things in the header, and this makes it even more cleaner. FWIW, it works with the Snow Leopard Xcode 4.2 build.
kainjow is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
Missing Tracks from iTunes in the Cloud AndrewLGP Mac Applications and Mac App Store 3 Jun 19, 2011 03:16 PM
iPad2 cannot be synced. The required file cannot be found. jeffreyk iPad Tips, Help and Troubleshooting 5 May 18, 2011 05:08 PM
How long do you think it will be for the new batch of MBPs to be in the refurb store? rhcpfan0 Buying Tips and Advice 2 Jun 11, 2009 07:26 PM
the iPhone 'x' could not be synced- the required file could not be found benfilan iPhone Tips, Help and Troubleshooting 2 Nov 12, 2007 12:57 PM
ipod cannot be updated. The required file cannot be found". davidwhitehall iPod 1 May 28, 2007 08:01 PM


All times are GMT -5. The time now is 12:37 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC