NSMutableArray, objectAtIndex giving a different number?

Discussion in 'iOS Programming' started by maymunaga, Jun 30, 2010.

  1. maymunaga macrumors newbie

    Joined:
    Jun 30, 2010
    #1
    Hi there this is a console application

    here is the code:
    Code:
    #import <Foundation/Foundation.h>
    
    int main (int argc, const char * argv[]) {
        NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    
        // insert code here...
    	NSMutableArray *aras = [[NSMutableArray alloc]init];
    	[aras addObject:[NSNumber numberWithInt:11]]; 
    	
    	
    	
        NSLog(@"MyNumber is, %d",[aras objectAtIndex:0 ]);
        [pool drain];
        return 0;
    }

    it prints to console


    2010-07-01 02:26:20.680 CalisArray[4936:903] MyNumber is, 1084752

    I want to print number 11 which i add into my Array, however it prints another big number 1084752 why?? And how can i call 11?
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    objectAtIndex: is not returning an int. Think about it.
     
  3. maymunaga thread starter macrumors newbie

    Joined:
    Jun 30, 2010
    #3

    So ?
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    Your NSLog() is trying to print the value of an int (via %d). objectAtIndex: returns an object (imagine that), not an int.
     
  5. maymunaga thread starter macrumors newbie

    Joined:
    Jun 30, 2010
    #5
    Ok I understand but what i have to do to see my number 11??
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    Reverse what you did here:
    Code:
    [NSNumber numberWithInt:11]
     
  7. maymunaga thread starter macrumors newbie

    Joined:
    Jun 30, 2010
    #7
    Really i dont understand i want a number from array by giving index number is that possible??
     
  8. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    If you don't understand what you did there in the first place (creating an NSNumber object that holds an int value) and how to reverse it, I'd suggest you step away from the real coding and go learn the basics of Objective-C.
     
  9. maymunaga thread starter macrumors newbie

    Joined:
    Jun 30, 2010
    #9
    Please don't be sapient, i am thinking like c#.Net or vb.Net or c++,and i am MCSD.NET, i am very new on Objective C, i just want to help! If you dont want to help people who are new on this subject why are you here in this forum? Firstly you learn what is the idea of forums...
     
  10. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #10
    I thought I was trying to help. If you just want code you can copy-and-paste but don't actually understand, you'll have to look to someone else. I prefer to try to guide people to their own discovery.

    I think we have different ideas of what this forum is for. To me, copying-code != programming.
     
  11. maymunaga thread starter macrumors newbie

    Joined:
    Jun 30, 2010
    #11
    my friend i don't want a code, just help.
    You said that : objectAtIndex: was not returning an int. Think about it.
    then said: Reverse what you did here:
    Code:
    [NSNumber numberWithInt:11]

    what kind of guide people??

    i don't know what does "Reverse what you did here" mean in programming?? there is no help just saying something.

    If i were to u , i would say that do this here, you go look at line number then change your code, you can not do this like that some thing like that.

    I need help just it...

    finally being_sapient = know_nothing
     
  12. maymunaga thread starter macrumors newbie

    Joined:
    Jun 30, 2010
    #12

    Why whole programming books contain sample codes? please think about it, And please you don't help me again. Because i don't have free time. Thank you for your help (your style help).:mad:
     
  13. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #13
    I'm not your friend...
    Especially if you make comments like this. I happen to believe I know plenty about Objective-C programming (among other things) and I think there are a number of other posters in this forum (some of whom have thanked me for my help) who would agree.
    As you wish.
     
  14. maymunaga thread starter macrumors newbie

    Joined:
    Jun 30, 2010
    #14
    I did not say that you don't know Objective C. Also knowing something well is not important in that case. The importance is helping people' not being sapient. Remove your arrogance. Just do it you would be a good helper.
     
  15. maymunaga thread starter macrumors newbie

    Joined:
    Jun 30, 2010
    #15
    Finally i found it,

    NSLog(@"MyNumber is, %d",[[aras objectAtIndex:0]intValue]);

    when i add intValue at the end of called array object.
     
  16. PhoneyDeveloper macrumors 68040

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #16
    Consider yourself guided.

    Take the day off and go fishing.
     
  17. jared_kipe macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #17
    You might also wonder if you have an object, how do you display that in a string or log.

    NSLog(@" %@ ", anObject);
     
  18. seepel macrumors 6502

    seepel

    Joined:
    Dec 22, 2009
    #18
    I'll try to go into some detail...

    aras is a pointer to an NSNumber object

    intValue is a member function of NSNumber that returns the value inside the NSNumber as an int. You could also do floatValue if you wanted a float, or several other formats that you can find in the documentation.

    Th following code is totally fine, it will print the value of the pointer (ie. the memory location that it is pointing to.)
    Code:
    NSLog(@"%d", aras)
    Of course you can always get an int or a float respectively...
    Code:
    NSLog(@"%d", [aras intValue]);
    NSLog(@"%f", [aras floatValue]);
    
    Finally, if an object has a description function (which NSNumber does) you can print that with
    Code:
    NSLog(@"%@", aras)
     
  19. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #19
    Actually, it's a pointer to an NSMutableArray object, thus the need for the objectAtIndex: method call.
     
  20. PhoneyDeveloper macrumors 68040

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #20
    No I didn't wonder that. But since you bring it up, and in the interests of teaching newbies to fish, there is a little more that should be explained.

    Code:
    NSLog(@"%@", anObject);
    is equivalent to

    Code:
    NSLog(@"%@", [anObject description]);
    Since description is a method on NSObject all subclasses will inherit the NSObject version of that method:

    Code:
    - (NSString *)description;
    For most Foundation classes (NSString, NSArray etc.) these classes implement their own useful versions of the description method. For your own classes you might want to write your own description method that returns something useful. The description is also visible in the debugger.
     
  21. jared_kipe macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #21
    I think you misunderstood what I was getting at.

    The OP was basically calling
    NSLog(@" %d ", (NSNumber *)anObject);

    What I was trying to point out was that this was not having the right results because %d is not the descriptor for an object, but he was giving it an object none the less.

    Now if the OP switched from %d to %@ he would have actually gotten the expected output (though maybe not the expected understanding)

    NSLog(@"%@ and %@",[NSNumber numberWithInt:25], [NSNumber numberWithDouble: 5.93]);

    Would give the expected values, and in the case of the double, a slightly better (prettier) result than using say %f and -doubleValue
     
  22. seepel macrumors 6502

    seepel

    Joined:
    Dec 22, 2009
    #22
    Good point, next time I decide to sound all preachy maybe I'll actually read the post...
     
  23. jared_kipe macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #23
    Especially when the variable name is as descriptive as "aras" :p
     
  24. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    Location:
    Sailing beyond the sunset
    #24
    It may be quite descriptive in the author's primary language. FWIW.

    Nevertheless, it's clear the OP is making a number of invalid assumptions about how Objective-C works and how foundation classes like NSArray work. The assumptions appear to be based on the fundamentals of C#, which has autoboxing, or VB or C++, where array-objects behave differently than NSArray.

    In my opinion, the single best thing the OP can do is to borrow a book on the fundamentals of Objective-C and the foundation classes, or simply read Apple's introductory docs on these topics.

    The overall principles of C#, VB, or C++ will be similar enough. The differences are mainly going to be in the details, but those differences will be crucial.

    Trying to figure out the details by unguided trial and error (and complaining on forums), without reading reference docs, is probably one of the worst possible approaches.
     
  25. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #25
    Yeah, that's kinda why I suggested a step-back in a previous post.
     

Share This Page