Wednesday, May 1, 2013

NSURL isEqual example ios


isEqual:

Returns a Boolean value that indicates whether the receiver and a given object are equal.
- (BOOL)isEqual:(id)anObject
Parameters of [NSURL isEqual]
anObject
The object to be compared to the receiver.
Return Value
YES if the receiver and anObject are equal, otherwise NO.
Discussion of [NSURL isEqual]
This method defines what it means for instances to be equal. Two NSURLs are considered equal if and only if they return identical values for both baseURL and relativeString.
Example of [NSURL isEqual]
// in NSURL+uriEquivalence.m

- (BOOL)isEquivalent:(NSURL *)aURL {

    if ([self isEqual:aURL]) return YES;
    if ([[self scheme] caseInsensitiveCompare:[aURL scheme]] != NSOrderedSame) return NO;
    if ([[self host] caseInsensitiveCompare:[aURL host]] != NSOrderedSame) return NO;

    // NSURL path is smart about trimming trailing slashes
    // note case-sensitivty here
    if ([[self path] compare:[aURL path]] != NSOrderedSame) return NO;

    // at this point, we've established that the urls are equivalent according to the rfc
    // insofar as scheme, host, and paths match

    // according to rfc2616, port's can weakly match if one is missing and the
    // other is default for the scheme, but for now, let's insist on an explicit match
    if ([[self port] compare:[aURL port]] != NSOrderedSame) return NO;

    if ([[self query] compare:[aURL query]] != NSOrderedSame) return NO;

    // for things like user/pw, fragment, etc., seems sensible to be
    // permissive about these.  (plus, I'm tired :-))
    return YES;
}

Example of [NSURL isEqual]

NSURL *url1 = [NSURL URLWithString:@"http://www.google.com"];
NSURL *url2 = [NSURL URLWithString:@"http://www.google.com"];
if ([url1 isEqual:url2]) {
......