1

I'm doing a tutorial from this book: "Objective-C 2.0 Essentials 3rd edition" by Neil Smyth. I have tried repeatedly but keep getting the same "Expected expression" error even though the books version claims to run. I've checked way too many times and my version is exactly the same as the books. Please, can someone help me. Code below:

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
    @autoreleasepool {
        int x;
        int j = 54321;

        for (x = 0; x < 10; x++) {

        }

        int j = x + 10;
        NSLog (@"Variable j in for loop is %i", j);

        NSLog (@"Variable j outside for loop is %i", j); /* I GET AN ERROR STATING " EXPECTED EXPRESSION HERE*/

    }
    return 0;
}

2 Answers 2

4

The line

    NSLog (@"Variable j outside for loop is %i", j);

contains a lot of invisible characters (UTF-8 sequence EF BF BC = OBJECT REPLACEMENT CHARACTER) between the Tab and the "NSLog".

Deleting and rewriting that line should help.

OP's code opened in hexa editor: enter image description here

Sign up to request clarification or add additional context in comments.

6 Comments

Added a screenshot, you were faster in answering and I have alredy created it :)
@trojanfoe: Even "Show Invisibles" in Xcode displays the characters only as "zero-width" space, so it is difficult to notice. What catched my eye was that "NSLog" did not have the syntax coloring.
I think both answers are correct; the code won't compile as shown by the OP anyway, due to the duplicate int j and it's clear from the strings that the first NSLog() should be inside the for loop.
@Sulthan: Thanks for the screenshot. I had copy/pasted the code into a plain text file and ran "hexdump -C" on it. That also revealed the problem.
These invisibles.. could they be a result of me having copied and pasted the code from the pdf ebook that I am studying?
|
1

Format your code better; if you do the misplaced } in the code becomes obvious:

 #import <Foundation/Foundation.h>
 int main (int argc, const char * argv[]) {
     @autoreleasepool {
         int x;
         int j = 54321;
         for (x = 0; x < 10; x++) {
             int j = x + 10;
             NSLog (@"Variable j in for loop is %i", j);
         }
         NSLog (@"Variable j outside for loop is %i", j);
     }

     return 0;
}

EDIT The invisible characters as pointed out by @MartinR are also an issue (that I didn't notice). So there are two errors in your code.

5 Comments

+1 I don't see a missing brace in the OP's code, but I think you're right that there's one missing in the actual source.
@Caleb I thought initially it was missing, but it's misplaced. The real key to the answer is about formatting the code, which makes such errors easier to see. I wish the original question had not be editted, to highlight this issue.
@Caleb I wasn't getting into the "brace on the same/new line" at all; the indentation was the only issue I was highlighting.
Thanks heaps guys!! That show invisibles thing worked! I saw that there was an extra character which I don't recognise..between the g on NSLog and the opening bracket - as you all might have guessed, I'm an extreme noob. I think the big lesson for me today is be wary of copying and pasting code!
@Caleb He was referring to my edit of the question. I originally thought there was a missing brace but when I realized the bracers are paired correctly, I fixed the indentation.

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.