How to call a method from another method in Objective C? - objective-c

Can someone answer me how to call one method into another in Objective C on Xcode

The basic syntax for calling a method on an object is this:
[object method];
[object methodWithInput:input];
If methods returns value:
output = [object methodWithOutput];
output = [object methodWithInputAndOutput:input];
More Detail
EDIT:
Here is a good example that how to call method from other class:
OBJECTIVE C - Objective-C call method on another class?
Example:
SomeClass* object = [[SomeClass alloc] init]; // Create an instance of SomeClass
[object someMethod]; // Send the someMethod message

You get a pointer to an object that implements the other method and send the appropriate message (e.g. [otherObject doSomething]).

For example:
#implementation view1
(void)someMethod
{
......code of method...
}
#implementation view2
(void)fistMethod
{
view1 *abc = [[view1 alloc]init];
[abc someMethod];
[abc release];
}
I hope you got it.

If you have 2 functions inside class(.m file):
-(void) func1{ }
-(void) func2{ }
If you want to call func2 from func1, you cannot just call func2();
instead just include self
That is:
-(void) func1{
[self:func2];
}

Related

Objective-C: Self-variable understanding Issues

I want to know some features about self.
Which context have self variable in class method?
Why self.self allowed in init method?
First:
We have a class ExampleClass
#interface ExampleClass:NSObject
#property (nonatomic,strong) NSString* a;
+(ExampleClass*)createExampleClass;
#end
#implementation ExampleClass
-(id)init{
self = [super init];
if(self){
[self.self init]; #Allowed
[self init]; #Not Allowed ?
}
}
+(ExampleClass*)createExampleClass{
/*do work here*/
NSLog(#"Self: %# \n Class: %#",self,[self class]);
}
#end
In this example we will see something like this:
Self: ExampleClass
Class: ExampleClass
But why?!
And in init method [self.self init] allowed, but not allowed '[self init]'.
Why does this happen?
In a class method, self is the class. For classes [self class] simply returns self, so self and [self class] are basically the same thing there.
Calling [self init] in the init method doesn't make any sense, it would cause an infinite recursion. However, the compiler error you get is a restriction of ARC, if you'd use self = [self init], the error would go away, but it would still make no sense. You might do this in a different initializer method though, to call the designated initializer.
self.self is short for [self self] which does nothing but return self.
self in class method is the class object itself.
NSObject has self method which returns itself.
    See here:  https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Protocols/NSObject_Protocol/Reference/NSObject.html#//apple_ref/occ/intfm/NSObject/self
Self: ExampleClass // Name of the class.
Class: ExampleClass // Name of the meta-class object which is same with class object.
If you print pointer address, you will see two objects are different.
Here's nice illustration and description.
http://www.sealiesoftware.com/blog/archive/2009/04/14/objc_explain_Classes_and_metaclasses.html
self.self.self.self.self.self is also valid :) or [[self self].self self].self.self

objective c help - calling methods?

Hey,
I am new to programming I wanted to know why is it always [self method];? I mean why is it that way could someone explain me why is it self and what is going on in the background? sorry if it is a stupid question
thanks,
TC
Basically, what self refers to is the object that you're currently in the context of. [self somemethod] means that you're invoking a method named somemethod in the class that self was initialized as.
For example, if you were to do something like this:
Foo *f = [[Foo alloc]init];
[f someMethod];
You'd be invoking someMethod on the Foo instance.
But if you're working inside of the class Foo, self serves as an explicit reference to the current object. In this case, you'd simply use [self someMethod] to invoke someMethod.
-(id) init {
if (self = [super init]) {
[self someMethod];
}
...
}
-(void) someMethod { }
Does that help?
[self method] calls the method of the calling class. For example, in the header file of your class,
#interface YourClass : NSObject {
}
- (void) myMethod;
then, you can call the 'myMethod' in YourClass by using [self myMethod]. Does it make sense?
During calling [self method], there is no any background working. [self method] is almost same the calling function in C. When you use [self method], 'method' in your class is just called right away.
Because [self method]; calls the -method method in the class from which it is called.
If you want kill John in ObjC:
[john sendBullet]
if you do sendBullet to myself (shortly self), it's a suicide
[self sendBullet]
got the difference? :)

