从控制台获得Objective-C,简单字符串输入? [英] Objective-C, Simple String input from Console?
问题描述
老实说,我(a)使用关键字进行搜索,并且(b)在询问之前阅读了具有类似标题的问题".
I honestly did a) search using key words and b) read the 'questions with similar titles' before asking this.
我也试图使这个问题更简洁,但是在这种情况下,我很难做到这一点.如果您觉得这个问题太罗word了,我明白了.只是不要尝试回答.
Also I tried to make this question more concise, but I had a hard time doing that in this case. If you feel the question is too wordy, I get it. Just don't try to answer.
我正在尝试编写非常简单的Objective-C程序,以反映我的入门Java类中的基本分配.整个夏天,我读了一本Objective-C书,现在我想在Objective-C中做很多练习问题,同时我也要做Java练习问题.我避免使用Objective-C GUI环境,只想在一段时间内专注于使用该语言.关于如何弄清事情,我还有很多要学习的东西.
I'm trying to write very simple objective-C programs that mirror the basic assignments in my introductory java class. I worked through an objective-c book over the summer and now I want to do lots of practice problems in objective-c, at the same time as I do java practice problems. I'm avoiding the objective-c GUI environment and just want to focus on working with the language for awhile. I still have a lot to learn about how to figure things out.
我从Java作业中复制的程序是一种标准类型.我要求用户通过控制台输入数字和输入字符串.我可以通过使用扫描f在这里找到的示例从控制台获取数字输入. (我将在下面放置几行代码).但是我不确定如何获取控制台输入并将其存储在字符串(NSString)中.我正在尝试学习使用Apple文档,并找到了对扫描类型命令的引用,但是我无法弄清楚如何使用该命令.似乎很可能是
The program I'm duplicating from my java homework, is a standard type. I ask the user for number input and string input via the console. I was able to get numeric input from the console using an example I found here using scan f. (I will put the couple code lines below). But I'm unsure on how to get console input and store it in a string (NSString). I'm trying to learn to use the apple documentation and found a reference to a scan type command, but I cannot figure out how to USE the command. The one that seems likely is
scanCharactersFromSet:(NSCharacterSet )scanSet intoString:(NSString * )name;
scanCharactersFromSet:(NSCharacterSet )scanSet intoString:(NSString *)name;
这就是我的理解和工作
int age = 0;
NSLog (@"How old are y'all?");
scanf("%d", &age);
NSLog (@"\n Wow, you are %d !", age);
但是我不明白如何获取一个名为'name'的NSString.我认为应该将"name"作为指针,因为该类是NSString.
But I don't understand how to pickup an NSString called 'name'. I THINK I'm supposed to make my 'name'a pointer, because the class is NSString.
(顺便说一句,我确实尝试过使用scanf来提取字符串,但是编译器不喜欢我尝试将scanf与名称结合使用.它说我不应该使用'scanf',因为它期望使用另一种类型我不确定在哪里找到数据类型"i".我正在查看文本以寻找不同的想法.我猜想scanf与"scanfloat"有关,后者显然处理数字数据,所以这是没什么大不了的)
(BTW I did try using scanf to pickup the string, but the compiler doesn't like me trying to use scanf in conjunction with name. It says that I shouldn't be using 'scanf' because it's expecting a different kind of data. I'm not sure where I found the data type 'i'. I was looking through my text for different ideas. I'm guessing that scanf is related to 'scanfloat' which clearly deals with numeric data, so this is not a big surprise)
我意识到'scanf'是不正确的命令(而且我真的不明白为什么我什至无法在Apple文档中找到scanf-也许是C吗?)
I realize that 'scanf' isn't the right command (and I don't really get why I can't even find scanf in the apple documentation - maybe it's C?)
我猜想scanCharactersFromSet可能是正确的用法,但我只是不明白您如何弄清楚命令中的内容.我想我倾向于通过示例学习,但是我还没有找到示例.我想通过阅读文档找出如何正确学习的方法.但是我还没在那里.
I'm guessing that scanCharactersFromSet might be the right thing to use, but I just don't understand how you figure out what goes where in the command. I guess I tend to learn by example, and I haven't found an example. I'd like to figure out how to learn properly by reading the documentation. But I'm not there yet.
NSString* name ;
scanf("%i", &name);
//scanCharactersFromSet:(NSCharacterSet *)scanSet intoString:(NSString **)name;
...
我的书旨在使我进入gui环境,因此它不涉及输入.
My book is oriented towards moving me into a gui environment, so it doesn't deal with input.
感谢您提供给我的任何指导.
Thank you for any pointers you can give me.
Laurel
推荐答案
目标C的外观如下:
NSString *FNgetInput() {
@autoreleasepool {
return [[[NSString alloc] initWithData:[[NSFileHandle fileHandleWithStandardInput] availableData] encoding:NSUTF8StringEncoding] stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
}
}
这篇关于从控制台获得Objective-C,简单字符串输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!