Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

maymunaga

macrumors newbie
Original poster
Jun 30, 2010
14
0
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?
 
Your NSLog() is trying to print the value of an int (via %d). objectAtIndex: returns an object (imagine that), not an int.

Ok I understand but what i have to do to see my number 11??
 
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.

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...
 
If you dont want to help people who are new on this subject why are you here in this forum?
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.

Firstly you learn what is the idea of forums...
I think we have different ideas of what this forum is for. To me, copying-code != programming.
 
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, then. To me, copying-code != programming.

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
 
copying-code != programming.


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:
 
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.

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.
 
Finally i found it,

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

when i add intValue at the end of called array object.
 
You might also wonder if you have an object, how do you display that in a string or log.

NSLog(@" %@ ", anObject);
 
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)
 
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.
 
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
 
Actually, it's a pointer to an NSMutableArray object, thus the need for the objectAtIndex: method call.

Good point, next time I decide to sound all preachy maybe I'll actually read the post...
 
Especially when the variable name is as descriptive as "aras" :p
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.