Rusty
Rusty

Reputation:

Objective-C: How do I do console input?

How do I get an NSString from console input, and then try to cast it into an int?

Upvotes: 10

Views: 13688

Answers (2)

Monomachus
Monomachus

Reputation:

You can also do it directly with scanf. Example

int number = 0;
scanf("%d", &number);

Upvotes: 9

codelogic
codelogic

Reputation: 73622

You can read in a char* string from console (scanf or whatever), like you would in a regular C program. Then, create an NSString object from it using stringFromCString:withEncoding:. Finally, use NSString's integerValue function to get it's numerical value.

Upvotes: 9

Related Questions