views:

464

answers:

4

Ok, so I've been using NSLog in objective-C for awhile now to debug and I know it's supposed to print to the terminal whatever I put in the parentheses. For some reason, it just stopped printing to the terminal and I'm not sure how to fix this error. I was wondering what other people would suggest doing to fix this problem. I've only included part of my code because I don't want to scare away someone from answering this simple (or at least I hope it's simple to fix) problem. When I run the code, the only two statements that print are "serverButton - Stage 1" and "serverButton - Stage 2 - Complete" but nothing else in between. FYI -(void)startServer is in another class called "Server" and I have made "server" a pointer to that said class.

-(IBAction)serverButton {
NSLog(@"serverButton - Stage 1");
[server startServer];
NSLog(@"serverButton - Stage 2 - Complete");
}

-(void)startServer {
    NSLog(@"serverButton - Stage 1");

    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_UNSPEC; // set to AF_INET to force IPv4
    hints.ai_socktype = SOCK_DGRAM;
    hints.ai_flags = AI_PASSIVE; // use my IP

    if ((rv = getaddrinfo(NULL, MYPORT, &hints, &servinfo)) != 0) {
     NSLog(@"ERROR: serverButton - Stage 1");
     fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
     serverError = 1;
     NSLog(@"Error");
    }
A: 

You say:

FYI -(void)startServer is in another class called "Server" and I have made "server" a pointer to that said class.

If I read that right, your server variable is actually pointing to a class, like so:

Class server = [Server class];
...
NSLog(@"begin");
[server startServer];
NSLog(@"end");

If that is the case, then your startServer method would have to be a class method:

+ (void)startServer {
    NSLog(@"hello!");
}

If that is not the case, can you please post a little bit more code? Specifically, the part where you assign to that server variable would be helpful.

e.James
A: 

Add a check to make sure server is not nil before you call startServer, if it was nil then nothing would get called and no error would be generated.

Solmead
Sorry, I just realized what was happening. I moved the code out of my class this morning and called it directly. There was nothing wrong with it in the first place and it was doing what it was supposed to do...which was having the server sit there and wait on a message from the client. I guess it was a combination of my assuming my code was wrong when it wasn't and being late into the night. Thanks for your help!
Josh Bradley
A: 

It looks like the problem isn't NSLog, but rather that startServer isn't actually getting sent to the receiver like you think it is.

Reed Olsen
A: 

If the NSLog doesn't print to the terminal (explain what is terminal). I assume terminal is the Xcode debugger console. Then, you need to check the Console.app, whether it's printed there.

The new version of Xcode doesn't have this problem.

Jesse Armand