How to implement a myclassWith... method in Objective-C?

What's the best way to implement a method that returns an autoreleased object? Does the following code work correctly?
#implementation MyClass
-(void) myclassWithSomeParameter:(SomeType) parameter
{
return [[MyClass allocWithSomeParameter:parameter] autorelease];
}
The return type must be MyClass *, you need to alloc the new instance, and it should be a class method rather than an instance method (otherwise you need an existing instance of MyClass).
+ (MyClass *)myClassWithSomeParameter:(SomeType)parameter {
return [[[MyClass alloc] initWithSomeParameter:parameter] autorelease];
}
Then create instances like so:
MyClass *instance = [MyClass myClassWithSomeParameter:parameter];

Object only initialisable through factory method (Objective-c)

I am trying to create an object only instantiatable through a factory method. I prevented init being used by throwing an exception (see Creating a class with no init method). However this meant that I had to create a new method secretInit to use in the factory method.
//Factory method
- (URLReqs *)buildURLReq:(NSString *)location
{
URLReqs *tmp=[[URLReqs alloc] secretInit];
tmp.str=location
return tmp;
}
- (id) secretInit{
return [super init];
}
This method is messy and while we can avoid declaring secretInit in the header file, someone could still access it. Is there a nicer solution?
One idea is to try calling init on the super object of URLReqs directly rather than creating a function to do it.
You don't want to do it this way, but it is possible:
#include <objc/message.h>
#implementation MyClass
+ (id) factoryMethodWithParameter:(NSString *) someString;
{
struct objc_super instanceSuper;
id myInstance = [self alloc];
instanceSuper.receiver = myInstance;
instanceSuper.super_class = [self superclass];
// instanceSuper.class = [self superclass]; // use this line if the above doesn't compile
myInstance = objc_msgSendSuper (&instanceSuper, #selector(init));
//continue on setting up myInstance's ivars . . .
[myInstance setVariable:someString];
return myInstance;
}
- (id) init
{
self = [super init];
if (!self) return nil;
[self release];
[self doesNotRecogniseSelector:_cmd];
return nil;
}
#end
This will allow you to continue to send init messages to myInstance's “super” object, but won't allow anyone to send init to MyClass instances.
Be careful though, although this is using standard runtime functions declared in objc/*.h, don't expect Apple to keep this runtime API consistent.
The solution is to use a category on your object to hide the secret initialization method.
Private Methods through categories
What goal are you trying to accomplish? There might be a better solution than this.
It only exposes it if it's referenced in the header file.
No, wait. I should state that another way. There's no better way to hide it than to not declare it in the header file. Private methods don't exist in Objective-C.

How to obtain the address of object in objective-C

I have to call an objective C method from a cpp Function.
I have a class C, whose object address is required in this function. I did come across another link which guided me on how to have a reference to the class C, and use it for invocation from the cpp function.
In my case, there is one small difference in that the Class C is already instantiated, and I would not want to allocate an object again. So how can I get its object address?
The code looks like this:
C.h
import Cocoa/Cocoa.h
id refToC
#interface C: NSObject
{
;
somemethod;
;
}
#end
C.m
#implementation C
- (void) somemethod
{
;
;
}
#end
B.mm
import C.h
void func()
{
//I need the address of object here, so as to invoke:
[refToC somemethod];
}
Thanks in Advance
~ps7
The id type is already a pointer to an object. Once you have created a valid object, e.g.:
refToC = [[C alloc] init]
The easiest way is to make use of the singleton design pattern. Here's a common way to make use of that pattern in Objective-C:
Widget.h
#interface Widget : NSObject {
// ...
}
// ...
- (void)someMethod;
+ (Widget *)sharedWidget;
#end
Widget.m
#implementation Widget
// ...
+ (Widget *)sharedWidget {
static Widget *instance;
#synchronized (self) {
if (!instance)
instance = [[Widget alloc] init];
}
return instance;
}
#end
CppWrapper.mm
void methodWrapper() {
[[Widget sharedWidget] someMethod];
}
Thanks a lot for your pointers. I had missed telling that the class C is a controller class. I tried assigning refToC to self in awakeFromNib, and the invocation in func() worked like a charm.
Thanks Matt and John for your pointers.
~ ps7