When to use a Class having both static methods and instance methods in IPhone - 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.

Related

Swift: class func .... why use this instead of func when creating a method inside a class?

I'm new to coding, apologies for dumb question.
Am following a tutorial to build a note taking app using Swift in Xcode.
Within a class definition I have been defining methods using the keyword func myMethod etc. At one point the instructor decides to define a Class method (within the existing class) using class func myMethod.
Why would you do this?
Thanks in advance for any feedback.
By defining a class method it means that you don't need an instance of that class to use the method. So instead of:
var myInstance: MyClass = MyClass()
myInstance.myMethod()
You can simply use:
MyClass.myMethod()
The static (class) function is callable without needing an instance of the class available; it may be called without having to instantiate an object.
This can be useful for encapsulation (avoiding placing the function in the global namespace), or for operations that apply to all objects of a given class, such as tracking the total number of objects currently instantiated.
Static functions can be used to define a namespaces collection of related utility functions:
aDate = Utils.getDate()
aTime = Utils.getTime()
Another common use is for the singleton pattern, where a static function is used to provide access to an object that is limited to being instantiate only once:
obj = MySingleton.getInstance()
obj.whatever()
One answer is namespacing. If a function is only relevant to a certain class there is no need to declare the function globally.
This is Swift's take on static methods:
Static methods are meant to be relevant to all the instances of a class (or no instances) rather than to any specific instance.
An example of these are the animation functions in UIView, or the canSendMail function from MFMailComposeViewController.

Different between calling to function by delegate or by instance

What are the different between calling to a function by delegate or by creating an instance and calling tomethod:
for example, I can use the next method
ViewC1.m
-(void) doSomthing{
}
viewC2.m
viewC1 *myInterface = [[viewC1 alloc] init];
[myInterface doSomthing];
or creating delegate with protocol... and use it like this:
viewC2.m
[self.delegate doSomthing];
It's primarily a question of the design pattern that you're trying to achieve. Delegation allows you to create less explicit linkages between classes. From Apple's The Objective-C Programming Language:
"Protocols free method declarations from dependency on the class
hierarchy, so they can be used in ways that classes and categories
cannot. Protocols list methods that are (or may be) implemented
somewhere, but the identity of the class that implements them is not
of interest."
So, to answer your question, there's no difference on an outcome basis, so long as self.delegate exists and implements doSomthing and viewC1 instances implement doSomthing. But without knowing the broader context, it's hard to say much more about the relative advantages. I'd read up on Objective-C protocols and about the delegate pattern as heavily used in the Cocoa frameworks to get a better feel for when to use this pattern. Follow Apple's lead in this case.
(By the way, in Objective-C, it's "methods" instead of "functions"...)
Assume there are two class ViewC1 and ViewC2. If you want to send message from ViewC1 to ViewC2 and receive the callback message from ViewC2 to ViewC1. It is better using protocol for this objects communication. If you using instance method for this objects communication, you will restricted ViewC2 to callback on specified object (In this case ViewC1). In order to let ViewC2 able to callback any objects. You can define the protocol on ViewC2. And let other object which want to receive the callback to conform it.

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.

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

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.

SenTest, Objective-C, Category Weirdness

I have a SenTest class I am attempting to write which uses a Category to override a method in a controller class. I want my mock controller class to call a method in the SenTest class to determine how to behave for various tests.
For example:
Set a var in the testFoo method.
In testFoo, call my mock controller's overridden method.
Have it call back up to the SenTest singleton to figure out how to behave for testFoo.
Is this possible, or am I an idiot?
Turns out the trick to doing this is to to make the associated reference IN the category code, not in the SenTest case.
So for example, declare this in category:
-(void)fakeMethodToSetTestData:(NSDictionary *)data;
And in that method set the associative reference to store your data "associated" with "self".
Call this in your test case on your object, passing in your data.
Then in your test case, call your:
-(void)realMethodInCategoryWhichIsBeingOverridden;
And in your category implementation of this method, retrieve the data from the associative reference you set in the first method.
This will let you set test data on a Controller so that you can hijack the delegation chain, for example, to prevent the second method from going to the network and returning your test data into your controller's code.