Hot to create array of objects of class into another class - ios

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

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];

Related

How come the instance of my object is missing the properties I want to assign?

I am trying to create a class in with properties defined in the header. Was planning to populate the object in a for loop. Here is the header, what could I be doing wrong? Am I defining this class wrong?
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#interface ViewController : UIViewController<UIPickerViewDataSource,
...
#end
#interface PickerObject : NSObject
{
NSString *minutes;
NSNumber *label;
}
#end
In the viewDidLoad I get message saying property minutes not found.
NSMutableArray *arr = [[NSMutableArray alloc] init];
for(int x = 0; x < total; x++)
{
PickerObject *myObject = [[PickerObject alloc] init];
myObject.minutes = x;
myObject.label = #"%# minutes",x;
[_pickerData addObject:myObject];
//[_pickerData addObject:[NSNumber numberWithInt:x]];
[arr addObject:[NSNumber numberWithInt:x]];
}
Here is a screenshot of the error depicting property not found
To access object variables using the dot '.' operator in Objective-C, you need a setter method. The easiest way to achieve this is by defining a property instead, like this:
#interface PickerObject : NSObject
{
}
#property (nonatomic, strong) NSString *minutes;
#property (nonatomic, strong) NSNumber *label;
#end
The object variables, setter- and getter methods you need will be synthesised automatically.

How to declare array in object and store with encode

