On Oct 5, 2010, at 02:48, Derek Huby wrote:

> This method isn't doing what I expect it to do (which probably means that I'm 
> expecting the wrong thing.)
> 
> //=====================================================
> NSRange range = NSMakeRange(0,1);
>       
> NSString *s1 = @"ab";
> NSString *s2 = @"ac";
>       
> //Just compare the first character
> NSComparisonResult result1 = [s1 compare:s2 options:NSLiteralSearch 
> range:range locale:[NSLocale currentLocale]];
> NSLog(@"Result 1 = %d", result1);
> 
> //Now swap the order of the strings
> NSComparisonResult result2 = [s2 compare:s1 options:NSLiteralSearch 
> range:range locale:[NSLocale currentLocale]];
> NSLog(@"Result 2 = %d", result2);
> //=====================================================
> 
> I get a result of -1 both times. Since we are only comparing the first 
> character, shouldn't the result be 0?

Your pairing of NSComparisonResult (which is typedef'ed to NSInteger) with 
string format specifier %d is incorrect and will fail under a 64-bit 
architecture (although I don't see how it would produce the -1 result -- but 
the above probably isn't your real code anyway, is it?). The generally 
recommended idiom is:

        NSLog(@"Result 1 = %ld", (long) result1);

FWIW


_______________________________________________

Cocoa-dev mailing list ([email protected])

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to