0

I have an NSMutableArray in my code and I want to access the last element of that array and save the element in integer n variable. But n does not give me the correct value when I NSLog it, it gives me a garbage value.

 for(int i=0;i<[array3 count];i++)
    {
        if(i==([array3 count]-1))
        {
            n = [array3 objectAtIndex:i];
        }    
    }

    NSLog(@"The id is=%d",n);

    NSError *error;
    NSString *aDocumentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
        NSString *dataPath = [aDocumentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%d",n]];
      [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error];
3
  • 1
    If n is type int, it would not work. Because array only holds object and int is not object. Commented Jul 18, 2013 at 16:30
  • the n is type int but it give me the garbage value. Commented Jul 18, 2013 at 16:32
  • You can't store int in an NSMutableArray directly. Paste the code where you are adding objects Commented Jul 18, 2013 at 16:34

2 Answers 2

3

Arrays contain objects, not scalar types like ints. Try this instead:

id n = [array3 lastObject];          // prettier than objectAtIndex:[array count]-1
NSLog(@"the last object is %@", n);   // %@ is an object format descriptor

Maybe the array contains NSNumbers that make sense as integers? Then,

NSNumber *n = [array3 lastObject];
NSLog(@"the last object is %@ or view it this way %d", n, [n intValue]);

You can also access an array with newer syntax like this:

id n = array3[array3.count-1];

You can also iterate an array more quickly and concisely like this:

for (id object in array3) {
    NSLog(@"%@", object);
}
Sign up to request clarification or add additional context in comments.

3 Comments

sir, what is "id" mean what is its type?
An id is an objective-c keyword that is 'some kind of object', see developer.apple.com/library/ios/#documentation/cocoa/conceptual/…
id is the objective c generic type, like void* in c. You probably ought to figure out what type(s) is really contained by the array and code for that.
0

It's giving you a garbage value because you're trying to cast an object to a primitive most likely.

Also, that for loop is unnecessary.

n = [[array3 lastObject] integerValue];

Should be everything you need. If that is still garbage, it's because the object in your array is garbage (or can't be cast to an integer). At that point, take a look at where you're setting up array3.

1 Comment

Since OP uses %d in his NSLog() I assume he uses int and not NSInteger, so would call intValue instead of integetValue ;)

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.