0

How can i access NSObject class method? i have one NSobject class for AudioStreaming where the audio gets start and stops, when i changing the tab i want to stop that streamer by method define in AudioStreaming class how can this be done.

Thank you.

mactalent
  • 971
  • 2
  • 13
  • 24

1 Answers1

3

A class method is invoked using the class name and the method. Thus if you have:

@interface AudioStreaming
{
    // ...
}
+(void)startAudio;
+(void)stopAudio;
@end

then all you need to do to call these methods is:

[AudioStreaming startAudio];
// ... do other things...
[AudioStreaming stopAudio];

Note that you cannot refer to instance variables within a class method (as there is no current instance!).

If you want to implement a Singleton, this StackOverflow Singleton answer is a good start.

Community
  • 1
  • 1
gavinb
  • 19,278
  • 3
  • 45
  • 60
  • But StopAudio method include Class Variable,so by accessing like [AudioStreaming stopAudio]; gives warning:instance variable 'stopreason' accessed in class method. – mactalent Jan 07 '10 at 10:00
  • Objective-C does not have class variables as such (not in the same way as say C++). So the warning is correct; you can't reference an instance variable in a class method. You can move the declaration from the @interface block to the implementation file and make it a static instance (like you would in C). Careful with instantiation and one-time initialisation. The Apple ObjC docs have some good guidance on this. – gavinb Jan 07 '10 at 11:01
  • so need your guidance for that what to do now? i cannot make it static as this method and variable are used in other files also so how do i call my stopAudio method from the applicationDelegate file. – mactalent Jan 07 '10 at 12:26
  • It sounds like what you're after is a Singleton. The answer in this SO question http://stackoverflow.com/questions/145154/what-does-your-objective-c-singleton-look-like seems quite a good start. So your class needs two aspects: the actual implementation for the class so you have an object to do the work (ie. manage audio channels, state, etc) and the singleton infrastructure to let you easily call the instance that actually does the work. Then you can call [AudioStreaming stopAudio] from your app, and it will use the singleton to do the work. Make sense? – gavinb Jan 08 '10 at 11:57