问题描述
我不知道Objective-C / Foundation是否有任何特殊命令来从控制台读取用户输入。因为它有NSLog输出可能还有一些我可以使用而不是scanf命令。
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.
我需要读取一些数字(用户输入)到我的工具。什么是最好的方式来获得这些输入类型像double或int?
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?
推荐答案
我之前很厌烦,发现了这个问题'use scanf' 。因为我想看看是否可以做到没有陷入c,以下出现:
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];
}
}
我相信有人可以优化,更好(这用于一个非常简单的PoC CLI工具)
I'm sure somebody could optimize this and make it nicer (this was used for a really simple PoC CLI tool)
这篇关于从可可/基础工具控制台读取输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!