I know that the convention is to do any kind of header in your .m. However, what if I need to import something so that I can have access to the protocol? Say I have a class Test.h/m and I have a protocol called TestProtocol that another class say, MyTest.h wants to implement. What I usually do in this case is to import Test.h in MyTest.h class file. I find this a bit distracting, is there a way to avoid this? What are some cases when this is fine?
You can put the protocol in it's own TestProtocol.h and import that in MyTest.h.
This is the way I do it for delegate protocols etc.
You can define the TestProtocol protocol in a separate TestProtocol.h file that you import wherever you want, or you can use a forward protocol declaration
#protocol TestProtocol;
There is no rule that says a .h file has to have a corresponding .m file.
So you could declare your protocol in TestProtocol.h and import that wherever it's needed.
Related
Is it possible to import just one class from a Swift module? I used
import struct Foundation.Date
but now, NSString and other Foundation classes/structs are available too, at the top level!
All I really want is the Date class, to avoid polluting the namespace.
This answer is originally made for objective-c but the behaviour is the same: Why does a simple program import <Foundation/Foundation.h> instead of individual header files?
You don't have to worry about that, I think your compiler do this job perfectly ! :)
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
#class vs. #import
I am Really confused with this,Whats the difference between writing #classname & #import"classname.h" .When Do we go for #classname?
You can find a good answer here and in the Objective-C Programming Language documentation on ADC
#classname is a forward declaration. Nothing gets imported, it just informes the compiler, that the class will exist on runtime.
#import will actually import the other class -> you can imagine it as a copy it into the file. so the imported classes will get compiled before the one, that it is written in.
The #classname just let's the compiler know that the class exists however you'll still need to import the corresponding .h. You can use the #classname in the .h and then import the class in the .m and this will allow you to avoid circular references. You can read more about it here Apple Docs
#className directive is introduced to overcome cyclic reference of classes, it is also known as Forward Declaration
#class will just inform the compiler that there is a class named "#className yourClass" no need to worry about that class and in runtime it just refer that class and executes.
#import "className" will keep a copy of that particular class.
#classname just tells the compiler that the class classname exists.
#import really imports the header file so that the compiler not only knows that it exists, but also how it looks. (like ivars, methods etc...)
Like:
#interface ClassXXName(private)
- (void) xxxfunctions
#end
or user category methods?
#interface Foo() creates a class extension (I stand corrected, props to bbum) on interface Foo which is like additional methods added to the interface. Some people also use #interafce Foo(Private) (category) instead of a class extension with (). It's more like "injecting" new methods into a class from outside the class.
Placing this in the .m file just keeps other things from "seeing it" in the .h file, but that's it. Basically people normally use categories or class extensions in .m files to specify private interfaces, but they are also used for things like UIKit uses categories to add row and section public methods to NSIndexPath. (This can be confusing.)
You don't really need to define private methods this way, but if you have a method called bar that calls method foo before foo is defined in the source file you'll get a compiler warning something like "object self may not respond to foo". You can get rid of that by defining foo before you define bar or any other foo-calling code. It's the same with plain C and functions.
Like Ole says this doesn't stop anyone from calling the private methods, it just declares your intention that they be private and causes the compiler to generate the "may not respond to" warnings even if they import the .h file.
I have a class A with textField1, class B with textField2.
i want to use textField1 and textField2 in class C. What i have to import or what i have to do. I think i am straggling with importing files. i.e. #include or something
Thanks for reply in advance.
Objective does not support multiple inheritance.
You can only inherit from one class, you could use a protocol to make the class match class B.
Yes, you have to import both class A and class B in class C, and then access textField1 and textField2. Import syntax is #import "classA.h"
I have built a class which has a few methods in it, once of which returns an array, lets call this class A.
I have a second class, class B, which I would like to use to call the method from class A.
But, now how do I call that method from class A and store what is returned in a var in class B? Do I have to initiate the class? I have made sure to include the .h file from class A into class B.
Thanks for helping a newbie.
UPDATE:
Here is how I thought I could do this (DataStore is my class A and pushRideData is my method that returns an array):
DataStore *store = [[DataStore alloc] init];
trailsArray = [store pushRideData];
Assuming you have files A.h, A.m B.h and B.m to define your two classes, then you need to do the following:
Make sure A.h and B.h are #imported into your projects PCH file (this is the easiest/fastest way, but you could also choose to import the files into all the .m files, instead).
If you refer to a class -- say, B *something in A.h -- before that class's header file is imported, then use a forward class declaration to shut up the compiler. I.e. #class B; before the #interface A:NSObject in A.h
If you want to call an instance method of a class, you need to instantiate the class as you describe. Or, if the instance is created somewhere else, you'll need some mechanism to retrieve it. A class method, perhaps, or a global variable or a controller or something like it.
None of this is really that much different than straight C save for a formal notion of Objects (as opposed to malloc'ing a bunch of memory and passing around pointers).
That's a fairly abstract question. Yes, you need an instance to be able to store instance variables in it. You will need to allocate and init the instance, assinging it to an instance or local (pointer) variable in the calling class unless it is one of the several in the Cocoa Touch frameworks which use the singleton pattern, such as the application delegate. Such singletons have special case-specific class methods for obtaining the singleton instance.