Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old May 13, 2013, 03:22 AM   #1
printz
macrumors regular
 
Join Date: Dec 2012
Is the dot notation supported in OSX 10.6- ?

In Xcode 4 I can say a.b instead of [a b] (when selector b has no arguments). I've been getting warnings and errors when trying to compile this with Xcode 3 on OSX Snow Leopard, but not everywhere I used dots. Shall I consider that if I want to make my code compilable in 10.6 and below, I should only use bracket notation?
printz is offline   0 Reply With Quote
Old May 13, 2013, 05:56 AM   #2
gnasher729
macrumors G5
 
gnasher729's Avatar
 
Join Date: Nov 2005
Quote:
Originally Posted by printz View Post
In Xcode 4 I can say a.b instead of [a b] (when selector b has no arguments). I've been getting warnings and errors when trying to compile this with Xcode 3 on OSX Snow Leopard, but not everywhere I used dots. Shall I consider that if I want to make my code compilable in 10.6 and below, I should only use bracket notation?
It's not a matter of the operating system where you want to run your code, but of the Xcode version that you are using. In some earlier Xcode versions dot notation only worked for things that were explicitly declared as properties. In new versions, it also works when you have just the setter and getter methods declared.

There are other differences; in old Xcode versions you get a warning when you use a method that is declared later.

So the answer: No, you don't need to use brackets notation. Look at the warning and fix what it says. You may have to declare a property, you may have to add an "interface" with missing declarations in front of your "implementation".
gnasher729 is offline   0 Reply With Quote
Old May 13, 2013, 11:02 AM   #3
Guiyon
macrumors 6502a
 
Join Date: Mar 2008
Location: North Shore, MA
Quote:
Originally Posted by printz View Post
In Xcode 4 I can say a.b instead of [a b] (when selector b has no arguments). I've been getting warnings and errors when trying to compile this with Xcode 3 on OSX Snow Leopard, but not everywhere I used dots. Shall I consider that if I want to make my code compilable in 10.6 and below, I should only use bracket notation?
If it's not a declared property, don't use dot notation - period. The fact that it works in some situations is not documented behavior and should not be relied upon.
__________________
Make life easier if you have a programming question!
http://www.sscce.org/
Guiyon is offline   0 Reply With Quote
Old May 13, 2013, 11:08 AM   #4
ArtOfWarfare
macrumors 603
 
ArtOfWarfare's Avatar
 
Join Date: Nov 2007
Send a message via Skype™ to ArtOfWarfare
Quote:
Originally Posted by Guiyon View Post
If it's not a declared property, don't use dot notation - period. The fact that it works in some situations is not documented behavior and should not be relied upon.
Just going to throw this out here: properties need not be backed by iVars. I often make read only "properties" that, as a getter implementation actually perform a calculation and return that calculate value (instead of an iVar.)

IDK if that was obvious to everyone else, but I only started doing it a few weeks ago. It makes for a lot neater looking code, I think.
ArtOfWarfare is offline   0 Reply With Quote
Old May 13, 2013, 11:24 AM   #5
gnasher729
macrumors G5
 
gnasher729's Avatar
 
Join Date: Nov 2005
Quote:
Originally Posted by Guiyon View Post
If it's not a declared property, don't use dot notation - period. The fact that it works in some situations is not documented behavior and should not be relied upon.
If it compiles, then it works - period. obj.property is compiled in exactly the same way as [obj property], and obj.property = value is compiled in exactly the same way as [obj setProperty:value]. No reason whatsoever to avoid this.

Common sense dictates that you use this for properties. NSMutableString* aString = NSMutableString.alloc.init works but is almost criminal.
gnasher729 is offline   0 Reply With Quote
Old May 14, 2013, 10:47 AM   #6
Guiyon
macrumors 6502a
 
Join Date: Mar 2008
Location: North Shore, MA
Quote:
Originally Posted by gnasher729 View Post
If it compiles, then it works - period. obj.property is compiled in exactly the same way as [obj property], and obj.property = value is compiled in exactly the same way as [obj setProperty:value]. No reason whatsoever to avoid this.

