Reading through the Objective C docs from Apple, I'm seeing this
neat-sounding thing called "categories", whereby one can extend existing
objects.
So, since I'm working with a wad of raw data (held by an NSMutableData
object) that needs to do many searches for "magic strings" in the wad to
locate the bits of data that it's actually interested in, I figured
"This is a good place to do some extending".
I came up with the following. Apologies for blown line-wrapping - pasted
directly from XCode, complete with my attempts at figuring out what's
going wrong via printf(). And yes, I'm aware it's crude, clunky, and has
its own built-in problem - as noted in the comment. If you'd be so kind
as to ignore those details and concentrate on helping me figure out why
it isn't working *AT ALL* (not even incorrectly...) I'd appreciate it:
-=-=-=- begin .h file
#im****t <Cocoa/Cocoa.h>
@[EMAIL PROTECTED]
NSMutableData (Search)
- (unsigned)findNSString:(NSString *)targetString
startingAt:(unsigned)Start;
@[EMAIL PROTECTED]
end .h file
-=-=-=-- begin .m file
#im****t "NSMutableDataSearch.h"
// Extending NSMutableData by adding a crude findString:startingAt
routine for NSMutableData objects.
// Takes an NSString (the target) and a starting location (offset from
the start of the NSMutableData buffer).
// Returns 0 = Not found (or found at location 0! Small problem here!)
or non-zero for found.
// Restrictions: Best bet is to avoid sending this message to an
instance unless it's known that the target data WILL NOT be found at
offset 0...
@[EMAIL PROTECTED]
NSMutableData (Search)
- (unsigned)findNSString:(NSString *)targetString
startingAt:(unsigned)Start
{
unsigned Max;
unsigned Index;
NSString *TempString;
char TempCString[255];
printf("Entering findNSString\n");
Max = [self length];
Index = 0;
printf("findNSString: TargetString = '%s', Max = %d\n", [targetString
getCString:&TempCString[0]], Max);
for (Index=Start;Index < Max;Index++)
{
TempString = [NSString stringWithCharacters:([self
mutableBytes]+Index) length:[targetString length]];
printf("Checking against '%s'...\n", [TempString
getCString:&TempCString[0]]);
if ([targetString isEqualTo:TempString])
{
printf("Got a match in findNSString!\n");
break;
}
else
{
printf("No match in findNSString\n");
}
}
if (Index < Max)
return Index;
else
return 0;
}
@[EMAIL PROTECTED]
end .m file
Looks OK "on paper", but doesn't operate *AT ALL* in reality - When
called, it does *ABSOLUTELY NOTHING*. It's as if it doesn't exist at all
- No printf output from any of the diagnostics, and the returned value
is zero every time. Trying to "step into" it in the debugger does
zip-ola - It's as if it's a null statement.
So it would appear that I'm doing something wrong, misunderstanding the
use of categories, or some of both.
Hints? Clarifications? Assistance?
--
Don Bruder - dakidd@[EMAIL PROTECTED]
- If your "From:" address isn't on my
whitelist,
or the subject of the message doesn't contain the exact text
"PopperAndShadow"
somewhere, any message sent to this address will go in the garbage without
my
ever knowing it arrived. Sorry... <http://www.sonic.net/~dakidd>
for more
info


|