Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to init objects in Objective-C [closed]

I'm unsure how I should initialise the various properties in an objective-C class. Please assume I'm a very new user to Objective-C in your answers...

I have the following classes:

Test class

@interface Test : NSObject
@property (nonatomic, strong) NSString *name;
@end

TestManager class

@interface TestManager : NSObject
@property (nonatomic, strong) NSMutableArray *tests; // array of Test objects (array size unknown until runtime)
@end

Controller class

@interface TestController : NSObject
@property (nonatomic, strong) TestManager *aManager;
-(void)initManager;
-(void)doSomething;
@end

I want to have an method like initManager called:

-(void)initManager
{
    // how can I init the aManager which will have an array of Test objects
}

which will automatically allocate an array of objects to be stored inside the manager class so I can do things like:

-(void)doSomething
{
   NSString *name = ((Test *)[self.aManager.tests objectAtIndex:0]).name;
}

I'm not even sure that initManager is the correct method to use - is there something built in that always gets called?

like image 641
user2543991 Avatar asked Oct 24 '25 03:10

user2543991


1 Answers

Firstly, let's look at the way we can initialize your Test class objects.

You can also write some initialization method for your Test class so instead of this:

Test example = [[Test alloc] init];
example.name = @"s";

you can write something like this:

Test example = [[Test alloc] initWithName:@"s"];

Please note that this is very common for initialization method to return newly created object, hence the initialization method usually returns 'id' type (not void).

This is the implementation for your test class which will be used in examples below.

.h file:

- (id)initWithName:(NSString *)aName;

.m file:

- (id)initWithName:(NSString *)aName 
{
   self = [super init];
   if (self) {
     _name = aName;
   }
  return self;
}

You can initialize your TestController class this way:

.h file:

- (id)initManager;

.m file:

- (id)initManager
{
  self = [super init]; //always call the superclass init method when your class inherit from other class
  if (self) { // checking if the superclass initialization was fine
    _tests = [NSMutableArray array];
    [_tests addObject:[[Test alloc] initWithName:@"s"]]; 
    [_tests addObject:[[Test alloc] initWithName:@"l"]];
  }
  return self;
}

Or something like this:

- (id)initManager
{
  self = [super init]; //always call the superclass init method when your class inherit from other class
  if (self) { // checking if the superclass initialization was fine
    _tests = [NSArray arrayWithObjects:[[Test alloc] initWithName:@"s"], [[Test alloc] initWithName:@"l"]];
  }
  return self;
}

Like the @Andrew said it is better to use alloc + init. Here are some examples of this syntax:

CGRect rect = CGRectMake(0, 0, 100, 100);
[[UIView alloc] initWithFrame:rect];

[[NSArray alloc] init]

This is the common way to initialize objects. Despite having this mechanism there are also some additional methods (which are in fact static functions) which give the programmer the nice way to initialize objects. Using them u don't have to write keyword 'alloc' so that the code is shorter and easier to read.

[NSArray array] //creates and returns empty array
[NSMutableArray array] //creates and return empty mutable array

[UIButton buttonWithType:UIButtonTypeContactAdd]; //creates and return button
like image 150
Rafał Augustyniak Avatar answered Oct 26 '25 23:10

Rafał Augustyniak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!