Scanf'ing for a string to search for within an array object...
Basically, I'd like to ask the user for what he'd like to search for in a particular array (an address book)...
and if he types part of a NAME (ie Mark), the program will search each object in the array (ie name part of address) for that NAME and print the object in the array if it contains it.
So I'm trying to use a scanf ("%@", &theName) where name is an NSString object.
I've tried using a rangeOfString when comparing the object to the string name, but they are "incompatible data types" and the program won't function...
What NS method can I use to search for string contents within an object in an array?
Thanks -
Mal
PS: I'm on chapter 15 exercise 2 of Kochan's Objective-C book - just trying to implement some user interaction to the exercise...
I thought you were using the built-in AddressBook. It appears you're using a custom class. We'll need to see the code for that, but I'm 100% sure the problem is in this line:
Code:
substr = [nextCard rangeOfString: theName];
You can't call rangeOfString on a non-NSString object. You need to be doing something more like:
Hmm, I did try that initially and had the same problem...I changed it back to that...here's everything I have and the program generated at the bottom...
What's the info you'd like to search for?2009-04-22 22:14:11.134 Address Book[198:10b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFString rangeOfString:options:range:locale:]: nil argument'
2009-04-22 22:14:11.136 Address Book[198:10b] Stack: (
2479034635,
2415971899,
2479034091,
2479034154,
2423308442,
2423344392,
10811,
9246,
8434
)
[Session started at 2009-04-22 22:14:11 -0400.]
Loading program into debugger…
GNU gdb 6.3.50-20050815 (Apple version gdb-962) (Sat Jul 26 08:14:40 UTC 2008)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i386-apple-darwin".Program loaded.
sharedlibrary apply-load-rules all
Attaching to program: `/Users/Mal/Desktop/Address Book/build/Debug/Address Book.app/Contents/MacOS/Address Book', process 198.
(gdb)
Most of this is from the textbook...the only part with errors are the parts I tried to create haha...could it be something to do with the myCard object? I feel like this is doing nothing...
ah ok that makes more sense-thanks...still getting same error for the program build. I changed the scanf so it's looking for %s instead of %@. There must be something wrong with the lookup method still since it's not finding entries.
I don't think you can scanf() into an NSString, and even if you can I think your code would be scanning it into a nil instance. Try scanning the user's input into a char array, then build an NSString from that if you need to. Something like this should do the trick:
Code:
char str[256]; // accepts a string up to 255 bytes long
scanf( "%s", str ); // get the user's input into 'str'
BTW, have you tried debugging this or are you just running it to see whether it crashes or not? I think that if you were to step through your code in the debugger, you'd quickly see what's working and what's causing problems.
I don't think you're actually passing in a valid NSString. Since you're using scanf, you're getting a cstring. You need to then create an NSString from that to pass into the lookup method. Try:
Code:
char name[100];
NSString *nombre;
printf("What's the info you'd like to search for?");
scanf("%s", &name);
nombre = [NSString stringWithCString: name encoding: NSASCIIStringEncoding];
[myBook lookup: nombre];
And use some NSLog statements to verify what you're getting at each stage.
char name[100];
NSString *nombre;
printf("What's the info you'd like to search for?");
scanf("%s", &name);
nombre = [NSString stringWithCString: name encoding: NSASCIIStringEncoding];
myCard = [myBook lookup: nombre];
if (myCard != nil)
[myCard print];
else
printf("Not found!");
Okay took your word and it works! So it looks like the problem was that I couldn't pass the scanf without the right encoding.. Thanks very much for your help.
Kalimba I haven't tried just a debug, I've always done build and run and looked for errors - thank for the advice. I just started with this a few weeks ago so there are probably quite a few obvious things I'm overlooking