Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

johnmerlino

macrumors member
Original poster
Oct 22, 2011
81
0
Hey all,

When a user logs in, I want to store that user instance in a method called currentUser. And in all my UIViewController classes, I would like to access certain properties of the currentUser, such as its email and id. I tried creating a category, but it would not allow me to synthesize because it doesnt allow instance variables. I do not want to subclass. Is there another way to do this?

thanks for response
 

chown33

Moderator
Staff member
Aug 9, 2009
10,750
8,422
A sea of green
in essence, I am looking something similar to a ruby module or php namespace.

Define a singleton class: MyCurrentUser. Put everything there.

If you had multiple users, then a singleton object/class might be a problem. But if there's always exactly one current user (or none), then a singleton is a perfectly fine solution, and doesn't need subclassing.
 
Last edited by a moderator:

mduser63

macrumors 68040
Nov 9, 2004
3,042
31
Salt Lake City, UT
I think chown33's suggestion is a good one, and is probably the way to go. Your idea of a category on UIViewController probably isn't really the best approach.

That said, I thought you might be interested to know that it's possible to sort of "fake" adding instance variables in categories using Objective-C runtime functions. See the documentation here.
 

jnoxx

macrumors 65816
Dec 29, 2010
1,343
0
Aartselaar // Antwerp // Belgium
Look up
"SynthesizeSingleton"
It's a header file someone created (for iOS5, need to change the release method to "oneway release".
If you follow it's documentation, you can create an instance of a class that is hold in memory thorough your entire application.
So if you have a Singleton of CurrentUser, and make his property's via a property public available via it's getters/setters.
You can acces this instance in any class, with [[CurrentUser sharedCurrentUser] currentUser].properties
Go look up a bit ^_-
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.