code from the book doesn't work in x-code

Discussion in 'Mac Programming' started by monomammoth, Jun 10, 2010.

  1. monomammoth macrumors newbie

    Joined:
    Jun 10, 2010
    #1
    I got books "objective-c for dummies" and "cocoa for dummies" when I'm inserting some codes from the book to my x-code (3.1.3.) is shows me errors.

    Is there a difference between x-code versions in terms of actual programming codes? :confused:

    thanks
     
  2. miles01110 macrumors Core

    miles01110

    Joined:
    Jul 24, 2006
    Location:
    The Ivory Tower (I'm not coming down)
    #2
    What are the errors? It's pointless to state "there are errors" and ask for help when you don't say what the errors are.
     
  3. GorillaPaws macrumors 6502a

    GorillaPaws

    Joined:
    Oct 26, 2003
    Location:
    Richmond, VA
    #3
    The compiler is case sensitive, so even a capitalization difference will cause problems. Also, it's easy to forget semi-colons and other details. Cut and paste your code here and maybe we can help you spot where the problem is.
     
  4. monomammoth thread starter macrumors newbie

    Joined:
    Jun 10, 2010
    #4
    sorry for that, here is one example:

    Code:
    int a; 
    	int b; 
    	int c; 
    	
    	a = 1; 
    	b = 2; 
    	c=a+b;
    	
    	NSLog (@” a + b = %i”, c);
    error: syntax error before '@' token
    error: syntax error at 'OTHER' token 
    	NSLog (@” a + b = %i”,a + b);  
    error: syntax error before '@' token
    error: syntax error at 'OTHER' token 
    	NSLog (@” a still = %i”, a); 
    	NSLog (@” b still = %i”, b); 
    error: syntax error before '@' token
    error: syntax error at 'OTHER' token 
    	
        return 0;
    }
     
  5. garethlewis2 macrumors 6502

    Joined:
    Dec 6, 2006
    #5
    How did you create your project in XCode? Was it an Command Line Tool with the Foundation classes or a Command Line Tool with the C code option set?
     
  6. monomammoth thread starter macrumors newbie

    Joined:
    Jun 10, 2010
    #6
    It is comand line utility -> foundation tool project
     
  7. GorillaPaws macrumors 6502a

    GorillaPaws

    Joined:
    Oct 26, 2003
    Location:
    Richmond, VA
    #7
    Cut/Paste the entire file. I suspect you're missing
    Code:
    #import <Foundation/Foundation.h>
    Basically, an @ symbol is used in objective-c but not regular c. In order for the compiler (the thing that's kicking out errors at you) to know how to use the NSLog(), it needs to import the foundation framework (you may sometimes see the <Cocoa/Cocoa.h> declaration instead).
     
  8. kpua macrumors 6502

    Joined:
    Jul 25, 2006
    #8
    What is the name of the file? Does it end with .m?
     
  9. monomammoth thread starter macrumors newbie

    Joined:
    Jun 10, 2010
    #9
    seems that it is the same, here is the screenshot:

    [​IMG]
     
  10. GorillaPaws macrumors 6502a

    GorillaPaws

    Joined:
    Oct 26, 2003
    Location:
    Richmond, VA
    #10
    I think I figured it out. The " character you're using isn't the right one. When I cut/pasted your code in, it wasn't recognized as a string. When I replaced your " with my own it picked it up.
     
  11. idelovski macrumors regular

    Joined:
    Sep 11, 2008
    #11
    Did you type it or was it a copy/paste from a pdf?

    You seem to have smart quotes in your code. From Wikipedia: Curved and straight quotes are also sometimes referred to as smart quotes (“”) and dumb quotes ("") respectively
     
  12. monomammoth thread starter macrumors newbie

    Joined:
    Jun 10, 2010
    #12


    yeah that helped!, now it's working, thanks!
     

Share This Page