Chapter 16 Challenge 2

There are so many topics here for the chapter 16 challenge(s) I wasn’t sure where to post. Also there seems to have been some confusion as to which challenge we were supposed to re-write. Some rewrote the AaronIsCool challenge and some re-wrote the Countdown challenge.

I’m too embarrassed to confess how long this took me to complete. I’m really struggling with the Object Oriented stuff. Anyway this is what I got (I re-wrote Countdown BTW):

[code]#import <Foundation/Foundation.h>
#import <stdio.h>
#import <readline/readline.h>

int main(int argc, const char * argv[])
{

@autoreleasepool {
    
    NSString *userInput = [NSString stringWithUTF8String:readline("Where shall we begin?\n")];
    NSInteger i = [userInput integerValue];
    while (i >=0) {
        NSLog(@"%ld\n", (long)i);
        if (i % 5 == 0) {
            NSLog(@"Found one!\n");
        }
        i -= 3;
    }
}
return 0;

}
[/code]

I like your use of nested message send. I’m not so sophisticated yet :slight_smile:. I think it could be more efficient to use [NSString intValue] vice [NSString integerValue] so you don’t have to recast. Also I used a for loop instead… but that’s just preference.

Also might have been nice in the book to remind that we need to re-add the library in the “Build Phase”.

#import <Foundation/Foundation.h>
#include <stdlib.h>
#include <readline/readline.h>

int main(int argc, const char * argv[])
{

    @autoreleasepool
    {
    
        NSLog(@"Where should I start counting?");
        const char *start = readline(NULL);
        NSString *myStart = [NSString stringWithUTF8String:start];
        
        for (int i = [myStart intValue]; i>=0; i-=3)
        {
            NSLog(@"%d",i);
            if(i%5==0) NSLog(@"Found One!");
        }
    }
    return 0;
}

I’ve noticed most people here seem to prefer for() loops over while(). Not sure where I picked up the preference, but as I’ve done more of the challenges, I’ve started using for() more often.

As far as the nested message send, I think I just guessed that might be what argument deadline() took. I know I didn’t look up. I got a little jolt of unearned pride when it worked though.

I’m always running into casting issues due to a lack of understanding. Now I’m going to have to go look up the difference between intValue and integerValue :wink:

Speaking of sophistication, I didn’t know you could single line the if statement like that; I like it; it really tidies things up.