I am creating a class Ticket. In that ticket I want a mutable array of NSStrings
i.e. in ticket.h
#interface Ticket : NSObject
#property NSString *ticketName;
#property NSMutableArray *games;
However Objective C doesn't allow me to do this. What am I supposed to do to have an array inside an object?
I then want to store that array using encodeWithCoder in the implementation of the object
like i said you might have a syntax problem, there is no reason why Objective-c won't allow you to add an NSMutableArray into your custom objects, try this:
#interface MyObject : NSObject
#property (nonatomic, strong) NSMutableArray *myMutableArray;
//
// .. other properties
//
#end
and in the implementation
#implementation MyObject
- (void)viewDidLoad
{
[super viewDidLoad];
[self.myMutableArray addObject:#"myString1"];
[self.myMutableArray addObject:#"myString2"];
[self.myMutableArray addObject:#"myString3"];
}

Create an array that hold custom class object

I have class, that have several properties, it look like this:
#interface PlaceHolder : NSObject
#property (strong, nonatomic) NSString *name;
#property (strong, nonatomic) NSString *description;
#property (strong, nonatomic) NSString *webPage;
#property (strong, nonatomic) NSNumber *latitude;
What i need is, to create an array, that hold objects of that class. Obvious, properties will not be nil and will be different each time. So, that array must have several hundreds of PlaceHolder object, and it should be possible to get information for any of that object and it properties. But, when i try to create that array, in NSLog i see that it contain only (NULL) objects. This is how i try too add object to array:
In header i wrote:
#property (strong, nonatomic) PlaceHolder *place;
Then:
self.place = [[PlaceHolder alloc]init];
self.place.name = nameString;
NSLog(#"%# name???", self.place.name);
[self.placeObjectsArray addObject:self.place];
self.place.name is not nil, and still, array is empty. Well, its not true, it not empty but, it only contains (null) objects. How to fill array with objects of my class?
Any advice would be appreciated, thanks!
UPDATED:
I init array like this -
-(id)initWithDelegate:(id)delegateObj{
...
self.placeObjectsArray = [NSMutableArray array];
...
return self;
}
UPDATED: Now, when i try to init-alloc array in same method (instead of setting #property and strong relation) i can see it in NSLog. I wonder why it won't happen when i use my array, that set as property..
You need to alloc-init your Mutable Array ;
NSMutableArray *myArray = [[NSMutableArray alloc]initWithObjects:self.place,nil];
or simply
NSMutableArray *myArray = [[NSMutableArray alloc]init];
Then you could add objects with a for loop or whatever you need, using the following :
for ( YOURINSTRUCTION )
{
[myArray addObject:YOUROBJECT]
}
I recommend to lazy instantiate the array, that way it will only get instantiated when really needed. Since you are setting the array as a property, you can override the getter method for it like this:
- (NSMutableArray *)placeObjectsArray
{
if (!_placeObjectsArray) _placeObjectsArray = [[NSMutableArray alloc] init];
return _placeObjectsArray;
}
With this, you can call [self.placeObjectsArray addObject:self.place] anywhere in your code and the array will always be initialized when needed.

Why do objects not get added to my array?

I have two custom classes: FSGame and FSEvent. FSGame has an ivar that should be an NSMutableArray of FSEvent objects.
Here's FSGame.h:
#interface FSGame : NSObject
#property (strong, nonatomic) NSMutableArray *players;
#property (strong, nonatomic) NSString *startTime;
#property (strong, nonatomic) NSString *endTime;
#property (strong, nonatomic) NSMutableArray *gameEvents;
#end
And here's my FSEvent.h:
#interface FSEvent : NSObject
#property NSInteger ID;
#property NSInteger pointTo;
#end
I use
#property (strong, nonatomic) FSGame *game;
to keep an instance of FSGame in my AppDelegate. Then, in my application:didFinishLaunchingWithOptions: I create an instance of FSGame so it can be filled throughout the "game".
_game = [[FSGame alloc] init];
Then, in one of my view controllers, I alloc and init an instance of FSEvent and then attempt to add it to the .gameEvents array:
[appDelegate.game.gameEvents addObject: event];
But something seems to go wrong here because if, from another view controller, I try to get access to that object, it will be nil:
FSEvent *previousEvent = [appDelegate.game.gameEvents lastObject];
if (previousEvent == nil) {
NSLog(#"previousEvent is NIL!");
}
What am I missing here?
It's hard to say with the code that's been shown, but it's possible that you never create the mutable array gameEvents and it's nil when you try to access it or add events to it. Make sure you're creating it somewhere, probably in FSGame's -init:
_gameEvents = [NSMutableArray array];
You need to instantiate the arrays, this does NOT happen when you use
_game = [[FSGame alloc] init];
Try using a lazy instantiation in your FSGame class, add this code
-(NSMutableArray *)gameEvents{
if(!_gameEvents){
_gameEvents = [NSMutableArray array];
}
return _gameEvents;
}
And that should be enough, also make sure of accesing by self.gameEvents and not by _gameEvents!
_game.gameEvents is a pointer to a mutable array. It is not initialized, so its value is nil. There are no any mutable array to add objects to.
You can create it when needed writing this method in FSGame.m:
-(NSMutableArray*) gameEvents {
if ( ! _gameEvents ) {
_gameEvents = [NSMutableArray array];
}
return _gameEvents ;
}

iOS: Saving to Data Structure

I would like to create an NSObject class that I can use the instance of and save to its variables and later pass its data elsewhere (NSManagedObject). Do I need to do anything else besides creating a new Object-C Class that inherits from NSObject. Create Variables in .h and synthesize in .m.
i.e.:
my .h file:
#import <Foundation/Foundation.h>
#interface MyDataClass : NSObject
#property (nonatomic, retain) NSNumber *variable1
#property (nonatomic, retain) NSString *variable2
#property (nonatomic, retain) NSDate *variable3
#end
.m file:
#import "MyDataClass.h"
#implementation MyDataClass
#synthesize variable1, variable2, variable3
#end
I would like to be able do the following in some SomeViewController:
#property (nonatomic, strong) MyDataClass *newDataClass
#systhesize newDataClass;
newDataClass.variable1 = #"123457890";
newDataClass.variable2 = #"This is the new Variable";
newDataClass.variable3 = [NSDate date];
Is there anything else I need to do to initialize each variable when an instance of this class is created? Am I missing anything?
That's all that you need for a custom object which you want to use to store data.
Of course when you go to use it (in SomeViewController), you need to actually create an instance of your class before you start setting the variables:
self.newDataClass = [[MyDataClass alloc] init];
I would also use self.newDataClass instead of just newDataClass unless you have a reason not to.

Resources