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 Build a Self-Destructing Message iPhone App Relating Users in Parse.com Adding Friends by Tapping on a Table View Cell

Harry Stromfelt
Harry Stromfelt
2,985 Points

What is the benefit of making variables an @property? When is it ok not to/better to?

The title has the question, but I noticed in the tutorial that Ben says "we'll use the currentUser a lot, so let's actually make it a property" this made me wonder why we sometime leave it out etc.

Thanks

1 Answer

Stone Preston
Stone Preston
42,016 Points

if you store the value of the current user in a property, you can just reference that property anytime you need the current user instead of having to call [PFUser currentUser] all the time.

Harry Stromfelt
Harry Stromfelt
2,985 Points

Ok I sort of get it, if you didn't declare it as a property then even if you made a currentUser variable within the scope of a function it couldn't be called elsewhere. What would happen if you did PFUser *currentUser = [PFUser currentUser]; in the viewDidLoad bit? Would that also only exist during the viewDidLoad call?

Alternatively, with methods, do we always have to declare them in the header file, and if not when is ok to leave it out?

Thanks!

Stone Preston
Stone Preston
42,016 Points

What would happen if you did PFUser *currentUser = [PFUser currentUser]; in the viewDidLoad bit

that currentUser variable only exists in the scope of viewDidLoad. you could not access it anywhere else except inside viewDidLoad

with methods, do we always have to declare them in the header file, and if not when is ok to leave it out?

if you want method to be seen/available in other classes then you need to declare them in the header file. if they are not in the header and you try to call the method outside of the class itself it wont know it exists. So if you are planning on calling that method somewhere other than the class file itself, it needs to be in the header.