Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
November 15, 2018, 12:23:14 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
 
   Home   Help Search Login Register Chat  
Pages: [1]   Go Down
  Print  
Author Topic: Naming Setter and Getter Methods.  (Read 4613 times)
JackRabbit
Jr. Member
**
Posts: 54



Email
« on: April 05, 2012, 01:36:34 AM »

Greetings everyone.

When I first started this online course with ClassroomM. I was puzzled with the nature and use of getter and setter methods. I get it now.

But as I struggled with the concept, I suggested that it would be a good idea to start any setter method with the prefix "set" and any getter method with the prefix "get".

Others on the forum told me that starting the setter methods that way was just fine. But Apple doesn't like the getter methods starting with the prefix "get"; that convention suggested that the name of the getter method should be named the same as the instance variable that it is dealing with.

OK, I get that too. But why? Why does Apple not like this?

Does anyone know?

JR
Logged
seerex
Full Member
***
Posts: 177


Email
« Reply #1 on: April 05, 2012, 02:21:59 AM »

Well, i don't KNOW why, but i could guess that it's simply a convention, similar to the convention of apple naming class names with a capital letter, define macros with a prefix k or full capital and so on.

However, one way to make it all appear more logical, is to think like this:

Okay, if i have this int variable, that needs an instance variable from my other object, i would do like this:

Code: (Objective-C)
int newInt = [myObject intValue];

It seems logical here, coz you say "i'm gonna get the intValue, simply by placing the name there". Hm this is kinda hard to explain actually, but to me it makes sense Tongue it's like, if you need to get a certain variable, you just say the name of that variable, thus meaning thats the one you want to get.

Damn, it sounds like rubbish when putting it out in words, but to me it makes sense and seems logical Tongue

Sorry J, thought i could explain myself but failed Sad
Logged

Best Regards -
J.B.J
If you think i helped you could return the favor Wink

Subscribe to my channel on youtube: http://www.youtube.com/user/JBJProgramming

My apps:
- iTap Fruits (all countries)
- Toilet-Quiz (Denmark only)
- Zoo Kids (Denmark only)
- Diablo Item Database (All countries)
JackRabbit
Jr. Member
**
Posts: 54



Email
« Reply #2 on: April 05, 2012, 03:43:25 AM »

Hello seerex.

I don't think you failed. I see your logic clearly. But, when you said:
Quote
but i could guess that it's simply a convention, similar to the convention of apple naming class names with a capital letter

That is the real concern of my question. IS it just a formatting convention like class names starting with a capital letter, or is there a more practical reason.

It just dawned on me that I should write a simple program with getters that start with "get" and see what Xcode does.

I'll get back to you on this.


...



There. I did it. I coded in Program 3.4 exactly as it was in the book but I replaced the getter methods named "numerator"and "denominator" with "getNumerator" and "getDenominator" while leaving the 2 instance variables that the setter and getter methods accessed the same (numerator and denominator). And Xcode ran with out any problems. So there can't be any practical reason. It has to  be as you wrote, seerex a convention/formatting issue.

Problem solved. Or is it...... (anyone else want to add there opinion?)


JR
« Last Edit: April 05, 2012, 04:44:56 AM by JackRabbit » Logged
seerex
Full Member
***
Posts: 177


Email
« Reply #3 on: April 06, 2012, 02:35:31 AM »

Well surely you can create some methods, that ACTS like getter methods, but according to the compiler, they really arent. I mean, you can write a lot of methods that returns a value of an instance variable, but, if you synthesize the properties to have the compiler create getter and setter methods for you, it will still use the custom setter method created by the compiler, and not the getThisVariable method you wrote. That is a whole other method, so the only way to get the system to use the new getter method (if working with the dot-operator) is to follow the naming convention and override the getter method created by the system, if you know what i mean.

So always stick to that when / if creating your getter methods.
Logged

Best Regards -
J.B.J
If you think i helped you could return the favor Wink

Subscribe to my channel on youtube: http://www.youtube.com/user/JBJProgramming

My apps:
- iTap Fruits (all countries)
- Toilet-Quiz (Denmark only)
- Zoo Kids (Denmark only)
- Diablo Item Database (All countries)
rue
Jr. Member
**
Posts: 53


« Reply #4 on: April 22, 2012, 10:09:55 PM »

Apple introduced the dot notation "recently" (don't know which Xcode version).

So before the convention was:
x = [myObject getValue]

now, can be written shorthand in dot notation as
x = myObject.getValue   

... but it's kinda not readable as just saying
x = myObject.Value
though I admit if written in the old way, x = [myObject Value] , it doesn't read right.

Logged
2000ache
Newbie
*
Posts: 9


« Reply #5 on: April 21, 2013, 06:43:07 AM »

I am new here, so hi Smiley

I have little prior knowledge of OOP using .NET, and I am also a bit confused about the naming conventions for getters and setters. I too find naming the getter the same als the instance variable confusing. I do understand it is better to use this convention because if the @synthesize directive. However, what if I would name all my instance variables starting with an underscore (like I am used to in .NET) ?

Is that considered bad practise as well? I assume it will also break the @synthesize.

So basicly I'll have to get used to this 'apple comvention' ?
Logged
afterDark
Global Moderator
Full Member
*****
Posts: 131


« Reply #6 on: April 21, 2013, 09:31:35 AM »

Hi!

Well, actually the 'Apple way' is indeed to let the instance variable names start with an underscore. But, also when using @property / @synthesize, you can name the instance variables anything you like. The book for instance teaches you here to use the names without the underscore.
It is a bit complicated (it took me a while before I got it), you can study this thread http://classroomm.com/objective-c/index.php?topic=7822 if you like. The Apple documentation on the issue is linked in that thread as well, it is actually very clear.
Logged

I am just an amateur with Objective-C, don't let the moderator label fool you. Working my way through the book slowly.
2000ache
Newbie
*
Posts: 9


« Reply #7 on: April 21, 2013, 02:32:14 PM »

Thanks!

I'll read that. And I'll probably be using the underscore then Smiley

EDIT:
And I'm glad to finally having found a forum where I can ask my beginner questions on iOS / objective-c programming. It is sure hard to find such a place!
« Last Edit: April 21, 2013, 02:39:16 PM by 2000ache » Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents ゥ 2009 classroomM.com. All rights reserved.