Accessing class method in iPhone - iphone

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.

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.

Related

How to make inherited class be able to see parent's hidden methods in Objective-C [duplicate]

This question already has answers here:
What is the Objective-C equivalent of a public get/protected set property in C#
(3 answers)
Closed 9 years ago.
I have got two classes, Class1 and Class2, the second one inherited from the first one. I need to override -update method of Class1 to achieve my goals. The changes of -update method in inherited method are performed in the middle of code, so I can not use [super update]. Thats why I need to copy-paste original method from parent to inherited class. This method is using private methods of parent, so when I am trying to do overriding, I got warnings about absence of private methods because Class2 imports only Class1.h. To clarify, here is the code:
Class1.h:
#interface Class1 : NSObject
-(void) update;
#end
Class1.m:
#interface Class1 (Private)
-(void) private1;
-(void) private2;
#end
#implementation Class1
-(void) update
{
[self private1];
[self private2];
}
-(void) private1
{
// some code
}
-(void) private2
{
// another code
}
#end
Class2.h:
#interface Class2 : Class1
-(void) update;
#end
Class2.m:
#implementation Class2
-(void) update
{
[self private1]; // warning here
// do my own stuff between private methods, that is the reason of inheritance
[self private2]; // warning too
}
#end
Also, Class1 is not in my ownership, it is the one from open-source library (Cocos3D, to be precise), so I could not change it (and that is why I do inheritance).
The question is: how can I remove warnings? The only solution I can see is to copy private methods' signatures to Class2, but it seems to be a dirty trick. Or, it would be perfect if somebody points not how to remove warnings, but how to achieve my goal in changing method more nicely.
No need for swizzling, performSelector: or any other runtime buggery.
Just move or copy this:
#interface Class1 (Private)
-(void) private1;
-(void) private2;
#end
To the beginning of your subclass's .m file.
Normally, when trying to achieve something like an #protected scope for methods, the declaration of such would be in a header file like Class1_Private.h and that header would be set to the private role in Xcode.
As others have noted, exposing a private method via this mechanism is a bit dangerous. Given that Cocos2D is open source, that danger is mitigated somewhat or you could always just modify it directly. Of course, doing so means you effectively have a branch, which is costly to maintain.
If this is something that other developers are likely to do, I'd suggest filing a bug with Cocos2D requesting that the methods be exposed for subclassing purposes.
The way Apple does this is to make any methods that can be overridden in a subclass public, but document the fact that they shouldn't be called directly and are only there to be overridden. Documentation for Apple's code is (usually) comprehensive, and often referred to, so this works. Of course, typical third party code isn't as well documented, nor is documentation as likely to be read...
Another solution: I often create a second header called "MyClass+SubclassMethods.h", and publicly declare subclass-overrideable, but otherwise private methods in a category there. In source files for users of the base class and subclasses, this isn't imported, so the methods stay (effectively) private. In subclasses of the base class, I import "MyClass+SubclassMethods.h" to allow me to override/call these methods. It's not a perfect solution, but without the notion of 'protected' methods in Objective-C, I'm not sure there's a much better one.
A category, as suggested in another response is a good solution given that Class1 cannot be modified. If it were possible to make adjustments to source, a class extension declared in a separate header file and #imported in both the Class1 implementation, and the Class2 implementation file would be the way because then you can share not only private methods but also instance variables marked #public or #protected this way. Find out more about class extensions from here:
http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/ProgrammingWithObjectiveC/CustomizingExistingClasses/CustomizingExistingClasses.html
Briefly, a class extension is a bit like a category (sometimes called a private category) but it can only be declared in the same compilation unit where the class it extends is implemented, and it can declare (and auto-synthesize) properties and instance variables. There's a source code template included in Xcode for creating class extensions (see below.)
You should be able to solve this problem using categories to extend your original class and try method swizzling to achieve your desired behavior.

How to get super class delegate and update the variable in super class from subclass

