Jump to content
10 posts in this topic

Recommended Posts

Another newbie question...

 

I want to create an Array with 18 integers that I am able to manipulate and get value of on demand.

I'm all new to Cocoa and Objective C and so far I've tried to create a NSArray but when I try to add things all I am able to add are zeros. ArrayWithObjetcs: 0,0,0,0,0,0,nil

If I try to add ArrayWithObjects; myInt1, myInt2, nil I recieve a casting error.

 

I am pretty sure that I am going about this all wrong.

Anyone feel up to giving me a 101 on Arrays?

 

Let me know if these types of questions are too simple/wide for this forum.

 

/H

Link to comment
https://www.insanelymac.com/forum/topic/98552-need-help-creating-nsarray/
Share on other sites

If you're going to do that, try something like int intArray[18] = { 1, 2, 3, 4, 5, <etc> };

 

To access a variable, you would do intArray[n], where n is the index of the integer (the index starts at 0, so '1' in the above example would have the index 0).

 

If you really need to use NSArrays, you should probably do [NSArray arrayWithObjects:[NSString stringWithFormat:@"%i", 1], [NSString stringWithFormat:@%i", 2], <etc>, nil]; and to get the integer value again, you would do [[NSArray objectAtIndex:n] intValue];

 

However the first method is probably the best.

 

The reason it doesn't work is because integers are not objects, and arrayWithObject: obviously requires an object.

If you're going to do that, try something like int intArray[18] = { 1, 2, 3, 4, 5, <etc> };

 

To access a variable, you would do intArray[n], where n is the index of the integer (the index starts at 0, so '1' in the above example would have the index 0).

 

If you really need to use NSArrays, you should probably do [NSArray arrayWithObjects:[NSString stringWithFormat:@"%i", 1], [NSString stringWithFormat:@%i", 2], <etc>, nil]; and to get the integer value again, you would do [[NSArray objectAtIndex:n] intValue];

 

However the first method is probably the best.

 

The reason it doesn't work is because integers are not objects, and arrayWithObject: obviously requires an object.

 

First of all, if you want to be able to manipulate the array (e.g. adding/remove objects from it after its creation) you should use NSMutableArray.

 

Second, don't use a string to save ints as objects, but use NSNumber.

Alternatively, you could declare variables as NSNumbers, then assign them, and then add them. This is from an old app of my (my first, incidentally) and this is in no way the best way... but it works.

day0 = [NSDate dateWithTimeIntervalSinceNow:86400*0];
day1 = [NSDate dateWithTimeIntervalSinceNow:86400*1];
day2 = [NSDate dateWithTimeIntervalSinceNow:86400*2];
day3 = [NSDate dateWithTimeIntervalSinceNow:86400*3];
day4 = [NSDate dateWithTimeIntervalSinceNow:86400*4];
day5 = [NSDate dateWithTimeIntervalSinceNow:86400*5];
day6 = [NSDate dateWithTimeIntervalSinceNow:86400*6];
day7 = [NSDate dateWithTimeIntervalSinceNow:86400*7];
day8 = [NSDate dateWithTimeIntervalSinceNow:86400*8];
day9 = [NSDate dateWithTimeIntervalSinceNow:86400*9];
day10 = [NSDate dateWithTimeIntervalSinceNow:86400*10];
combodates = [[NSMutableArray alloc] initWithCapacity:11];
[combodates addObject:day0];
[combodates addObject:day1];
[combodates addObject:day2];
[combodates addObject:day3];
[combodates addObject:day4];
[combodates addObject:day5];
[combodates addObject:day6];
[combodates addObject:day7];
[combodates addObject:day8];
[combodates addObject:day9];
[combodates addObject:day10];

Also, you should use initWithObjects: instead of initWithCapacity:.

Again, I'm sure there are better ways to do this. And remember to declare all of your variables in [your class].h.

And even though these are dates, just adapt it for [NSNumber numberWithInt:(intvalue)].

First of all, if you want to be able to manipulate the array (e.g. adding/remove objects from it after its creation) you should use NSMutableArray.

 

Second, don't use a string to save ints as objects, but use NSNumber.

 

Yeah, sorry, completely forgot about NSNumbers, since I never use them.

 

Obviously it should be a mutable array, but I was just using an array as an example.

Thanks for all the help.

 

However, if I create int myIntArray[18] = {1,2,3,2,1,2,3,2,1,2,3,3,2, etc};

and then try to do this:

myTempInt=myIntArray[0];

i get incomparable types in assignment.

 

Why is that? Shouldn't myIntArray[0] simply be an int of value 1?

 

I appreciate all the help I've been getting from this forum, thanks to all!

×
×
  • Create New...