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

Doju

macrumors 68000
Original poster
Jun 16, 2008
1,510
1
I'm beginning programming in Obj-C, and the book I'm learning from seems to have some typos here and there and this example code the book gave fails to compile.

Code:
// First program example

int main (int argc, const char * argv[])
{
	NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
	
	int value1, value2, sum;
	
	value1 = 50;
	value2 = 25;
	sum = value1 + value2;
	
	NSLog (@”The sum of %i and %i is %i”, value1, value2, sum);
	
	[pool drain];
	return 0;
}

Why is that?
 
This is my error:

5odp


Build and Run in Xcode. Yes, that's the complete source.
 
http://developer.apple.com/iphone/l...Foundation_Functions/Reference/reference.html

NSLog is not built-in/intrinsic. C and Objective-C don't have such a thing (there is a standard library). The link above tells you what framework NSLog is included with. You need to import the appropriate thing to use any function.

I'm not trying to be obtuse for fun, but it will rob you of a learning opportunity if I tell you exactly what to do.

-Lee
 
Actually I think the problem is the quotes:

Code:
NSLog (@[COLOR="Red"][B]”[/B][/COLOR]The sum of %i and %i is %i[B][COLOR="Red"]”[/COLOR][/B], value1, value2, sum);

Those should be the normal " character.
 
You need to this import statement at the beginning of your code
Code:
#import <Foundation/Foundation.h>
in order for NSLog to be recognized.

if you have that, then the other problem looks like it is with the smart, or curly, quotes around the string...

Code:
”The sum of %i and %i is %i”
should be
Code:
"The sum of %i and %i is %i"
with straight double quote marks
 
Oh, thanks guys. Fixing the quotes fixed the problem. Thanks. :D

5p4z


And the book is "Programming in Objective-C 2.0".
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.