How Can I Call A Method Written In Class1 To Class2?

- 1 answer

I have class1.m . I declared a method and written in it. Now i need to call it into another class. How can I make it? Can we use extern for it like we use for variables. Thank you.



You should separate your declaration and definition, and place the declaration for class1 in class1.h. Then, you should include class1.h using #import "class1.h" in your source file for class2. Within class2, you can instantiate and use class1 as follows:

class1* instance_of_class1 = [[class1 alloc] init];
[class1 invokeMyMethod];

When you are done using your instance, be sure to decrement the reference count via release as in:

[instance_of_class1 release];
instance_of_class1 = nil;