4

Is there anyway we can take input from command line in Objective-C, the way we can take in C/C++?

int inputVariableName;

cin >> inputVariableName;

4 Answers 4

10
scanf("%i", userInput);

just as you would in c

To get Objective-C objects from the input, simple convert them after reading them in:

NSNumber * number = [nsnumber numberwithint:useriput];

Sign up to request clarification or add additional context in comments.

3 Comments

But i have another question, though it can solve my problem by now but if have an Object which has NSString or NSNumber objects within it, how i can take input for them? Since there is there no way scanf can handle these kind of objects?
I think i got answer to my second question on the following ur: stackoverflow.com/questions/493688/…
Shouldn't scanf("%i", userInput) read scanf("%i", &userInput)?
2

Sure. Compile your code as Objective-C++.

This is typically as easy as renaming the file from having a .m suffix to a .mm suffix.

Documentation is included with the Xcode tools as to the details of Objective-C++.

3 Comments

I don't know about Objective-C++ but is there any native way in Objective-C?
Objective-C++ is native Objective-C. But, yes, you can do this in pure Objective-C. See the NSFileHandle class.
@Dmitry What doesn't work? Compiling the code as C++ works fine has been supported for over a decade. And it includes the standard library.
2

As bbum mentioned, you can use NSFileHandle to get access to stdin. If you just want to read the command line arguments, you can get them from [[NSProcessInfo processInfo] arguments]. It's also worthwhile to know what else NSProcessInfo can tell you.

Comments

0

Command line input with spaces

    char textInput[1000];
    scanf("%[^\n]%*c", textInput);

    NSString* userInput = [NSString stringWithUTF8String:textInput];
    NSLog(@"\n%@\n%@", @"Hello, World.", userInput);

Input text with white spaces, 1000 is input character limit.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.