newbie help loading arrays programatically
newbie help loading arrays programatically
- Subject: newbie help loading arrays programatically
- From: Steve Gran <email@hidden>
- Date: Sat, 31 Dec 2005 18:04:09 -0800
Sorry but I'm really confused again, which I know is because I'm such
a newbie when it comes to programming, but any help would be greatly
appreciated. I'm trying to load data from a file into an array which
is controlled via an array controller (bindings). But I don't
understand why I'm getting the results I'm getting (in the array).
Here's the code that loads the file and then parses the data into an
object which I want the array to contain.
declared earlier
NSMutableArray *myFinalArray;
NSArray *lineBylineList;
unsigned rowCountOfFile;
my key method
- (void)loadDataIntoArray:(NSString *) aFile
{
NSMutableArray *tempArray;
unsigned i;
theInputFile = [NSString stringWithContentsOfFile:aFile];
//This builds an array of each line of data from the file
lineByLineList = [theInputFile componentsSeparatedByString:@"\n"];
// Most files end with a line seperator so we handle this
special case
if( [[lineByLineList objectAtIndex: ([lineByLineList count] -1)]
isEqualToString:@""])
{
rowCountOfFile = [lineByLineList count]-1 ;
}
else rowCountOfFile = [lineByLineList count];
//Add objects to the final array with all of the data,
for (i = 0; i < rowCountOfFile; i++)
{
tempArray = [[NSMutableArray alloc] init];
//Create an array of immutable strings for each "i"th row (each
item is separated with a comma)
[tempArray addObjectsFromArray:[[lineByLineList objectAtIndex: i]
componentsSeparatedByString:@","]];
//Create strings of each item in the array and then set them to
anObject objects
[anObject setValue:[tempArray objectAtIndex:0]
forKey:@"personNumber"];
[anObject setValue:[tempArray objectAtIndex:1] forKey:@"fullName"];
[myFinalArray insertObject: anObject atIndex: i];
}
}
I actually have two problems. First the tableview will not show
anything unless I first "set" the array by initializing the array
(for example):
-(id)init
{
myFinalArray = [[NSMutableArray alloc] init];
int i;
for (i = 0; i < 3; i++)
{
[ myFinalArray addObject: anObject = [[MyClass alloc] init]];
}
return self;
}
So my first question is...Why can't I programatically initialize the
array that an array controller (with bindings, does this matter?) is
handling?
And second, when I run all this code, the same data is copied into
all the objects (in this simple example, all 3 objects in the
array). So if my data (the file) contains the following:
23,JohnDoe
19,BillSmith
44,HarryJones
My tableview shows the following:
44 HarryJones
44 HarryJones
44 HarryJones
I don't get why the final values are being set to all the objects in
the array. I had thought I set up the loop to assign each piece of
data into an object which is then added at incremental indexes into
the array.
Again, I'm a real newbie so don't worry about being too detailed in
your advice in fact, *please* feel free to talk to me like a child.
:)
Thanks,
Steve
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden