Want to improve this question? Add details and cl开发者_运维技巧arify the problem by editing this post.
Closed 9 years ago.
Improve this questionI 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?
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)
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.
- What should my Objective-C singleton look like?
- http://en.wikipedia.org/wiki/Singleton_pattern
精彩评论