Common sense dictates that you use this for properties. NSMutableString* aString = NSMutableString.alloc.init works but is almost criminal.
I'm not saying it doesn't work and I'm not arguing that @property isn't syntactical sugar but just because it works *right now* doesn't mean it's right. Treating the dot operation outside of its expected use (accessing/mutating properties) is like depending on a private API; sure it works for now but you're just asking for the rug to be pulled out from under you.
__________________
Make life easier if you have a programming question!
http://www.sscce.org/
Guiyon is offline   0 Reply With Quote
Old May 14, 2013, 10:55 AM   #7
gnasher729
macrumors G5
 
gnasher729's Avatar
 
Join Date: Nov 2005
Quote:
Originally Posted by Guiyon View Post
I'm not saying it doesn't work and I'm not arguing that @property isn't syntactical sugar but just because it works *right now* doesn't mean it's right. Treating the dot operation outside of its expected use (accessing/mutating properties) is like depending on a private API; sure it works for now but you're just asking for the rug to be pulled out from under you.
Well, no. I said "if it compiles then it works". That's something you can rely on. If it ever breaks (which is highly, highly unlikely), then the compiler will tell you. Software shipped to customers isn't going to break.

And there are plenty of getters/setters in Cocoa that haven't been declared as properties but that are in fact properties; there is no reason not to use dot accessors for them.
gnasher729 is offline   0 Reply With Quote
Old May 14, 2013, 11:16 AM   #8
ArtOfWarfare
macrumors 603
 
ArtOfWarfare's Avatar
 
Join Date: Nov 2007
Send a message via Skype™ to ArtOfWarfare
Quote:
Originally Posted by gnasher729 View Post
And there are plenty of getters/setters in Cocoa that haven't been declared as properties but that are in fact properties; there is no reason not to use dot accessors for them.
I agree with this. It'd be nice if Apple would formally declare them all as properties, though.
__________________
Don't tell me Macs don't last: 2007 iMac, 2007 Mac Mini, 2008 MacBook Air, all Vintage.
(iMac obsoletion: April 28, 2015, MBA: October 14, 2015, Mac Mini: March 9, 2016)
ArtOfWarfare is offline   0 Reply With Quote
Old May 14, 2013, 02:08 PM   #9
Sydde
macrumors 68000
 
Sydde's Avatar
 
Join Date: Aug 2009
@property requires a method or methods to be accessible. The easy way to do this is with @synthesize, but otherwise you can easily code the method yourself. For example, you could consider length to be a property of NSString, even though it is not clear whether the value is stored or calculated. In fact, the following code works as one might expect:
Code:
NSMutableString *aString = [NSMutableString alloc];
aString = aString.init;
[aString setString:@"words"];
NSInteger itsLen = aString.length;
Though, that one would use dot syntax with - init is highly dubious, it just points out that a method with no parameters that returns a value can use dot syntax. (I tried to write aString.string = @"words"; but my compiler would not accept that.)
__________________
You got to be a spirit. You can't be no ghost.
Sydde is online now   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

Similar Threads
thread Thread Starter Forum Replies Last Post
All iPads: Question for composers/musicians. Suggest the best size for music notation apps Vodka iPad 6 Oct 26, 2013 04:15 PM
iPad: Dot on Back? Squilly iPad 1 Oct 23, 2013 03:44 PM
"@" notation instead of initializer? dantastic iPhone/iPad Programming 6 Jun 14, 2013 08:25 AM
List of graphics cards supported by OSX 10.8.3 mistasparkaru OS X 10.8 Mountain Lion 1 Apr 24, 2013 06:26 AM
A beginner, confused with dot and bracket notation Vishwas Gagrani iPhone/iPad Programming 13 Oct 3, 2012 02:38 AM

Forum Jump

All times are GMT -5. The time now is 11:48 AM.

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

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