2

This should be dead easy, but somehow it doesn't want to work for me. Using iOS 7 and XCode 5. All I'm trying to do is create an array with values from 1 to 100.

NSMutableArray *array;
for (int i = 0; i < 100; i++)
{
    [array addObject:i];
}

This doesn't work. I get a "Implicit conversion of 'int' to 'id' is disallowed with ARC. I get it, I can't add primitive types to an NSMutableArray.

    [array addObject:@i];

This doesn't work either. I get a "unexpected '@' in program"

    [array addObject:[NSNumber numberWithInt:i]];
    [array addObject:[NSNumber numberWithInteger:i]];

(either case) This "works" (compiles) but it really doesn't "work". The problem with this is that the value from NSNumber is really not a 1-100. What I get for each row is "147212864", 147212832", "147212840"...not what I want.

Lastly:

for (NSNumber *i = 0; i < [NSNumber numberWithInteger:100]; i++)
{
    [array addObject:i];
}

This also doesn't compile. I get an error on the i++. "Arithmetic on pointer to interface 'NSNumber', which is not a constant size for this architecture and platform"

Any suggestions on how to do this extremely simple thing on obj-c?

9
  • 4
    Minimally, @i should be @(i) Commented Mar 3, 2014 at 17:40
  • 1
    And don't forget that on the other end you need to do intValue on the NSNumber to get the int back. Commented Mar 3, 2014 at 17:43
  • 1
    @TooManyEduardos. That's because you forgot to allocate and init your array. Commented Mar 3, 2014 at 17:46
  • 1
    Or loop from 1 to <= 100, etc. Commented Mar 3, 2014 at 17:49
  • 1
    When you are getting the "147212864", 147212832", "147212840" it is because you are outputing the values wrongly. It is obvious those are pointer values, not the value stored in objects at those locations. Commented Mar 3, 2014 at 17:49

5 Answers 5

3

Either one of these should work:

NSMutableArray *array = [NSMutableArray array];
for (int i = 0; i < 100; i++) {
    [array addObject:@(i)];
}

or

NSMutableArray *array = [NSMutableArray array];
for (int i = 0; i < 100; i++) {
    [array addObject:[NSNumber numberWithInt:i]];
}

Here are the reasons why your code snippets did not work:

  1. [array addObject:i] - You cannot add primitives to Cocoa collections
  2. [array addObject:@i] - You forgot to enclose the expression i in parentheses
  3. NSNumber *i = 0; i < [NSNumber numberWithInteger:100]; i++ - You cannot increment NSNumber without "unwrapping" its value first.
Sign up to request clarification or add additional context in comments.

1 Comment

damn parentheses. Thanx.
1

If memory serves, I think you're simply missing parenthesis around the NSNumber shorthand expression.

NSMutableArray *array = [[NSMutableArray alloc] init];
for (NSUInteger i = 0; i < 100; i++)
{
    [array addObject:@(i)];
}

2 Comments

This can't work unless you allocate and init the array first. NSMutableArray *array = [NSMutableArray array]
@ahwulf Absolutely correct. I took the code posted in the question and only focused on the issue with the NSNumber literal. I'll edit for your concerns for any future visitors to this question.
1

Minimally, @i should be @(i) as described here. You are also forgetting to allocate and initialise your array

NSMutableArray *array = [[NSMutableArray alloc] init];
for (int i = 0; i < 100; i++) {
    [array addObject:@(i)];
}

And since you are getting: "147212864", 147212832", "147212840"...not what I want., I think you are probably printing out your information wrongly or because the array is unallocated, that's simply garbage. Can you show us how you are outputting?

Comments

0
 NSMutableArray *array = [[NSMutableArray alloc] init];
 NSNumber *myNum;
 for (int i = 0; i < 100; i++) {

     myNum = [[NSNumber alloc]initWithInt:i];
     [array addObject:myNum];
 }
 NSLog(@"%@", array); // 1 - 99 as expected

Worked for me :)

2 Comments

"1 - 99 as expected". Well, I'd expect 0 through 99 (from you code).
my childhood is a lie
-1

Just saying: Turn on all reasonable warnings in your Xcode project. Then read what the warnings are saying and do something about them. When you write something like

for (NSNumber *i = 0; i < [NSNumber numberWithInteger:100]; i++)

What does a for loop do? An object is in the end a pointer. So you initalise i to nil. Then you compare a pointer with a random pointer: [NSNumber numberWithInteger:100] returns a pointer to an object which could be anywhere in memory, and you compare pointers. Next the i++: No, you can't increment a pointer to an NSNumber. It doesn't make sense.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.