#interface first{
nsstring one;
second secondobject;
}
#interface second{
nsstring two;
}
in the above classes from second class I want to update the first class "one" string.
I knew that we can update the "two" string from the first class
but I want to update the string "one" from second class
should not use appdelegate
should not use inheritance
*I want to know like our AppDelegate has the [[uiapplication sharedapplication]delegate]
by getting above delegate of appdelegate we can access properties of appDelegate class
Like this how can we can get the delegate for first class and access the properties of first class from the second class.
if any pictorial tutorial for tree structure please specify the link
here is my structure
It seems that you don't really understand the meaning of delegates and what they are used for.
In your example class first aggregates (contains) instance of class second. It means that the reference to that class is an ivar. You can access all public members, properties and methods of that instance from class first.
If you want to access the ivars of the superclass then you can do that like they were declared in the child class.
Delegates are used when you need to notify another object about something during execution. Objects usually have weak references (they don't retain) to delegates.
So i think the best choice for you would be to read some good book about object oriented programming. This is really good book about that
You can pass a pointer from the first class to the second, and the second can use this to call methods or access data members in the first class.
Or if first will be a singleton class (like UIApplication), you can set up a class method (like sharedApplication) that second can call to get a reference to the first object.
Generally, structuring iPhone apps using the MVC pattern helps to reduce these kinds of tangles.

iphone - delegates

I have an object that belongs to a class. Lets call it classA. This classA is a subclass of classB.
ClassA has a delegate protocol.
I create an classA object on my main code. This object is inside a view. Lets call it viewX.
Now I am in classB and I would like to get a reference to viewX.
remember that classA has a delegate protocol, so it has a reference to its delegate, that is the viewController where viewX is. From class A I can access viewX doing [delegate view], but how do I do that from classB???
thanks.
If you have access to class B, I would say you should add a variable to the class of type id, and set that variable as the view, and that would be a very easy way to do it. Otherwise, I don't think it's possible. But I may be wrong.

Unnecessary warnings in the code

I am not able to find out why am I getting unnecessary warnings like:
"Method 'someMethod' not found"? Though at run time it is executing this method and I am getting the desired results. FYI... The called method resides in separate class which i have already imported in my class.
Usually one of two reasons:
1) You haven't casted the object that you're calling that method on correctly.
[(UITableView*)myTableView setDelegate:self];
2) The method that you're calling may not be in your custom Class' (public) #interface
#interface MyCustomClass : NSObject {
}
- (void)doSomethingReallyImportant;
#end
If you are trying to do something to an object, did you cast your object to the object's class?
If you are trying to access a method in your implementation of a class, do you have that method declared in your .h?
Then you have probably not put that method in the class' #interface. You should if it is a public method.
Being able to compile without warnings is a good thing.

"An instance method you define in a category of the NSObject class might be performed not only by instances but by class objects as well

Can you explain "An instance method you define in a category of the NSObject class might be performed not only by instances but by class objects as well", i have come across this sentence while reading an Objective C guide...! But i am not able to get it.
Instance method is a method you can call on an object (as opposed to a class). Each object is an instance of a certain class (just as each of us is an instance of a human1), that’s why we talk about instance methods. For example when you say [someArray count], you are calling an instance method called count on some array object (= instance of the NSArray class).
Class methods are called on classes, for example [UIApplication sharedApplication] is a class method of the UIApplication class. You can’t call an instance method on a class, nor can you call a class method on an object.
Category is a way to extend behaviour of existing classes, a kind of plugin you stick on an existing class. For example by writing this:
#interface NSObject (SampleCategory)
- (void) sayFoo;
#end
#implementation NSObject (SampleCategory)
- (void) sayFoo {
NSLog(#"Foo!");
}
#end
…you make it possible to call [anObject sayFoo] on any object derived from NSObject. And now we are getting to the point of the sentence: NSObject seems to be special, because when you declare an instance method of NSObject using a category (such as our sayFoo), you can call this method even as a class method: [NSObject sayFoo], [NSView sayFoo], etc.
1] Sorry, Googlebot!