Challenge 1 solution

I didn’t write a program to test this since the challenge didn’t ask me to, but here are the methods I found…

To do a case-insensitive search, you could use rangeOfString:options: with NSCaseInsensitiveSearch specified as your option.

To return the actual portion of the string that was found, you could use getCharacters:range: using the NSRange struct returned by rangeOfString:options:.

The only thing I’m a little confused about is the buffer parameter in getCharacters:range:. The apple documentation gives this formula as the proper size for your buffer:

Is this as simple as calling malloc(aRange.length*sizeof(unichar))? If so, what kind of variable would you use for the buffer? The only example of malloc() in the book so far was put into a pointer for a float variable, but in that example, it was holding floats. :question: Bit confused…

Or perhaps I’m going about this all wrong and there’s a simpler method for returning a portion of a string.

Yes, unichar:

unichar *pu = (unichar *) malloc (aRange.length * sizeof (unichar));
assert (pu);

Ah, duh. That makes perfect sense.