Read input from a cocoa/foundation tool console?

后端 未结 3 1650
面向向阳花
面向向阳花 2020-12-17 06:44

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

3条回答
  •  生来不讨喜
    2020-12-17 07:18

    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)

提交回复
热议问题