2

I have three Classes A,B,C. In Class A the code is like this,

B* test=[[C alloc]init];
test.empID= RollNo; //(for eg value is 10)
test.empName=Name; // (for eg name is Cyril)

I can print the values in the Class B, but now i want to inherit that value to the class C, where the Class C is like this,

in header file
@interface C: UITableViewCell

I know that , objective C doesn't support multiple inheritance, When I import the B class in class C and tried to print it, it showing as null. All i need is to print the values from Class B in Class C. help me

Cyril
  • 1,216
  • 3
  • 19
  • 40

2 Answers2

0

What about explicitly storing a reference to the B class in your C class like this:

@interface C: UITableViewCell {
    B *bObject;
}

Then in your C class, you can reference like so:

bObject.empID = RollNo

etc.

danielbeard
  • 9,120
  • 3
  • 44
  • 58
  • I tried that , created a object for Class B in class C, i tried to print it as NSLog(@"%@ , %@ ",bObject.empID,bObject.name); It is printing as Null – Cyril Jan 30 '12 at 04:48
  • Even i declared a static variable in Class A , i tried to print that value in the class C , it is also showing Null – Cyril Jan 30 '12 at 04:51
  • 1
    You need to make sure that the object is being allocated correctly and the reference is being retained properly. Set up some break points, if they show the address to the B object as 0x0, it means it's either not initialised properly, or being released somewhere. – danielbeard Jan 30 '12 at 04:53
  • The object is released before the class C, how to copy that object to the class C. any idea ? – Cyril Jan 30 '12 at 05:55
  • are you assigning any value to class B object in class c? – Parag Bafna Jan 30 '12 at 06:10
  • No, i am not assigning. I want to display the values of B – Cyril Jan 30 '12 at 06:18
  • In your C class include an initWithClassB that takes a B object as a parameter and stores the instance variables with that data. – danielbeard Jan 30 '12 at 06:32
0

Objective c does not support class variables. but you can use static variable for you requirement.

// A.h
@interface A : NSObject {
}
+ (B*)test;
+ (void)setTest:(B*)object;
@end

// A.m
#import "MyClass.h"

static B* test;

@implementation MyClass

+ (B*)test {
    return test;
}

+ (void)setStr:(B*)object {
    if (test != object) {
        [test release];
        test = [object copy];
    }
}
@end

Declare/Define an static variable within the classA.m so it will be only accessible for the classA methods (and everything you put inside classA.m).

Take a look at this post

Community
  • 1
  • 1
Parag Bafna
  • 22,812
  • 8
  • 71
  • 144
  • The project is already developed, if I want to change some variables, then that could result in some serious problems. So i wish to see how the object can be retained or copied. – Cyril Jan 30 '12 at 09:23
  • How to print the value from NSMutable Array ?? – Cyril Jan 30 '12 at 09:26
  • 1
    NSLog(@"%@", Array); or NSLog(@"%@", [Array objectAtIndex:i]), i is integer. – Parag Bafna Jan 30 '12 at 09:34