Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Objective-C Basics (Retired) Introduction to Objective-C From Structs to Objects

Getter Setters, really?

Is it possible in Objective C that (if the variable is public) I can access the var directly like MyFancyInstance.MyFancyVar = 4; instead of MyFancyInstance.SetMyFancyVar(4);

I am not a programming noob, I know Java, Ruby and other languages VERY well, but until now I've had a slight phobia of C and Descendants... I know OO and everything, but nothing C or Obj-C specific.

4 Answers

Rashii Henry
Rashii Henry
16,433 Points

Are you asking if you declare a public variable? im assuming you mean decalring it in the header(.h) file. Can you access it with the dot syntax. Well, if you declare it as a property in your header(.h) file you can use the dot syntax in any scope of that class. Declaring a property automatically defines the setters and getters for you.

Yes but I would prefer something like this: [Sphere radius] to get a value and [Sphere radius:3] to set a value, or something like that? is it possible. Defining them isn't bad but using them is.

Ooops, I though this was a comment. Nvm this

Rashii Henry
Rashii Henry
16,433 Points

you could always just initialize the variable as soon as you create it, all in one line if your sure its going to remain static or non-changing. Whenever you start using brackets in objective-c your usually calling a method.. whether its on a class, a variable, etc. I believe you're understanding what you're doing, you just look at the syntax and get pretty confused.

there are different ways to define variables. you could easily declare: float radius = 3; and pass radius around whenever you need it. or you could declare it without initializing it: float radius; Then whenever you want to, you can change the value for radius in whichever scope you want. All of which, im pretty sure you know how to do. But as far as getting and setting values the way that you just mentioned, i dont think it works like that.

Oh great, so I have to make my own Dialect of Objective-C...

Rashii Henry
Rashii Henry
16,433 Points

yeah, its sorta like not knowing how to swim but you go ahead and just jump straight into the ocean.

I can do basic swimming, just not fancy high-class swimming.