In the challenge at the end of Chapter 16 we’re asked to rewrite the “Coolness” project using NSString and NSLog. But I can’t seem to make it work. I also can’t find the documentation for readline().
NSLog(@"Who is cool?");
const char *cn = (const char *)readline(NULL);
NSString *name = [[NSString alloc] initWithUTF8String:cn];
NSLog(@"%@ is cool?", name);
Mine is a little different from everyones. I used functions that we really haven’t touched in the book, but I have used them in the past so I used them here.
[code]#import <Foundation/Foundation.h>
int main(int argc, const char * argv)
{ @autoreleasepool
{
NSLog(@“Who is cool?\n\n>”);
NSString *coolName =[[NSString alloc]init];
char coolNameArray[17];
scanf(“%s”,coolNameArray);
coolName = [NSString stringWithUTF8String:coolNameArray];
NSLog(@“"%@" is COOL.”,coolName);
}
return 0;
}[/code]
The output is:[quote]Who is cool?
Michael
“Michael” is COOL.
Program ended with exit code: 0[/quote]…any comments?
I could not get this to work. I wasn’t exactly sure of the specific challenge: I thought the idea was to convert the C-string to a NSString. But I didn’t know how to make a C-string in Obj-C. So I tried to use the stringWithUTF8String two different ways, with redline() and without. Apparently deadline() isn’t working with XCode6?
I get the following errors:
"_readline", referenced from:
_main in main.o
Symbol(s) not found for architecture x86_64
Linker command failed with exit code 1
I’m wondering if this is a depreciation? The documentation doesn’t even mention it. I’ve tried including the various libraries I’ve seen in the other posts with no success, but most of those a from last year, and I think using the older Xcode. The program accepts the formatting of the string, just not the readline() function.
I know this is a late reply but I just wanted to include it in case people were having the same problem like Haolejon . Make sure to select your project on the left where the project navigator is located at (This is the very most top folder). Then in the work space select Build Phases > collapse Link Binary with Libraries > click the + symbol and add libreadline.dylib .
This should solve those extra errors showing up; took me a few minutes myself to realize I forgot this step. I’m using Xcode 6.4 beta by the way