Been playing around with XCode for about 2 weeks now, and reading about MVC a bit. I have a problem trying to connect the Model to the Controller because I find it hard to get my head around arrays. I can handle simple arrays when i programmed some in Java but I'm quiet intimidated by the Obj-C NSArrays
I see.
If somebody would be so kind to show me some simple calls on an array of objects i would be eternally grateful.
My model:
Person.h
#import <Foundation/Foundation.h>
@interface Person : NSObject
{
NSString *name;
NSNumber *age;
}
@property(nonatomic, retain) NSString *name;
@property(nonatomic, retain) NSNumber *age;
@end
Person.m
#import "Person.h"
@implementation Person
@synthesize name;
@synthesize age;
@end
I've kept if very simple while I try and learn.
Now my Controller class. What i want to do is create an array of 40 'Person' objects. But i don't know the correct way to put that in code in Obj C.
Controller.h
#import <Foundation/Foundation.h>
@class Person;
@interface Controller : NSObject
{
Person *person;
}
@property(nonatomic, retain) Person *person;
-(void) doSomeWork;
@end
Controller.m
#import "Controller.h"
#import "Person.h"
@implementation Controller
@synthesize person;
-(IBAction)doSomeWork
{
// I guess here is where i should create my array of 40 person objects
}
@end
My problem is in how to declare the array of person objects of size 40. And then how to access the array to read and write to it.
Thanks in advance for reading my post.
Here is an example that creates an NSMutableArray instance variable in the Controller class and adds a Person object to that array each time you evoke doSomeWork:
Arrays in Objective-C with Cocoa/CocoaTouch are quite different from Java. The three main differences are:
The Obj-C arrays are just classes like any other, and have no built-in support in the language. In Java, arrays have a built-in support in the language through the
[]
operators.The Obj-C arrays are untyped, they take any object that inherits from
NSObject
. This means that you can mix types within the same array, although this is probably bad practice unless you have a very good reason to. (Note thatint
,double
,char
,BOOL
and other built-in types do not inherit fromNSObject
, so you'd have to wrap them inNSNumber
objects if you ever wanted to hold them in an array.)The Obj-C arrays don't really have a concept of a fixed length, unless the whole array is immutable. So you don't need to decide on length when you create the array.
To create a mutable array, i.e. one where you're allowed to change the objects (and even add and remove objects, changing the array's length), you create an
NSMutableArray
:To add an object, such as an instance of your
Person
, you call theaddObject:
method, either making a newPerson
right there, or giving it a pointer to an existing one:To remove an object, you can use
removeObjectAtIndex:
orremoveLastObject
. To replace, you callreplaceObjectAtIndex:withObject:
. To insert an object in the middle of your array (and remember, this will also increase the size of the array), you callinsertObject:atIndex:
.That covers the most important mutating methods, i.e. those specific to
NSMutableArray
. Of course, you also have the methods that only observes an array, and those are defined inNSArray
. The most important ones arecount
, giving the current number of elements, andobjectAtIndex:
, giving the object at the index you provide.Supposing you have some
Person
objects in your array, you can iterate over them like this:You might wonder how you'd create an
NSArray
as such, if you're not allowed to add to it. Well, one of its constructors isinitWithObjects:
, which allows you to write a comma-separated list of objects to add (and you need to end with anil
value). Another isinitWithArray:
, which allows you to pass another array (which could be mutable), and the constructor will use the objects from the provided array when making the new one.