views:

533

answers:

2

I created an object in the method viewDidLoad:. In another method that I create, I want to access this object.

One way to do it is to declare the object in the h file.

Another way to do it is to pass it as a parameter.

Are there any other ways?

+15  A: 

There are many ways to do this. This is not an exhaustive list.

  • pass it as parameter

  • declare a global

  • stick it in NSThread's +threadDictionary

  • declare a class method that returns the object

  • stick a reference to the object in an instance variable

  • declare a function or method that has a static local that stores the object and returns it

  • use associated references to attach it to some random object somewhere that both methods can get to

  • map a hunk of memory at a known address and write the reference into the first word of the page

  • archive the pointer to NSUserDefaults and read it back

  • archive the pointer or object to a file in the filesystem and read it back

  • draw the address into an image and use optical character recognition to grab it back

Without more details, it is difficult to say which is the best approach (beyond saying that I would be exceedingly surprised if the last 4 were the right solution).


Some additional:

  • use audio synthesis to say the address, voice recognition to read it back (@chockenberry says "ou could whisper to get a weak reference.")

  • @boredzo suggested that you simulate an Apple ][/C64 casette interface

  • in the audio vein, the most practical is likely to use morse code as it is easy to recognize

  • you could probably use a push notification, but then you'd have to count on the user pushing a button to give you the reference back (assuming iOS)

bbum
I've actually done the Apple II cassette output (but not input): https://bitbucket.org/boredzo/file-to-audio-file/wiki/Home The protocol includes a header that should make it easy enough to recognize—possibly even easier than Morse code.
Peter Hosey
You could also post the object via an `NSNotification` and have the other method be the notification's action
Dave DeLong
You can post the data you want to save to Twitter via a simple API call along with a hashtag, then set up a Twitter search for that hashtag. Hit the RSS feed for that hashtag and read the body of the RSS payload. The data you put there should be back. Unfortunately you're limited to 140 chars (minus hashtag length). For anything larger I'd consider an instance variable in my view controller. But that would be doing it the hard way.
Ramin
If you come from a JavaScript background, you might find it convenient to hide the storage behind a pair of getter/setter closures.
Ahruman
The twitter solution is not a good idea, twitter is sometimes offline, meaning that your application will not work when twitter is not working.
Jacob
+2  A: 

It is true there are a lot of options. Without knowing more I will just guess that the most likely answer to your question is to use a singleton. Look up the "Singleton design pattern" as this is the most likely answer to your question.

Jacob