views:

70

answers:

3

i have two classes class a and class b

import class b;
class a{
  nsmutablearray *a=@"--some objects----";
}


class b{

    something
}

i want to use that array in class b so that i can display it on a table in class b can anybodt tell how can i do it?

A: 

If B holds a reference to A, simple declare a property in A.

@interface A : NSObject {
  NSMutableArray *theArray;
}
@property (nonatomic, retain) NSMutableArray* theArray;
@end

@implementation A
@synthesize theArray;
...
@end

if the reference to A hold by B is a :

-(void)anyMethodInB {
  NSMutableArray* dataSource = a.theArray;
}

Edit: You can of course inverse the logic if A holds a reference to B.

VdesmedT
sorry i cant understand ,i had made reference to class b in class a but u r saying to make reference of class a in class b?can we make reference to each other
pankaj kainthla
+1  A: 

To have a class whose instances can hold arrays of other objects, give the class an NSArray. You will have to fill it using one of the NSArray methods.

There is no templating in Objective-C, so you can't specify what the NSArray should hold, except by proper encapsulation, documentation, and naming of variables, methods, etc.

Holder.h:

@interface Holder : NSObject {
    NSArray *someRandomObjects;
}

RandomObject.h:

@interface RandomObject : NSObject {
}
Chris Cooper
+1  A: 

create a function in class B

-(void)initByArray:(NsMutableArray*)preArray {

LocalArray=preArray;

}

and call this function when create the variable for Class B in class A like

ClassB* classvariable=[[ClassB alloc]initWithNibName:@"ClassBXib" bundle:nil];

[classvariable initByArray:a];

[self.navigationController pushViewController:classvariable animated:YES];

Remb to declare Function in Class B

Ashish Mathur
thanks it works
pankaj kainthla