Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Stefan Mach
3,691 PointsHow can you pass a valyue to an NSString object via printf?
I am trying to create a command line tool that will loop thru a series of questions, each which assigns a value to an established variable, and then evaluates the group of variable values in an if/ if else statement, assigning a value to another variable if condition is met, after which it asks if you are done. If yes, then it breaks out of what is otherwise an infinite loop, wherein it repeatedly takes you through the same questions, until you say yes to the "are you done" statement. The purpose of the exercise is to create a command line tool that exhibits data persistence, via the use of a potentially infinite loop. The idea seemed great until I ran into what I believe is simply syntactical errors. Here is a short example of what will not work.
NSMutableString *color;
printf(@"What color:", &color);
I have tried all kinds of formatting after the ",", i.e., &color, &_color, &_color, etc.
It would seem like a perfectly natural thing to want to do. Create a variable of type NSMutableString, and then request a value for it via printf. I get that *color is a pointer, and so I am not asking for an address, but rather the actual string itself that should be housed at that address. One error message I keep getting has something to do with trying to pass an NSString to a variable of type const char, which it is not, so I cannot interpret the error.
Hope someone who gets objective-c can chime in and really explain this. It is tiring to get this far and get tripped up by what appears to be basic syntactical errors.
As an aside, team treehouse might want to add objective-c to the list below. It is after all something that is taught here. IOS is not the same as objective-c since Swift, yet seems to be the best choice for asking an objective-c related question.
2 Answers
Ben Shockley
6,094 PointsSo I am not that proficient in Objective-C, and I did a little digging and saw that scanf doesn't work with NSString. It only works with C string, which are basically an array of chars. So what you can do is read into a C String, or an array of chars with scanf, and then convert that to an NSString.. Here is a good answer about it on stackoverflow http://stackoverflow.com/questions/7266165/objective-c-simple-string-input-from-console
basically from what I can gather, scanf is not a very safe input method, because it can easily cause buffer overruns. Hope this helps a little bit, or at least puts you on the write path.
Ben Shockley
6,094 PointsI'm not sure if I fully understand what you mean. printf is an output function, it sends output to the console. To get input you would use something like scanf
so something like this
printf("Please input an integer value: ");
scanf("%d", &a);
printf("You entered: %d\n", a);
Stefan Mach
3,691 PointsI apologize. I got distracted and meant scanf. To state differently what I am trying to do: I am trying to use the input from a scanf as the value of an existing NSMuatableString object. In other words, I am trying to use scanf as a setter for this object. It seems like a natural thing to do, as I was taught about scanf as a means to set the value of a primitive, why not a string?
NSMutableString *name = [[NSMutableString alloc ]init stringWithString:@"input"];
scanf(@"What value do you want to pass to name: %@", &someSyntaxICantFigureOut);
It would seem that the %@ would be the correct token for passing an NSString but I also tried %s. The number of attempts I made at getting the syntax correct for what gets passed was numerous and kept throwing cryptic errors I do not understand.
Thanks for your help.
Stefan Mach
3,691 PointsStefan Mach
3,691 PointsThat was very helpful. Thanks.