custom class init issue - ios

Hi I've made my own custom class:
with this init method:
-(id)init:(NSString*) strFilename withImageData:(NSData*) objImageData withUploadTaskDelegate:(id<NSURLSessionTaskDelegate>) objUploadTaskDelegate
{
filename = strFilename;
uploadTaskDelegate = objUploadTaskDelegate;
imageData = objImageData;
return self;
}
I've made sure that the .h file also have the definition of this init file:
-(id)init:(NSString*) strFilename withImageData:(NSData*) objImageData withUploadTaskDelegate:(id<NSURLSessionTaskDelegate>) objUploadTaskDelegate;
however when I try to use it like this:
UploadQueueCellData *objTest =[UploadQueueCellData init:strImageName withImageData:objData withUploadTaskDelegate:objUploadDelegate];
it gives this error:
No known class method for selector
'init:withImageData:withUploadTaskDelegate:'
this is my 3th day trying to develop in objective c code so I'm sure I'm missing something but I do not seem to be able to spot what I'm doing wrong ?

Your init method is an instance method, not the class method, check below example:
For instance method, you've to create an instance before using it.
#interface MyClass : NSObject
+ (void)aClassMethod;
- (void)anInstanceMethod;
#end
To call a method of MyClass, for instance method:
[[MyClass alloc] anInstanceMethod]
For Class method:
[MyClass aClassMethod]
In your case do this:
[[UploadQueueCellData alloc] init:strImageName withImageData:objData withUploadTaskDelegate:objUploadDelegate]
Class Method
A class method is applied to the class as a whole. If you check the alloc method, that's a class method denoted by the + before the method declaration. It's a class method because it is applied to the class to make a specific instance of that class.
Instance Method
You use an instance method to modify a specific instance of a class that is unique to that instance, rather than to the class as a whole. init for example (denoted with a - before the method declaration), is an instance method because you are normally modifying the properties of that class after it has been created with alloc.

Related

instance methods from class methods swift

I have a class TestClass and a class & instance method inside it
class TestClass {
class func classMethod(){
print("how do i call instance method from here")
}
func instanceMethod(){
print("call this instance method")
}
}
My question is how do i call that instanceMethod from classMethod??
One way i have noticed is
class func classMethod(){
TestClass().instanceMethod()
}
But,Is this the good way to do?
What you are trying to do rarely makes any sense from a design perspective.
By definition, an instance method operates on an instance of an object.
For example, it might require access to some instance members, or somehow meddle with the state of the object you call the method on.
class methods on the other hand do not require an instance to be able to call them - and should in general only operate on the given parameters, not be dependant on shared state.
If you need to call instanceMethod() in classMethod(), and instanceMethod() does not require any state - why is it not also a class method, or a (global) pure function?
You can pass the instance object as a parameter to the class method, and then call the instance method of the object:
class TestClass {
class func classMethod(obj:TestClass){
print("how do i call instance method from here")
obj.instanceMethod()
}
func instanceMethod(){
print("call this instance method")
}
}
To call the instance method, you need an instance of TestClass. That's what TestClass() is getting you when you call TestClass().instanceMethod().
If you want to call it from a specific instance, you could pass it in as a parameter to the class function: class func classMethodUsingInstance(instance: TestClass)
If you don't need a specific instance for instanceMethod(), maybe consider making it a class method as well.

What is need of Class type method?

