-1

i have 2 classes i.e one is "MainClass" and another is "StartView" Class .i want to create array of objects of MainClass into StartView Class.

here i created 10 objects of MainClass and added into one Muttable Array . That Array is also part of MainClass.Its going difficult to access them. Is there any way to create array of objects directly .I want to know how to access and how to create array of objects in another class. Code Is below ...

  //MainClass.h 
 #import <Foundation/Foundation.h>
 #import "StartView.h"
 #import "TableViewController.h"
 @interface MainClass : NSObject

  @property(nonatomic, strong) NSString *que;
  @property(nonatomic, strong) NSString *img;
  @property(nonatomic, strong) NSArray *option;
  @property(nonatomic, strong) NSMutableArray *nms;
  -(void)ObjectsAssignment;

 @end

 //MainClass.m
  -(void)ObjectsAssignment
   {
       nms=[[NSMutableArray alloc]init];

       MainClass *mc1=[[MainClass alloc]init];
       mc1.que=@"Who invented Microprocessor ?";
       mc1.img=@"SuperComputer.jpg";
       mc1.option=[[NSArray alloc]initWithObjects:@"Joseph  
       Jacquard",@"Herman H 
       Goldstein",@"Marcian E Huff",@"George Boole",nil];
       [nms addObject:mc1];


     MainClass *mc2=[[MainClass alloc]init];
     mc2.que=@".INI extention refers to which kind of file ? ";
     mc2.img=@"SuperComputer.jpg";
     mc2.option=[[NSArray alloc]initWithObjects:@"Joseph Jacquard",
     @"Herman H Goldstein",@"Marcian E Huff",@"George Boole",nil];
     [nms addObject:mc2];
 }
 @end
  • Please note this has nothing to do with the `xcode IDE` so please don't use that tag. – Popeye Feb 10 '15 at 13:18
  • possible duplicate of [Passing Data between View Controllers](http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – Hot Licks Feb 10 '15 at 13:30

1 Answers1

0

Create a property in StartView which you can assign the array to. StartView and your other code then share pointers to the same array and both can access it.

In StartView:

@property NSMutableArray *myArrayReference;

Elsewhere

NSMutableArray *theArray=[[NSMutableArray alloc] init];
for (int i=0;i<10;i++){
   MainClass *instance=[[MainClass alloc] init];
   [theArray addObject:instance]
}

// Pass this array to other object.
StartView *startView=[[StartView alloc] init];
[startView setMyArrayReference:theArray];
Rory McKinnel
  • 7,936
  • 2
  • 17
  • 28