• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Not understanding NSString's compare:options:range:locale: method
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Not understanding NSString's compare:options:range:locale: method


  • Subject: Re: Not understanding NSString's compare:options:range:locale: method
  • From: "Stephen J. Butler" <email@hidden>
  • Date: Fri, 8 Oct 2010 13:40:26 -0500

On Tue, Oct 5, 2010 at 4:48 AM, Derek Huby <email@hidden> 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?

>From the docs:

range
The range of the receiver over which to perform the comparison. The
range must not exceed the bounds of the receiver.

So you are comparing @"a" to @"ac" in the first case, and @"a" to
@"ab" in the second.
_______________________________________________

Cocoa-dev mailing list (email@hidden)

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:

This email sent to email@hidden

References: 
 >Not understanding NSString's compare:options:range:locale: method (From: Derek Huby <email@hidden>)

  • Prev by Date: Re: Problem using dictionary
  • Next by Date: Re: Not understanding NSString's compare:options:range:locale: method
  • Previous by thread: Not understanding NSString's compare:options:range:locale: method
  • Next by thread: Re: Not understanding NSString's compare:options:range:locale: method
  • Index(es):
    • Date
    • Thread