I want to know what is need of class type method. Whenever this question is asked people compare between Class type method and Instance method. when we should use Class type method and what is benefit of Class type method over Instance methods ?
It will be great if anybody answer me. Thanks in advance.
Here is the difference between class and instance method.
Class Method
You can use class method for your common functionality like validation,color from hex color,etc.. and used anywhere in project without creating instance of that class (called like utility class).
Class method indirectly said static method.
No need to create object of class for calling this type of method.
Class method starts with + in objective-c and class func in swift.
stringWithFormat is a class method of NSString class you can call directly using class name (not required to create object of NSString).
int no = 5;
NSString *str = [NSString stringWithFormat:#"Some String %d",no];
Instance Method
You create instance method for your specific functionality like setupView,etc.. and you have to create instance for calling it.
Instance method is a simple method within specific class.
Need to create object of class for calling this type of method.
Instance method starts with - in objective-c and func in swift.
isEqualToString is an instance method of NSString class you can call only using with NSString class object.
NSString *str = #"some string";
NSString *str2 = #"some string";
Bool isEqual = [str isEqualToString:str2];

Replace method implementation in Swift

I'm trying to replace method implementation with parent implementation:
// Get superclass's valueDisplayText()
// XLFormSelectorCell is a parent of HCFormSelectorCell
// HCFormSelectorCell - swift class
// XLFormSelectorCell - obj-c class
let impParent = class_getMethodImplementation(XLFormSelectorCell.self, Selector("valueDisplayText"))
let method = class_getInstanceMethod(HCFormSelectorCell.self, Selector("valueDisplayText"))
method_setImplementation(method, impParent)
class_replaceMethod(HCFormSelectorCell.self, Selector("valueDisplayText"), impParent, method_getTypeEncoding(method))
But instead in replaces parent implementation with subclass implementation.
Why and how to achieve desired?
P.S: The reason I'm doing this: I need to call parent class's hidden method (obj-c class). Any other approaches to do this?

How to explicitely pass self as a super class to a method and being called back instead of subclass?

I have 3 classes
One base class : MyBaseClass
That implements a method :
- (void) aSuperMethod {
[[UtilClass sharedUtil] doThisAndSendAnswerToMe:self]
}
- (void) answerReader:(bla bla)someParams {}
One subclass of MyBaseClass : MySubClass
That also implements these kind of methods :
- (void) anotherMethod {
[[UtilClass sharedUtil] doThisAndSendAnswerToMe:self]
[self aSuperMethod];
}
- (void) answerReader:(bla bla)someParams {}
And of course a utility class : UtilClass
That implements this kind of method :
- (void) doThisAndSendAnswerToMe:(id)listener {
do some stuff
[listener answerReader:someAnswerParams];
}
In debug mode, of course, the listener received in both calls by the doThisAndSendAnswerToMe method is of MySubClass class.
How may I do to point either on MySubClass or MyBaseClass depending on the call origin ?
You can't. The subclass's method implementation always overrides the superclass implementation.
One solution is to use a different method name in the superclass and the subclass, and pass the name of the callback method to UtilClass. UtilClass then uses -[NSObject performSelector:withObject:] to call that method.
-(void)baseClassAnswerReader:(id)someAnswerParams { ... }
[[UtilClass sharedUtil] doSomethingAndSendAnswerToMe:self
selector:#selector(baseClassAnswerReader:)];
Another solution is to pass a completion block object to UtilClass. UtilClass then calls the block object when it is done.
[[UtilClass sharedUtil] doSomethingWithCompletionBlock:^(id someAnswerParams){ ... }];
You use an instance of the base class if you want to use the base class, and you use an instance of the subclass if you want to use the subclass.
If you don't want the subclass method to override the superclass one, then make it a different method instead of overriding.

"Invalid use of 'this' in non-member function" in objective-c context?

Using Xcode.
In this code (func is declared in interface), tells subj error, standing on string with 'self'.
+ (void) run: (Action) action after: (int) seconds
{
[self run:action after:seconds repeat:NO];
}
What the... ?
self is an instance variable used to refer to an instance of the current object.
You are attempting to use it in a class level method +(void)... where self has no meaning. Try using a shared instance, or passing an instance of the class in question to the method.
+ (void) run:(Action)action on:(MyClass*) instance after:(int) seconds
{
[instance run:action after:seconds repeat:NO];
}
EDIT
My commenters have pointed out that self does have meaning in class level contexts, but it refers to the class itself. That would mean you were trying to call a method that looks like this:
[MyClass run:action after:seconds repeat:NO];
Where you should be aiming for:
[myClassInstance run:action after:seconds repeat:NO];

Resources