I’ve made it through data types (might have a post about that) and am now in the looping chapter. In one of the examples the author brought up reading input from a console app to show how to dynamically do a for loop.

In Objective-C, to read keyboard input from the console you can use:

int userInput;
scanf("%i", &userInput);
NSLog(@"You typed %i.", userInput);

This isn’t too much different than C#:

int userInput = int.parse(Console.ReadLine());
Console.WriteLine(String.Format("You typed {0}.", userInput.toString()));

(note: wrote both by hand without testing so don’t bash me if I missed something) šŸ˜‰

I need to learn more about scanf but it seems the first parameter requires an int but I’m not sure if that means the user is restricted to typing only numbers where characters won’t even show on entry or if it means it attempts to parse the value as a number. I need to play with it but i wanted to blog my note about scanf before I forgot.

Quick Notes on Objective-C block above:

  • the %iĀ  means replace or expect an integer value
  • the @ in NSLog(@”…” is required. It denotes the string as an NSString. More on that in another blog post.
  • the &userInput in the scanf has something to do with a pointer but I haven’t made it to pointers yet so will post about those when I get there.

Update

There are numerous other methods for reading input different ways. You can read more here.