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

Reply
 
Thread Tools Search this Thread Display Modes
Old May 16, 2011, 01:05 PM   #1
Schnigges
macrumors newbie
 
Join Date: Feb 2010
Location: Germany
Properties

Hi,

I started learning Objective-C a few weeks ago. By doing a lot of tutorials I already understood the main purpose of properties in Objective-C, but there are a few things, which are not quiet clear yet.
First question would be when it's better to only use a property instead of using a property + an instance variable, which are named the same.
Secondly, why would you name instance variable + property differently and then assign them in the @synthesize - part,
e.g: @synthesize window = _window

Thanks,
Schnigges
Schnigges is offline   0 Reply With Quote
Old May 16, 2011, 01:30 PM   #2
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
On the iPhone runtime you don't have the option of a declared property and no variable. So don't worry about that. Basically the option of not having the variable was added to the, so-called, "modern" runtime.

As for why you'd use a variable with a different name? Basically to prevent you assigning values directly to the variable as if you do this the setter does not get called and any side effects (like retain) don't get called.
robbieduncan is offline   0 Reply With Quote
Old May 16, 2011, 02:13 PM   #3
Schnigges
Thread Starter
macrumors newbie
 
Join Date: Feb 2010
Location: Germany
Thanks for the quick reply.
Regarding my second question...is the default behaviour that the setter is called when variable and property have the same names?
And how about IBOutlets / IBActions as properties...I've seen code where an instance variable was used, in another tutorial it wasn't. Are there any specific situations where using both should be preferred and where not?
Schnigges is offline   0 Reply With Quote
Old May 16, 2011, 02:48 PM   #4
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
This calls the setter
Code:
myObject.property = value;
From an instance method this calls the setter
Code:
self.property = value;
This does not call the setter (ever)
Code:
property = value;
I would suggest always ensuring the setter gets called.
robbieduncan is offline   0 Reply With Quote
Old May 16, 2011, 03:28 PM   #5
Schnigges
Thread Starter
macrumors newbie
 
Join Date: Feb 2010
Location: Germany
Great!

And what about the IB-thing...when should I use property only and when not?
Schnigges is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Instance Variables vs. Properties ahan.tm iPhone/iPad Programming 34 Oct 27, 2013 05:00 PM
How I Determine Runtime Properties PhoneyDeveloper iPhone/iPad Programming 1 Oct 5, 2013 06:23 PM
Does iOS 7 have addictive properties? cleo1 Wasteland 13 Aug 1, 2013 02:03 PM
iPhone: How to see properties of .mp3 file? TheShadowXX Jailbreaks and iOS Hacks 3 Apr 9, 2013 03:09 PM
Properties MickeyT iPhone/iPad Programming 16 Jun 26, 2012 03:39 PM

Forum Jump

All times are GMT -5. The time now is 04:12 AM.

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

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