Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read input from a cocoa/foundation tool console?

I wonder if Objective-C/Foundation has any special commands for reading user input from the console. Since it has NSLog for output maybe there is something else I could use instead of the scanf command.

I need to read some numbers (user input) into my tool. What is the best way to get these input in types like double or int? And how do I get user input into an NSString?

like image 951
TalkingCode Avatar asked May 24 '26 23:05

TalkingCode


1 Answers

I was bored earlier and came across this issue of 'use scanf'. since I wanted to see if I could do it without dropping into c, the following came up:

NSFileHandle *input = [NSFileHandle fileHandleWithStandardInput];
while (1)
{
    NSData* data = [input availableData];
    if(data != nil)
    {    
        NSString* aStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    }
 }

I'm sure somebody could optimize this and make it nicer (this was used for a really simple PoC CLI tool)

like image 173
Oren Mazor Avatar answered May 27 '26 13:05

Oren Mazor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!