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"
Related
I want to design a solution, but facing a problem because of limited knowledge of OOPs concept. My requirement is as follows
Assume I have 4 classes
Class A
Class B
Class C
Class D
I want object of class D can only be allowed to create, or class can only be accessible in Class C. Same Class C object and class itself can be allowed access and created in Class B. Class D should not have access to class B.
Same Class A should not have access to C, D. It only have access to B, and it can create object of B.
How we can implement with best possible architecture/design pattern in C#.
With Best Regards.
Take a look at this question: Ensure that only class A can call class B
After you have read that, you will know you have to use a combination of package and private classes.
The only solution of your problem can be solved by the use of private inner classes.
make class D class inner to class C and class C inner to class B and so on.
but remember the use of inner classes is highly deprecated.
implementing this in C#, I leave up to you.
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.
If I have three classes:
ClassA
ClassB
ClassC
They all have import statements to import each other i.e. ClassC imports ClassB and ClassB imports ClassA only.
Given this design, does ClassC have access to ClassA automatically?
First: in any case import/include doesn't make one class to have access to another class, it just makes class A to know about class B.
Second: visibility is determined by where you imported headers.
If you did import ClassA header in the ClassB header and then imported ClassB header in ClassC header than yes, ClassC knows about ClassA. But if imported it in an implementation file then the answer is no.
Edit: should notice that it's better to make class forward declarations in header and then include appropriate one in an implementation file.
hi i am new to iphone. what i am doing is creating two classes named as classone,classtwo. now what i need is i have to call calsstwo from classone. I already import classtwo in classone. pls post some code thank u.
Init an object of type Classtwo in Classone and send it messages from there. You should probably head for some basic Objective-C stuff first: http://www.otierney.net/objective-c.html
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.