NSMutableArray within NSString
After 10 years without programming (in Delphi at the time), I began learnin开发者_StackOverflow社区g Objective-C. I confess I have met lots of problems as this approach is very different.
I want to make a simple table in which each line consists of several NSString variables. An example:
ID Firstname Lastname
01 FN1 LN1
02 FN2 LN2
My table:
NSString * FirstName, * LastName;
NSMutableArray * person = [NSMutableArray arrayWithObjects: firtsname, lastname, nil];
The problem is that I can not add a line with the 2 NSString, for example:
FirstName = @ "FN1";
LastName = @ "LN1";
[person AddObject: FirstName, LastName];
FirstName = @ "FN2";
LastName = @ "LN2";
[person AddObject: FirstName, LastName];
//etc. ...
I know that AddObject can only add 1 object but it is to explain the concept of my example.
How can I add 2 objects at the same time like in my example?
then, how can i manage/retrieve objects NString "1" (firstName) & NString "2" (lastName) by the index (i);
I confess I can not find the logic of development and your responses would be very helpful.
Thank you very much for your reply.
EDIT :
I have a strange problem. My .m :
Person *pers = [[Person alloc]init];
NSMutableArray *myarray = [[NSMutableArray alloc] init];
[pers setFirstName:@"FN 1"]; // = pers.firstName;
[pers setLastName:@"LN 1"]; // = pers.lastName;
[myarray addObject:pers];
[pers setFirstName:@"FN 2"];
[pers setLastName:@"LN 2"];
[myarray addObject:pers];
[pers release];
NSLog(@"count: %d", [myarray count]);
for(int i = 0; i < [myarray count]; i++)
{
pers = [myarray objectAtIndex:i];
NSLog(@"%d %@ %@ %@", i, pers, pers.firstName, pers.lastName);
}
[myarray release];
and my NSLog is : 2011-04-27 19:07:57.582 temp[11724:903] count: 2
2011-04-27 19:07:57.584 temp[11724:903] 0 FN 2 LN 2
2011-04-27 19:07:57.585 temp[11724:903] 1 FN 2 LN 2
As you can see, i have FN 2 LN 2 twice. Object "pers" have twice the same value
Normal ?
The easiest way to do this would be to crank out a very simple model class:
"Person.h"
#import <Foundation/Foundation.h>
@interface Person : NSObject {
NSString *firstName;
NSString *lastName;
}
@property (readwrite, copy) NSString *firstName;
@property (readwrite, copy) NSString *lastName;
@end
"Person.m"
#import "Person.h"
@implementation Person
@synthesize firstName, lastName;
- (void)dealloc
{
[firstName release];
firstName = nil;
[lastName release];
lastName = nil;
[super dealloc];
}
@end
This would allow you to use addObject:
since it would technically be one object, you're just able to store multiple pieces of data in it. An example follows:
//...
Person *p1 = [[Person alloc] init];
[p1 setFirstName:@"First Name"];
[p2 setLastName:@"Last Name"];
[personArray addObject:p1];
[p1 release];
//...
If you aren't familiar with @property
or @synthesize
I would recommend this article explaining both.
In response to the edit, you would be able to get at the values using something like the following:
Person *peep = [personArray objectAtIndex:i];
NSString *firstName = [peep firstName]; //or you could use peep.firstName;
NSString *lastName = [peep lastName];
Try this:
NSDictionary *person = [[NSDictionary alloc] initWithObjectsAndKeys:
@"Bill", @"Firstname",
@"Gates", @"Lastname", nil];
NSMutableArray *storage = [[[NSMutableArray alloc] init] autorelease];
[storage addObject:person];
[person release];
精彩评论