iphone, calling a method of different class in cocos2d layers? - iphone

I have two classes, both are subclasses of CCLayer,
I want to call a method of first class into second class, what should I code?

Your question is not providing much detail, but from my understanding of what you say, you need the following:
a selector in the public interface of your first class;
a pointer ivar in the second class that you will properly initialize so that it points to an instance of the first class;
In this way you will be able to call the first class' method from the second class.

Related

Swift. Let to override but not to call the method

Is there any way to let the method of the superclass be overrided, but not called directly?
For example: A inherited from B. There is two methods. One is final and must be called, second is overridable but shouldn't be called, only override.
I tried #available and private but that don't fit. I think that it can be reached by delegate, but maybe there is another way?
For example, you can throw an error in your method that will say that this method shouldn't be called and child class should override it. But, of course, it is no compile time restriction, only runtime.
Also it has sense for you to read discussion here: Abstract functions in Swift Language

How to select a specific class from set of classes which are implementing same interface class?

How to select a specific class from set of classes which are implementing same interface class ?
You don't call a method directly from an interface, you call it on a reference that points to an instance of a class. Whichever class that is determines which method gets called.

When to use a Class having both static methods and instance methods in IPhone

I have created a class which contain only static method in iPhone. The class was mainly to do my core data operations. But suddenly, I had a need to make a method call in a view controller, when an insertion of data in to a table got completed.
At first, I decided to send an NSNotification, once the loop finishes iteration. But then, since I need to use this only for single time, I decided not to go for NSNotificationCenter, instead to use delegation.
Now I have many static method, and two instance methods:
//1
-(id)initWithDelegate:(id)delegate;
//2
-(void)insertContentsInToTheTableFromArray:(NSArray *)contentArray;
Is this a good design pattern, to have both instance methods and class methods in this class. Please share your thoughts.
We can use both method in one class. We know that instance methods use an instance of a class, whereas a static method can be used with just the class name. But the static method is a convenience method that use on many foundation classes.

Calling methods from another class in objective-c

I know usually, when you want to call a method on another object, you do:
NewObject *object = [NewObject alloc]init];
[object callMethod];
But I created a class that isn't an object itself meaning it doesn't have properties or memory management. It has a couple methods that calculate some stuff.
From any other class, all I have to do is import the header for this class and do:
#import "MyClass.h"
[MyClass callMethod];
Why in this case do I not have to alloc init? It works just fine.
It sounds like you are trying to call a class method. These are methods which have been defined as:
+(void) myStaticMethod;
instead of
-(void) myMethod;
The plus sign indicates that the method does not use any fields, and thereby does not need to instantiate the object.
In your example, "object" is an instance of a class "NewObject" which has been allocated memory and initialized. Where-as your example, "MyClass" is only a class which because it has static members declared as above, does not need to be instantiated.
Class methods provide a nice way to combine a bunch of related functions into one place, rather than having them spread out in the regular namespace, as would usually be done in straight C. You can also have both class methods and instance methods in the same class, using the class ones when needed, and instantiating the class to use the instance ones when needed.
EDIT: Changed terminology to refer to class methods instead of static methods.
because you are calling a class method. You only need to alloc init objects. Classes only need to be included but not alloc inited. So you don't need to init an NSString class, say.
Edit:
Let's just have some nonsense examples:
+ (void)classMethod {
NSLog("Hi!");
}
[SomeClass classMethod]; // prints Hi!
- (void)instanceMethod { // (say it's an instance method of NSString)
NSLog(self);
}
[#"someNSString" instanceMethod]; // prints someNSString. But you need to have a string first, otherwise you cannot use this method.
There is a difference between "instance" methods (normal ones), that have to be called on an object and have access to self, and "class" methods (called static, in many programming languages), that are invoked on the class and thus do not have a self.
Class methods are similar to C++ static methods, in that they can be invoked without creating a concrete instance of the class. The usefulness of this is you can call a class's specialized factory methods to create a new instance; or, you can define a utility library under the scope of a class that may or may not provide concrete instances depending on the task.
Look at NSDate and NSNumber are good examples of this in the Foundation framework.

objective c categories and inheritance

If a method is defined in both a class and a category on that class, it is undefined which implementation will be called.
But how does this interact with inheritance? Specifically:
Given a superclass category method and a regular method in the subclass, is it guaranteed that the subclass implementation will win when called on a member of the subclass?
Given a superclass regular method and a subclass category method trying to override it, is it guaranteed that the subclass category implementation will win when called on a member of the subclass?
Given a superclass category method and a subclass category method, is it guaranteed that the subclass category method will win when called on a member of the subclass?
Lets just put it this way. Don't override methods using categories, period, ever, end of answer.
If a method is defined in both a class and a category on that class,
it is undefined which implementation will be called.
That is incorrect; the category method will always win. What won't work, though, is if you have multiple categories that implement the same method, then the "which one wins" is undefined.
It is generally "last loaded wins", but that really isn't a hard rule, either.
Note that since many classes will internally have their implementation dividing across categories for code organization purposes, you can't rely on the first rule anyway.
In short, what Joshua said; Do not override methods using categories.
Beyond the inheritance reasons, you are also viciously breaking encapsulation when you do so. It isn't that a category based implementation overrides an existing method, it entirely replaces it. Thus, if you don't reproduce every last internal implementation detail, including bugs, your replacement won't quite work right and debugging it will be hard.
From what I test
Given a superclass category method and a regular method in the
subclass, is it guaranteed that the subclass implementation will win
when called on a member of the subclass? => subclass wins
Given a superclass regular method and a subclass category method
trying to override it, is it guaranteed that the subclass category
implementation will win when called on a member of the subclass? =>
subclass category wins
Given a superclass category method and a subclass category method,
is it guaranteed that the subclass category method will win when
called on a member of the subclass? => subclass category wins
Take a look at the Test category and subclass