Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Object-Oriented Objective-C Diving Deeper - Classes, Properties and Methods Custom Classes Continued

Daniel Kokin
Daniel Kokin
1,496 Points

Setting and Getting question

I'm just now starting to understand the concept of setting and getting. However, I'm a bit confused about all the different ways. In the diner example there are few ways discussed:

//set values like this

 [table1 setSubtotal: 12.75];

 table1.subtotal = 12.75;

//get values like this

 float someValue = table1.subtotal;

However, I'm not sure how I would apply the following approach in a similar scenario. Is this just for elements with key value pairs? Can someone clear it up?

[grilledCheese setValue:<#(nullable id)#> forKey:<#(nonnull NSString *)#>]

1 Answer

Martin Wildfeuer
PLUS
Martin Wildfeuer
Courses Plus Student 11,071 Points

Let's assume grilled cheese is an instance of a class called Cheese. Cheese has a property of type string called country. This is what the setting this value would look like, just as in your example above.

grilledCheese.country = "Switzerland";
[grilledCheese setCountry:"Switzerland"];
[grilledCheese setValue:"Switzerland" forKey:"country"];

Dot notation, that is, the first example, is the preferred way. This is explained in detail in the Apple Developer Docs: Key-Value Coding Fundamentals.

Hope that helps :)