diff --git a/KSFileWrapperExtensions.m b/KSFileWrapperExtensions.m index cfe74e9..b0928e0 100644 --- a/KSFileWrapperExtensions.m +++ b/KSFileWrapperExtensions.m @@ -33,18 +33,18 @@ @implementation NSFileWrapper (KSFileWrapperExtensions) - (NSString *)addFileWrapper:(NSFileWrapper *)wrapper subdirectory:(NSString *)subpath; { // Create any directories required by the subpath - NSArray *components = [subpath pathComponents]; + NSArray *components = subpath.pathComponents; NSFileWrapper *parentWrapper = self; - NSUInteger i, count = [components count]; + NSUInteger i, count = components.count; for (i = 0; i < count; i++) { - NSString *aComponent = [components objectAtIndex:i]; - NSFileWrapper *aWrapper = [[parentWrapper fileWrappers] objectForKey:aComponent]; + NSString *aComponent = components[i]; + NSFileWrapper *aWrapper = parentWrapper.fileWrappers[aComponent]; if (!aWrapper) { aWrapper = [[NSFileWrapper alloc] initDirectoryWithFileWrappers:nil]; - [aWrapper setPreferredFilename:aComponent]; + aWrapper.preferredFilename = aComponent; [parentWrapper addFileWrapper:aWrapper]; #if ! __has_feature(objc_arc) @@ -62,13 +62,13 @@ - (NSString *)addFileWrapper:(NSFileWrapper *)wrapper subdirectory:(NSString *)s - (void)ks_removeAllVisibleFileWrappers; { // Leopard had a bug where -fileWrappers returns its own backing store, which means it will mutate while enumerating - NSDictionary *wrappers = [[self fileWrappers] copy]; + NSDictionary *wrappers = [self.fileWrappers copy]; for (NSString *aFilename in wrappers) { if (![aFilename hasPrefix:@"."]) { - NSFileWrapper *aWrapper = [wrappers objectForKey:aFilename]; + NSFileWrapper *aWrapper = wrappers[aFilename]; [self removeFileWrapper:aWrapper]; } } @@ -89,7 +89,7 @@ - (NSFileWrapper *)ks_symbolicLinkDestinationFileWrapper; } #endif - NSFileWrapper *result = [[NSFileWrapper alloc] initWithURL:[self symbolicLinkDestinationURL] + NSFileWrapper *result = [[NSFileWrapper alloc] initWithURL:self.symbolicLinkDestinationURL options:0 error:NULL]; return [result autorelease]; @@ -104,7 +104,7 @@ - (BOOL)ks_writeToURL:(NSURL *)URL options:(NSFileWrapperWritingOptions)options - (BOOL)ks_writeToURL:(NSURL *)URL options:(NSFileWrapperWritingOptions)options originalParentDirectoryURL:(NSURL *)originalParentDirectory copyIfLinkingFails:(BOOL)fallbackToCopy error:(NSError **)outError; { - NSString *filename = [self filename]; + NSString *filename = self.filename; // The NSFileWrapper docs state: // @@ -115,10 +115,10 @@ - (BOOL)ks_writeToURL:(NSURL *)URL options:(NSFileWrapperWritingOptions)options // Does NSFileWrapper then sacrifice a little possible efficiency by only doing hardlinking if the filenames match too? i.e. that the filename being written to is the same as the source? If so, that would avoid this fasle positive. On the downside it would make adjusting filenames for existing files inside the package impossible to do efficiently, but that's pronbably not a big deal // I haven't devised a proper test of NSFileWrapper for this yet; just going to go ahead and do the filename check for now - NSURL *originalURL = ([filename isEqualToString:[URL lastPathComponent]] ? [originalParentDirectory URLByAppendingPathComponent:filename] : nil); + NSURL *originalURL = ([filename isEqualToString:URL.lastPathComponent] ? [originalParentDirectory URLByAppendingPathComponent:filename] : nil); // NSFileWrapper won't create hardlinks when writing an individual file, so we try to do so ourselves when reasonable, for performance reasons - if ([self isRegularFile]) + if (self.regularFile) { // If the file is already inside a doc, we favour hardlinking for performance if ([self matchesContentsOfURL:originalURL]) diff --git a/KSMailtoURLs.h b/KSMailtoURLs.h index b4e72d1..3307ae0 100644 --- a/KSMailtoURLs.h +++ b/KSMailtoURLs.h @@ -40,6 +40,6 @@ extern NSString *KSURLMailtoHeaderBody; + (NSURL *)ks_mailtoURLWithEmailAddress:(NSString *)address headerLines:(NSDictionary *)headers; // Nil if its not a mailto URL or has no header lines -- (NSDictionary *)ks_mailHeaderLines; +@property (nonatomic, readonly, copy) NSDictionary *ks_mailHeaderLines; @end diff --git a/KSMailtoURLs.m b/KSMailtoURLs.m index 752e250..efe2ddb 100644 --- a/KSMailtoURLs.m +++ b/KSMailtoURLs.m @@ -53,7 +53,7 @@ + (NSURL *)ks_mailtoURLWithEmailAddress:(NSString *)address headerLines:(NSDicti if (headers) { NSString *query = [self ks_queryWithParameters:headers]; - if ([query length]) + if (query.length) { string = [string stringByAppendingFormat:@"?%@", query]; } @@ -64,9 +64,9 @@ + (NSURL *)ks_mailtoURLWithEmailAddress:(NSString *)address headerLines:(NSDicti - (NSDictionary *)ks_mailHeaderLines; { - if (![[self scheme] isEqualToString:KSURLMailtoScheme]) return nil; + if (![self.scheme isEqualToString:KSURLMailtoScheme]) return nil; - NSString *urlString = [self absoluteString]; + NSString *urlString = self.absoluteString; NSRange queryIndicatorRange = [urlString rangeOfString:@"?"]; if (queryIndicatorRange.location != NSNotFound) diff --git a/KSPathUtilities.h b/KSPathUtilities.h index fad9efd..1a674c8 100644 --- a/KSPathUtilities.h +++ b/KSPathUtilities.h @@ -36,7 +36,7 @@ // Given a path "foo/bar.png", adjusts it to be "foo/bar-2.png". Calling -ks_stringByIncrementingPath on that string will then give "foo/bar-3.png" and so on // More at http://www.mikeabdullah.net/incrementing-paths.html -- (NSString *)ks_stringByIncrementingPath; +@property (nonatomic, readonly, copy) NSString *ks_stringByIncrementingPath; // like -stringByAppendingString: but inserts the suffix string in front of path extension if there is one. e.g. [@"foo.png" ks_stringWithPathSuffix:@"-2"] = @"foo-2.png" - (NSString *)ks_stringWithPathSuffix:(NSString *)aString; @@ -62,7 +62,7 @@ #pragma mark POSIX Paths // NSString has built-in methods for standardizing a path, but they consult the filesystem for symlinks. This method only looks at the path itself -- (NSString *)ks_standardizedPOSIXPath; +@property (nonatomic, readonly, copy) NSString *ks_standardizedPOSIXPath; // Like -isEqualToString: but ignores trailing slashes - (BOOL)ks_isEqualToPOSIXPath:(NSString *)otherPath; diff --git a/KSPathUtilities.m b/KSPathUtilities.m index dbedf5e..bf3af21 100644 --- a/KSPathUtilities.m +++ b/KSPathUtilities.m @@ -34,7 +34,7 @@ @interface KSIncrementedPath : NSString NSUInteger _suffix; } -- (id)initWithBasePath:(NSString *)basePath suffix:(NSUInteger)suffix; +- (instancetype)initWithBasePath:(NSString *)basePath suffix:(NSUInteger)suffix; @end @@ -49,7 +49,7 @@ + (NSString *)ks_stringWithPath:(NSString *)path relativeToDirectory:(NSString * { NSParameterAssert(path); - if (!directory || [path isAbsolutePath]) return path; + if (!directory || path.absolutePath) return path; NSString *result = [directory stringByAppendingPathComponent:path]; return result; @@ -59,11 +59,10 @@ + (NSString *)ks_stringWithPath:(NSString *)path relativeToDirectory:(NSString * - (NSString *)ks_stringWithPathSuffix:(NSString *)aString; { - NSString *extension = [self pathExtension]; - if ([extension length]) + NSString *extension = self.pathExtension; + if (extension.length) { - NSString *result = [[[self - stringByDeletingPathExtension] + NSString *result = [[self.stringByDeletingPathExtension stringByAppendingString:aString] stringByAppendingPathExtension:extension]; return result; @@ -154,8 +153,8 @@ - (void)ks_enumeratePathComponentsInRange:(NSRange)range - (BOOL)ks_isEqualToPath:(NSString *)aPath; { - NSString *myPath = [self stringByStandardizingPath]; - aPath = [aPath stringByStandardizingPath]; + NSString *myPath = self.stringByStandardizingPath; + aPath = aPath.stringByStandardizingPath; BOOL result = ([myPath caseInsensitiveCompare:aPath] == NSOrderedSame); return result; @@ -193,14 +192,14 @@ - (BOOL)ks_isSubpathOfPath:(NSString *)aPath - (NSString *)ks_pathRelativeToDirectory:(NSString *)dirPath { - if ([dirPath isAbsolutePath]) + if (dirPath.absolutePath) { - if (![self isAbsolutePath]) return self; // job's already done for us! + if (!self.absolutePath) return self; // job's already done for us! } else { // An absolute path relative to a relative path is always going to be self - if ([self isAbsolutePath]) return self; + if (self.absolutePath) return self; // But comparing two relative paths is a bit of an edge case. Internally, pretend they're absolute dirPath = (dirPath ? [@"/" stringByAppendingString:dirPath] : @"/"); @@ -209,7 +208,7 @@ - (NSString *)ks_pathRelativeToDirectory:(NSString *)dirPath // Determine the common ancestor directory containing both paths - __block NSRange mySearchRange = NSMakeRange(1, [self length] - 1); + __block NSRange mySearchRange = NSMakeRange(1, self.length - 1); NSMutableString *result = [NSMutableString string]; @@ -220,7 +219,7 @@ - (NSString *)ks_pathRelativeToDirectory:(NSString *)dirPath } else { - __block NSRange dirSearchRange = NSMakeRange(1, [dirPath length] - 1); + __block NSRange dirSearchRange = NSMakeRange(1, dirPath.length - 1); [self ks_enumeratePathComponentsInRange:mySearchRange options:0 usingBlock:^(NSString *myComponent, NSRange myRange, NSRange enclosingRange, BOOL *stopOuter) { @@ -254,7 +253,7 @@ - (NSString *)ks_pathRelativeToDirectory:(NSString *)dirPath if (range.length == 2) NSAssert([dirPath compare:@".." options:NSLiteralSearch range:range] != NSOrderedSame, @".. unsupported: %@", dirPath); - if ([result length]) [result appendString:@"/"]; + if (result.length) [result appendString:@"/"]; [result appendString:@".."]; }]; } @@ -268,15 +267,15 @@ - (NSString *)ks_pathRelativeToDirectory:(NSString *)dirPath pathRelativeToCommonDir = [pathRelativeToCommonDir substringFromIndex:1]; } - if ([pathRelativeToCommonDir length]) + if (pathRelativeToCommonDir.length) { - if ([result length]) [result appendString:@"/"]; + if (result.length) [result appendString:@"/"]; [result appendString:pathRelativeToCommonDir]; } // Were the paths found to be equal? - if ([result length] == 0) + if (result.length == 0) { [result appendString:@"."]; [result appendString:[self substringWithRange:mySearchRange]]; // match original's oddities @@ -295,9 +294,9 @@ - (NSString *)ks_standardizedPOSIXPath { NSString *result = self; - while ([result length] > 1 && [result hasSuffix:@"/"]) // Stops @"/" being altered + while (result.length > 1 && [result hasSuffix:@"/"]) // Stops @"/" being altered { - result = [result substringToIndex:([result length] - 1)]; + result = [result substringToIndex:(result.length - 1)]; } return result; @@ -320,7 +319,7 @@ - (BOOL)ks_isEqualToPOSIXPath:(NSString *)otherPath @implementation KSIncrementedPath -- (id)initWithBasePath:(NSString *)basePath suffix:(NSUInteger)suffix; +- (instancetype)initWithBasePath:(NSString *)basePath suffix:(NSUInteger)suffix; { NSParameterAssert(suffix >= 2); @@ -340,7 +339,7 @@ - (NSString *)ks_stringByIncrementingPath; #pragma mark NSString Primitives -- (NSUInteger)length; { return [_storage length]; } +- (NSUInteger)length; { return _storage.length; } - (unichar)characterAtIndex:(NSUInteger)index; { return [_storage characterAtIndex:index]; } @end diff --git a/KSURLComponents.h b/KSURLComponents.h index 5e91041..279bc62 100644 --- a/KSURLComponents.h +++ b/KSURLComponents.h @@ -52,7 +52,7 @@ @param url The URL whose components you want. @param resolve Whether to resolve relative URLs before retrieving components */ -- (id)initWithURL:(NSURL *)url resolvingAgainstBaseURL:(BOOL)resolve __attribute((nonnull)); +- (instancetype)initWithURL:(NSURL *)url resolvingAgainstBaseURL:(BOOL)resolve __attribute((nonnull)); /** Initializes and returns a newly created `KSURLComponents` with the components of a URL. @@ -67,21 +67,21 @@ @param url The URL whose components you want. @param resolve Whether to resolve relative URLs before retrieving components */ -+ (id)componentsWithURL:(NSURL *)url resolvingAgainstBaseURL:(BOOL)resolve __attribute((nonnull)); ++ (instancetype)componentsWithURL:(NSURL *)url resolvingAgainstBaseURL:(BOOL)resolve __attribute((nonnull)); /** Initialize a `KSURLComponents` with a URL string. If the URLString is malformed, `nil` is returned. */ -- (id)initWithString:(NSString *)URLString; +- (instancetype)initWithString:(NSString *)URLString; /** Initializes and returns a newly created `KSURLComponents` with a URL string. If the URLString is malformed, `nil` is returned. */ -+ (id)componentsWithString:(NSString *)URLString; ++ (instancetype)componentsWithString:(NSString *)URLString; /** Returns a URL created from the `KSURLComponents`. @@ -92,7 +92,7 @@ and has a path component, the path component must not start with `//`. If those requirements are not met, `nil` is returned. */ -- (NSURL *)URL; +@property (nonatomic, readonly, copy) NSURL *URL; /** Returns a URL created from the `KSURLComponents` relative to a base URL. diff --git a/KSURLComponents.m b/KSURLComponents.m index c0e4af6..7211639 100644 --- a/KSURLComponents.m +++ b/KSURLComponents.m @@ -41,11 +41,11 @@ @implementation KSURLComponents #pragma mark Lifecycle -- (id)initWithURL:(NSURL *)url resolvingAgainstBaseURL:(BOOL)resolve; +- (instancetype)initWithURL:(NSURL *)url resolvingAgainstBaseURL:(BOOL)resolve; { if (!url) [NSException raise:NSInvalidArgumentException format:@"-[KSURLComponents %@] requires a non-nil URL", NSStringFromSelector(_cmd)]; - if (resolve) url = [url absoluteURL]; + if (resolve) url = url.absoluteURL; CFStringRef urlString = CFURLGetString((CFURLRef)url); BOOL fudgedParsing = NO; @@ -178,12 +178,12 @@ - (id)initWithURL:(NSURL *)url resolvingAgainstBaseURL:(BOOL)resolve; return self; } -+ (id)componentsWithURL:(NSURL *)url resolvingAgainstBaseURL:(BOOL)resolve; ++ (instancetype)componentsWithURL:(NSURL *)url resolvingAgainstBaseURL:(BOOL)resolve; { return [[[self alloc] initWithURL:url resolvingAgainstBaseURL:resolve] autorelease]; } -- (id)initWithString:(NSString *)URLString; +- (instancetype)initWithString:(NSString *)URLString; { NSURL *url = [[NSURL alloc] initWithString:URLString]; if (!url) @@ -198,7 +198,7 @@ - (id)initWithString:(NSString *)URLString; return self; } -+ (id)componentsWithString:(NSString *)URLString; ++ (instancetype)componentsWithString:(NSString *)URLString; { return [[[self alloc] initWithString:URLString] autorelease]; } @@ -236,7 +236,7 @@ - (NSURL *)URLRelativeToURL:(NSURL *)baseURL; // If the KSURLComponents has an authority component (user, password, host or port) and a path component, then the path must either begin with "/" or be an empty string. if (hasAuthorityComponent && - !(path.length == 0 || [path isAbsolutePath])) + !(path.length == 0 || path.absolutePath)) { return nil; } @@ -507,7 +507,7 @@ - (BOOL)isEqual:(id)object; if (myHost != otherHost && ![myHost isEqualToString:otherHost]) return NO; NSNumber *myPort = self.port; - NSNumber *otherPort = [(KSURLComponents *)object port]; + NSNumber *otherPort = ((KSURLComponents *)object).port; if (myPort != otherPort && ![myPort isEqualToNumber:otherPort]) return NO; NSString *myPath = self.percentEncodedPath; @@ -553,7 +553,7 @@ - (id)copyWithZone:(NSZone *)zone; - (NSString *)description; { - return [[super description] stringByAppendingFormat: + return [super.description stringByAppendingFormat: @" {scheme = %@, user = %@, password = %@, host = %@, port = %@, path = %@, query = %@, fragment = %@}", self.scheme, self.percentEncodedUser, diff --git a/KSURLFormatter.m b/KSURLFormatter.m index 34932de..2669d50 100644 --- a/KSURLFormatter.m +++ b/KSURLFormatter.m @@ -51,7 +51,7 @@ + (NSURL *)URLFromString:(NSString *)string; NSRange range = [(__bridge NSString *)escapedString rangeOfString:@"#"]; if (range.location != NSNotFound) { - NSRange postFragmentRange = NSMakeRange(NSMaxRange(range), [(__bridge NSString *)escapedString length] - NSMaxRange(range)); + NSRange postFragmentRange = NSMakeRange(NSMaxRange(range), ((__bridge NSString *)escapedString).length - NSMaxRange(range)); range = [(__bridge NSString *)escapedString rangeOfString:@"#" options:0 range:postFragmentRange]; if (range.location != NSNotFound) @@ -82,13 +82,13 @@ + (BOOL)isValidEmailAddress:(NSString *)address; BOOL OK = (whereAt.location != NSNotFound && whereAt.location >= 1); // make sure there is an @ and it's not the first character if (OK) { - NSInteger len = [address length] - whereAt.location; + NSInteger len = address.length - whereAt.location; NSRange whereDot = [address rangeOfString:@"." options:0 range:NSMakeRange(whereAt.location, len)]; OK = (whereDot.location != NSNotFound); if (OK) { // make sure there is room between the @ and the . - OK = (whereDot.location - whereAt.location >= 2) && ([address length] - whereDot.location >= 3); + OK = (whereDot.location - whereAt.location >= 2) && (address.length - whereDot.location >= 3); } } return OK; @@ -103,7 +103,7 @@ + (BOOL)isLikelyEmailAddress:(NSString *)address; if (URL) { // Account for strings like http://example.com/@foo which seem to be technically valid as an email address, but unlikely to be one - if ([URL scheme]) result = NO; + if (URL.scheme) result = NO; } } @@ -112,7 +112,7 @@ + (BOOL)isLikelyEmailAddress:(NSString *)address; #pragma mark Init & Dealloc -- (id)init +- (instancetype)init { if (self = [super init]) { @@ -152,16 +152,16 @@ - (NSString *)stringForObjectValue:(id)anObject NSURL *URL = anObject; NSString *result; - if ([self useDisplayNameForFileURLs] && [anObject isFileURL]) + if (self.useDisplayNameForFileURLs && [anObject isFileURL]) { - result = [[NSFileManager defaultManager] displayNameAtPath:[URL path]]; + result = [[NSFileManager defaultManager] displayNameAtPath:URL.path]; } else { - result = [URL absoluteString]; + result = URL.absoluteString; // Append trailing slash if needed - if ([URL ks_hasNetworkLocation] && [[URL path] isEqualToString:@""]) + if ([URL ks_hasNetworkLocation] && [URL.path isEqualToString:@""]) { result = [result stringByAppendingString:@"/"]; } @@ -190,15 +190,15 @@ + (NSURL *)URLFromString:(NSString *)string defaultScheme:(NSString *)fallbackSc // Allow fragment links as-is - if ([string hasPrefix:@"#"] && ([string length] > [@"#" length] && result)) + if ([string hasPrefix:@"#"] && (string.length > (@"#").length && result)) { return result; } // This is probably a really naive check - if ((![result scheme] && ![string hasPrefix:@"/"]) || // e.g. foo - (![result host])) // e.g. foo.com:8888 + if ((!result.scheme && ![string hasPrefix:@"/"]) || // e.g. foo + (!result.host)) // e.g. foo.com:8888 { // if it looks like an email address, use mailto: if ([self isLikelyEmailAddress:string]) @@ -207,9 +207,9 @@ + (NSURL *)URLFromString:(NSString *)string defaultScheme:(NSString *)fallbackSc } else { - NSString *scheme = [result scheme]; + NSString *scheme = result.scheme; - if (!scheme || (![result isFileURL] && + if (!scheme || (!result.fileURL && [scheme caseInsensitiveCompare:@"javascript"] != NSOrderedSame)) { result = [self URLFromString:[NSString stringWithFormat: @@ -223,9 +223,9 @@ + (NSURL *)URLFromString:(NSString *)string defaultScheme:(NSString *)fallbackSc // Append a trailing slash if needed - if ([result ks_hasNetworkLocation] && [[result path] isEqualToString:@""]) + if ([result ks_hasNetworkLocation] && [result.path isEqualToString:@""]) { - result = [[NSURL URLWithString:@"/" relativeToURL:result] absoluteURL]; + result = [NSURL URLWithString:@"/" relativeToURL:result].absoluteURL; } return result; @@ -236,9 +236,9 @@ - (BOOL)getObjectValue:(id *)anObject forString:(NSString *)string errorDescript if (anObject) { NSURL *URL = [self URLFromString:string]; - if ([self generatesURLStrings]) + if (self.generatesURLStrings) { - *anObject = [URL absoluteString]; + *anObject = URL.absoluteString; } else { @@ -253,20 +253,20 @@ - (NSURL *)URLFromString:(NSString *)string; { NSURL *result = nil; - if ([string length] > 0) + if (string.length > 0) { - result = [KSURLFormatter URLFromString:string defaultScheme:[self defaultScheme]]; + result = [KSURLFormatter URLFromString:string defaultScheme:self.defaultScheme]; // Does the URL have no useful resource specified? If so, generate nil URL - if (result && ![result isFileURL]) + if (result && !result.fileURL) { - NSString *scheme = [result scheme]; + NSString *scheme = result.scheme; if ([scheme caseInsensitiveCompare:@"mailto"] != NSOrderedSame && [scheme caseInsensitiveCompare:@"javascript"] != NSOrderedSame) { - NSString *resource = [result resourceSpecifier]; - if ([resource length] == 0 || + NSString *resource = result.resourceSpecifier; + if (resource.length == 0 || [resource isEqualToString:@"/"] || [resource isEqualToString:@"//"]) { @@ -278,9 +278,9 @@ - (NSURL *)URLFromString:(NSString *)string; // URLs should also really have a host and a path if (result) { - NSString *host = [result host]; - NSString *path = [result path]; - if (!host && !path && ![result fragment]) + NSString *host = result.host; + NSString *path = result.path; + if (!host && !path && !result.fragment) { result = nil; } @@ -290,26 +290,26 @@ - (NSURL *)URLFromString:(NSString *)string; // Did the user not enter a top-level domain? We'll guess for them - if (result && ![result isFileURL] && [self fallbackTopLevelDomain]) + if (result && !result.fileURL && self.fallbackTopLevelDomain) { - if ([[result ks_domains] count] == 1) + if ([result ks_domains].count == 1) { NSString *urlString = [NSString stringWithFormat: @"%@://%@.%@/", - [result scheme], - [result host], - [self fallbackTopLevelDomain]]; + result.scheme, + result.host, + self.fallbackTopLevelDomain]; result = [self URLFromString:urlString]; } } // Make sure the scheme is allowed - NSArray *allowedSchemes = [self allowedSchemes]; + NSArray *allowedSchemes = self.allowedSchemes; if (allowedSchemes) { - NSString *scheme = [result scheme]; - if (scheme && ![allowedSchemes containsObject:[result scheme]]) + NSString *scheme = result.scheme; + if (scheme && ![allowedSchemes containsObject:result.scheme]) { // Look for the best matching scheme based on the assumption that the URL was simply pasted in missing a small number of initial characters for (NSString *aScheme in allowedSchemes) @@ -322,7 +322,7 @@ - (NSURL *)URLFromString:(NSString *)string; } // Nothing matched? Alright, go for the first one then - result = [result ks_URLWithScheme:[allowedSchemes objectAtIndex:0]]; + result = [result ks_URLWithScheme:allowedSchemes[0]]; } } } diff --git a/KSURLNormalization.h b/KSURLNormalization.h index b120a42..de43d3d 100644 --- a/KSURLNormalization.h +++ b/KSURLNormalization.h @@ -78,7 +78,7 @@ @interface NSURL (KSURLNormalization) -- (NSURL *)ks_normalizedURL; +@property (nonatomic, readonly, copy) NSURL *ks_normalizedURL; @end diff --git a/KSURLNormalization.m b/KSURLNormalization.m index bba7c66..c722687 100644 --- a/KSURLNormalization.m +++ b/KSURLNormalization.m @@ -49,18 +49,18 @@ @interface NSURL (KSURLNormalizationPrivate) - (NSRange)ks_replacementRangeOfURLPart:(ks_URLPart)anURLPart; #pragma mark Normalizations that preserve semantics. -- (NSURL *)ks_URLByLowercasingSchemeAndHost; -- (NSURL *)ks_URLByUppercasingEscapes; -- (NSURL *)ks_URLByUnescapingUnreservedCharactersInPath; -- (NSURL *)ks_URLByAddingTrailingSlashToDirectory; -- (NSURL *)ks_URLByRemovingDefaultPort; -- (NSURL *)ks_URLByRemovingDotSegments; +@property (nonatomic, readonly, copy) NSURL *ks_URLByLowercasingSchemeAndHost; +@property (nonatomic, readonly, copy) NSURL *ks_URLByUppercasingEscapes; +@property (nonatomic, readonly, copy) NSURL *ks_URLByUnescapingUnreservedCharactersInPath; +@property (nonatomic, readonly, copy) NSURL *ks_URLByAddingTrailingSlashToDirectory; +@property (nonatomic, readonly, copy) NSURL *ks_URLByRemovingDefaultPort; +@property (nonatomic, readonly, copy) NSURL *ks_URLByRemovingDotSegments; #pragma mark Normalizations that change semantics. -- (NSURL *)ks_URLByRemovingDirectoryIndex; -- (NSURL *)ks_URLByRemovingFragment; +@property (nonatomic, readonly, copy) NSURL *ks_URLByRemovingDirectoryIndex; +@property (nonatomic, readonly, copy) NSURL *ks_URLByRemovingFragment; //- (NSURL *)ks_URLByReplacingIPWithHost; -- (NSURL *)ks_URLByRemovingDuplicateSlashes; +@property (nonatomic, readonly, copy) NSURL *ks_URLByRemovingDuplicateSlashes; //- (NSURL *)ks_URLByRemovingEmptyQuery @end @@ -82,7 +82,7 @@ - (NSURL *)ks_normalizedURL norm = [norm ks_URLByUnescapingUnreservedCharactersInPath]; norm = [norm ks_URLByAddingTrailingSlashToDirectory]; norm = [norm ks_URLByRemovingDefaultPort]; - if (![norm isFileURL]) + if (!norm.fileURL) { norm = [norm ks_URLByRemovingDirectoryIndex]; } @@ -100,90 +100,90 @@ - (NSRange)ks_replacementRangeOfURLPart:(ks_URLPart)anURLPart // Determine correct range for replacing the specified URL part INCLUDING DELIMITERS. // Note that if the URL part is not found, the range length is 0, but the range location is NOT NSNotFound, but rather the location that the indicated URL part could be inserted. // NOTE: Duplicate "/" must be removed from URL before finding the path's (and later URL element's) range is guaranteed correct! - NSString *scheme = [[self scheme] lowercaseString]; + NSString *scheme = self.scheme.lowercaseString; NSString *templateSchemePart = @"://"; NSString *realSchemePart = @""; - NSString *user = [self user]; - NSString *password = [self password]; + NSString *user = self.user; + NSString *password = self.password; NSString *passwordDelimiter = @":"; NSString *userDelimiter = @"@"; - NSString *host = [[self host] lowercaseString]; + NSString *host = self.host.lowercaseString; NSString *port = nil; - if ([[self port] intValue] > 0) + if (self.port.intValue > 0) { // lurking bug: if port has leading 0 - port = [NSString stringWithFormat:@"%d", [[self port] intValue]]; + port = [NSString stringWithFormat:@"%d", self.port.intValue]; } NSString *portDelimiter = @":"; - NSString *path = [self path]; // updated later - NSString *parameterString = [self parameterString]; + NSString *path = self.path; // updated later + NSString *parameterString = self.parameterString; NSString *parameterDelimiter = @";"; - NSString *query = [self query]; + NSString *query = self.query; NSString *queryDelimiter = @"?"; - NSString *fragment = [self fragment]; + NSString *fragment = self.fragment; NSString *fragmentDelimiter = @"#"; NSRange rPart = (NSRange){0,0}; if (anURLPart >= ks_URLPartScheme) { rPart.location += 0; - rPart.length = [scheme length]; + rPart.length = scheme.length; } if (anURLPart > ks_URLPartScheme) { - rPart.location += [scheme length]; + rPart.location += scheme.length; - NSRange testForSchemePart = NSMakeRange(rPart.location, [templateSchemePart length]); - NSRange searchForSchemePart = [[self absoluteString] rangeOfString:templateSchemePart]; + NSRange testForSchemePart = NSMakeRange(rPart.location, templateSchemePart.length); + NSRange searchForSchemePart = [self.absoluteString rangeOfString:templateSchemePart]; if (NSEqualRanges(testForSchemePart, searchForSchemePart)) { realSchemePart = templateSchemePart; - rPart.length = [realSchemePart length]; + rPart.length = realSchemePart.length; } else { realSchemePart = @""; - rPart.length = [realSchemePart length]; + rPart.length = realSchemePart.length; } } if (anURLPart >= ks_URLPartUserAndPassword || anURLPart == ks_URLPartPath) { - rPart.location += [realSchemePart length]; + rPart.location += realSchemePart.length; rPart.length = 0; - if (user && [user length]) + if (user && user.length) { - rPart.length += ([user length] + [userDelimiter length]); + rPart.length += (user.length + userDelimiter.length); } - if (password && [password length]) + if (password && password.length) { - rPart.length += ([password length] + [passwordDelimiter length]); + rPart.length += (password.length + passwordDelimiter.length); } } if (anURLPart >= ks_URLPartHost || anURLPart == ks_URLPartPath) { - if (user && [user length]) + if (user && user.length) { - rPart.location += ([user length] + [userDelimiter length]); + rPart.location += (user.length + userDelimiter.length); } - if (password && [password length]) + if (password && password.length) { - rPart.location += ([password length] + [passwordDelimiter length]); + rPart.location += (password.length + passwordDelimiter.length); } - rPart.length = [host length]; + rPart.length = host.length; } if (anURLPart >= ks_URLPartPort || anURLPart == ks_URLPartPath) { - rPart.location += [host length]; + rPart.location += host.length; rPart.length = 0; - if (port && [port length]) + if (port && port.length) { - rPart.length = [port length] + [portDelimiter length]; + rPart.length = port.length + portDelimiter.length; } } if (anURLPart >= ks_URLPartParameterString || anURLPart == ks_URLPartPath) { - if (port && [port length]) + if (port && port.length) { - rPart.location += ([port length] + [portDelimiter length]); + rPart.location += (port.length + portDelimiter.length); } // NOTE: Duplicate "/" must be removed from URL before finding the path's (and later URL element's) range is guaranteed correct! @@ -191,56 +191,56 @@ - (NSRange)ks_replacementRangeOfURLPart:(ks_URLPart)anURLPart // There are 2 problems with NSURL's path method: first it drops trailing "/" characters, second it percent-unescapes the path. // We have to work around those issues here. // Get the length of un-escaped path by comparing the length of complete URL to URL with path stripped. - NSInteger urlLength = [[self absoluteString] length]; + NSInteger urlLength = self.absoluteString.length; NSURL *rootPathURL = [[self copy] autorelease]; - NSArray *pathComponents = [rootPathURL pathComponents]; - NSInteger cnt = [pathComponents count]; + NSArray *pathComponents = rootPathURL.pathComponents; + NSInteger cnt = pathComponents.count; for (NSInteger i = cnt; i > 1; i--) { - rootPathURL = [rootPathURL URLByDeletingLastPathComponent]; + rootPathURL = rootPathURL.URLByDeletingLastPathComponent; } // At this point, the path of rootPathURL is "/" or "". - NSInteger rootPathURLLength = [[rootPathURL absoluteString] length]; - rPart.length = urlLength - rootPathURLLength + [[rootPathURL path] length]; + NSInteger rootPathURLLength = rootPathURL.absoluteString.length; + rPart.length = urlLength - rootPathURLLength + rootPathURL.path.length; // Replace path with corrected version. - path = [[self absoluteString] substringWithRange:rPart]; + path = [self.absoluteString substringWithRange:rPart]; } if (anURLPart >= ks_URLPartParameterString) { - rPart.location += [path length]; + rPart.location += path.length; rPart.length = 0; - if (parameterString && [parameterString length]) + if (parameterString && parameterString.length) { - rPart.length = [parameterString length] + [parameterDelimiter length]; + rPart.length = parameterString.length + parameterDelimiter.length; } } if (anURLPart >= ks_URLPartQuery) { - if (parameterString && [parameterString length]) + if (parameterString && parameterString.length) { - rPart.location += ([parameterString length] + [parameterDelimiter length]); + rPart.location += (parameterString.length + parameterDelimiter.length); } rPart.length = 0; - if (query && [query length]) + if (query && query.length) { - rPart.length = [query length] + [queryDelimiter length]; + rPart.length = query.length + queryDelimiter.length; } } if (anURLPart >= ks_URLPartFragment) { - if (query && [query length]) + if (query && query.length) { - rPart.location += ([query length] + [queryDelimiter length]); + rPart.location += (query.length + queryDelimiter.length); } rPart.length = 0; - if (fragment && [fragment length]) + if (fragment && fragment.length) { - rPart.length = [fragment length] + [fragmentDelimiter length]; + rPart.length = fragment.length + fragmentDelimiter.length; } else { - NSString *abs = [self absoluteString]; - if (rPart.location < [abs length]) + NSString *abs = self.absoluteString; + if (rPart.location < abs.length) { NSString *maybeFragmentDelimiter = [abs substringWithRange:NSMakeRange(rPart.location, 1)]; if ([maybeFragmentDelimiter isEqualToString:fragmentDelimiter]) @@ -261,8 +261,8 @@ - (NSRange)ks_replacementRangeOfURLPart:(ks_URLPart)anURLPart - (NSURL *)ks_URLByLowercasingSchemeAndHost { // If both scheme and host are already lowercase, nothing need be done - NSString *scheme = [self scheme]; - NSString *host = [self host]; + NSString *scheme = self.scheme; + NSString *host = self.host; NSCharacterSet *uppercase = [NSCharacterSet uppercaseLetterCharacterSet]; @@ -275,14 +275,14 @@ - (NSURL *)ks_URLByLowercasingSchemeAndHost NSRange rScheme = [self ks_replacementRangeOfURLPart:ks_URLPartScheme]; NSRange rHost = [self ks_replacementRangeOfURLPart:ks_URLPartHost]; - NSString *abs = [self absoluteString]; - if ([scheme length]) + NSString *abs = self.absoluteString; + if (scheme.length) { - abs = [abs stringByReplacingCharactersInRange:rScheme withString:[scheme lowercaseString]]; + abs = [abs stringByReplacingCharactersInRange:rScheme withString:scheme.lowercaseString]; } - if ([host length]) + if (host.length) { - abs = [abs stringByReplacingCharactersInRange:rHost withString:[host lowercaseString]]; + abs = [abs stringByReplacingCharactersInRange:rHost withString:host.lowercaseString]; } NSURL *correctedURL = [NSURL URLWithString:abs]; return correctedURL; @@ -294,21 +294,21 @@ - (NSURL *)ks_URLByUppercasingEscapes { // http://en.wikipedia.org/wiki/Percent_encoding - NSString *anURLStr = [self absoluteString]; - NSRange rSearch = NSMakeRange(0, [anURLStr length]); + NSString *anURLStr = self.absoluteString; + NSRange rSearch = NSMakeRange(0, anURLStr.length); while (rSearch.length > 0) { NSRange rFound = [anURLStr rangeOfString:@"%" options:0 range:rSearch]; if (rFound.location != NSNotFound) { rFound.length += 2; - if (rFound.location + rFound.length < [anURLStr length]) + if (rFound.location + rFound.length < anURLStr.length) { - NSString *escapeUpper = [[anURLStr substringWithRange:rFound] uppercaseString]; + NSString *escapeUpper = [anURLStr substringWithRange:rFound].uppercaseString; anURLStr = [anURLStr stringByReplacingCharactersInRange:rFound withString:escapeUpper]; } rSearch.location = rFound.location + rFound.length; - rSearch.length = [anURLStr length] - rSearch.location; + rSearch.length = anURLStr.length - rSearch.location; } else { @@ -324,7 +324,7 @@ - (NSURL *)ks_URLByUppercasingEscapes // Decode percent-encoded octets of unreserved characters. - (NSURL *)ks_URLByUnescapingUnreservedCharactersInPath { - NSString *abs = [self absoluteString]; + NSString *abs = self.absoluteString; NSRange pathRange = [self ks_replacementRangeOfURLPart:ks_URLPartPath]; NSString *rawPath = [abs substringWithRange:pathRange]; @@ -344,23 +344,23 @@ - (NSURL *)ks_URLByUnescapingUnreservedCharactersInPath // Add trailing "/". - (NSURL *)ks_URLByAddingTrailingSlashToDirectory { - NSString *pathExt = [self pathExtension]; - if (pathExt && [pathExt length] > 0) + NSString *pathExt = self.pathExtension; + if (pathExt && pathExt.length > 0) { // No need for trailing slash. return self; } NSRange rPath = [self ks_replacementRangeOfURLPart:ks_URLPartPath]; - if (rPath.length == 0 && [[self host] length] == 0) + if (rPath.length == 0 && self.host.length == 0) { // No need for trailing slash. return self; } - NSString *abs = [self absoluteString]; - if ([abs length] == 0) + NSString *abs = self.absoluteString; + if (abs.length == 0) { return self; } NSString *path = [abs substringWithRange:rPath]; - if ([path rangeOfString:@"/" options:NSBackwardsSearch].location == ([path length] - 1)) + if ([path rangeOfString:@"/" options:NSBackwardsSearch].location == (path.length - 1)) { // last char of path is "/" already return self; } @@ -374,8 +374,8 @@ - (NSURL *)ks_URLByAddingTrailingSlashToDirectory // Remove default port for http, https. - (NSURL *)ks_URLByRemovingDefaultPort { - NSString *scheme = [[self scheme] lowercaseString]; - NSInteger portVal = [[self port] integerValue]; + NSString *scheme = self.scheme.lowercaseString; + NSInteger portVal = self.port.integerValue; BOOL removePort = NO; if ([scheme isEqualToString:@"http"] && portVal == 80) { @@ -391,7 +391,7 @@ - (NSURL *)ks_URLByRemovingDefaultPort } NSRange rPort = [self ks_replacementRangeOfURLPart:ks_URLPartPort]; - NSString *abs = [self absoluteString]; + NSString *abs = self.absoluteString; abs = [abs stringByReplacingCharactersInRange:rPort withString:@""]; NSURL *correctedURL = [NSURL URLWithString:abs]; return correctedURL; @@ -401,7 +401,7 @@ - (NSURL *)ks_URLByRemovingDefaultPort // Remove dot-segments. - (NSURL *)ks_URLByRemovingDotSegments { - NSURL *standardized = [self standardizedURL]; + NSURL *standardized = self.standardizedURL; if (standardized) { return standardized; @@ -419,7 +419,7 @@ - (NSURL *)ks_URLByRemovingDotSegments - (NSURL *)ks_URLByRemovingDirectoryIndex { // Check whether a "directory index" page specified in URL. - NSString *lastPathComponent = [self lastPathComponent]; + NSString *lastPathComponent = self.lastPathComponent; static NSArray *defaultsArray; static dispatch_once_t onceToken; @@ -441,7 +441,7 @@ - (NSURL *)ks_URLByRemovingDirectoryIndex { if ([defPage caseInsensitiveCompare:lastPathComponent] == NSOrderedSame) { - return [self URLByDeletingLastPathComponent]; + return self.URLByDeletingLastPathComponent; } } @@ -458,7 +458,7 @@ - (NSURL *)ks_URLByRemovingFragment return self; } - NSString *abs = [self absoluteString]; + NSString *abs = self.absoluteString; abs = [abs stringByReplacingCharactersInRange:rFragment withString:@""]; NSURL *correctedURL = [NSURL URLWithString:abs]; return correctedURL; @@ -475,22 +475,22 @@ - (NSURL *)ks_URLByRemovingDuplicateSlashes // Replace all duplicate slashes ("//") except if preceeded by ":" // NOTE: This method does not depend on the -ks_replacementRangeOfURLPart: method. This is required because all the URL ranges for the path and later URL components depend on this method being called on the URL first. - NSMutableString *abs = [[[self absoluteString] mutableCopy] autorelease]; + NSMutableString *abs = [[self.absoluteString mutableCopy] autorelease]; NSRange schemeSlashesRange = [abs rangeOfString:@"://"]; NSRange replaceRange; if (NSNotFound == schemeSlashesRange.location) { - replaceRange = NSMakeRange(0, [abs length]); + replaceRange = NSMakeRange(0, abs.length); } else { NSInteger start = schemeSlashesRange.location + schemeSlashesRange.length; - replaceRange = NSMakeRange(start, [abs length] - start); + replaceRange = NSMakeRange(start, abs.length - start); } while ([abs replaceOccurrencesOfString:@"//" withString:@"/" options:0 range:replaceRange]) { NSInteger start = schemeSlashesRange.location + schemeSlashesRange.length; - replaceRange = NSMakeRange(start, [abs length] - start); + replaceRange = NSMakeRange(start, abs.length - start); } NSURL *cleanedURL = [NSURL URLWithString:abs]; diff --git a/KSURLQueryUtilities.h b/KSURLQueryUtilities.h index 05d6094..81820b3 100644 --- a/KSURLQueryUtilities.h +++ b/KSURLQueryUtilities.h @@ -30,7 +30,7 @@ @interface NSURL (KSURLQueryUtilities) // It's common to use the query part of a URL for a dictionary-like series of parameters. This method will decode that for you, including handling strings which were escaped to fit the scheme -- (NSDictionary *)ks_queryParameters; +@property (nonatomic, readonly, copy) NSDictionary *ks_queryParameters; // To do the reverse, construct a dictonary for the query and pass into either of these methods. You can base the result off of an existing URL, or specify all the components. - (NSURL *)ks_URLWithQueryParameters:(NSDictionary *)parameters; @@ -49,7 +49,7 @@ @interface NSString (KSURLQueryUtilities) // Follows RFC2396, section 3.4 -- (NSString *)ks_stringByAddingQueryComponentPercentEscapes; -- (NSString *)ks_stringByReplacingQueryComponentPercentEscapes; +@property (nonatomic, readonly, copy) NSString *ks_stringByAddingQueryComponentPercentEscapes; +@property (nonatomic, readonly, copy) NSString *ks_stringByReplacingQueryComponentPercentEscapes; @end diff --git a/KSURLUtilities.h b/KSURLUtilities.h index bee9c00..a594c0d 100644 --- a/KSURLUtilities.h +++ b/KSURLUtilities.h @@ -33,9 +33,9 @@ #pragma mark Host -- (NSURL *)ks_hostURL; -- (NSArray *)ks_domains; -- (BOOL)ks_hasNetworkLocation; // checks for a host with 2+ domains +@property (nonatomic, readonly, copy) NSURL *ks_hostURL; +@property (nonatomic, readonly, copy) NSArray *ks_domains; +@property (nonatomic, readonly) BOOL ks_hasNetworkLocation; // checks for a host with 2+ domains - (NSURL *)ks_URLWithHost:(NSString *)host; // swaps out host for a new one @@ -45,7 +45,7 @@ + (NSString *)ks_fileURLStringWithPath:(NSString *)path; -- (BOOL)ks_hasDirectoryPath; +@property (nonatomic, readonly) BOOL ks_hasDirectoryPath; - (NSURL *)ks_URLByAppendingPathComponent:(NSString *)pathComponent isDirectory:(BOOL)isDirectory; - (BOOL)ks_isSubpathOfURL:(NSURL *)aURL; @@ -70,7 +70,7 @@ #pragma mark RFC 1808 -- (BOOL)ks_canBeDecomposed; +@property (nonatomic, readonly) BOOL ks_canBeDecomposed; #pragma mark Comparison @@ -112,6 +112,6 @@ - (NSString *)ks_stringByAddingPercentEscapesWithSpacesAsPlusCharacters:(BOOL)encodeSpacesAsPlusCharacters; - (NSString *)ks_stringByAddingPercentEscapesWithSpacesAsPlusCharacters:(BOOL)encodeSpacesAsPlusCharacters escape:(NSString *)toEscape; -- (NSString *)ks_URLDirectoryPath; +@property (nonatomic, readonly, copy) NSString *ks_URLDirectoryPath; @end diff --git a/KSURLUtilities.m b/KSURLUtilities.m index b8b02ec..6118e10 100644 --- a/KSURLUtilities.m +++ b/KSURLUtilities.m @@ -34,14 +34,14 @@ @implementation NSURL (KSPathUtilities) - (NSURL *)ks_URLWithScheme:(NSString *)newScheme; { - NSString *scheme = [self scheme]; + NSString *scheme = self.scheme; if (!scheme) return nil; // -resourceSpecifier is supposed to give me everything after the scheme's colon, but for file:///path URLs, it just returns /path. Work around by deducing when resource specifier truly starts. Also found CFURLCopyResourceSpecifier() returns NULL for such URLs, against its documentation NSString *string = [[NSString alloc] initWithFormat: @"%@:%@", newScheme, - [[self absoluteString] substringFromIndex:[scheme length] + 1]]; // should be safe since a colon was needed to know scheme + [self.absoluteString substringFromIndex:scheme.length + 1]]; // should be safe since a colon was needed to know scheme NSURL *result = [[self class] URLWithString:string]; @@ -60,17 +60,17 @@ - (NSURL *)ks_hostURL; // returns a URL like "http://launch.karelia.com/" - (NSArray *)ks_domains; { - NSArray *result = [[self host] componentsSeparatedByString:@"."]; + NSArray *result = [self.host componentsSeparatedByString:@"."]; return result; } - (BOOL)ks_hasNetworkLocation { - NSString *resourceSpecifier = [self resourceSpecifier]; + NSString *resourceSpecifier = self.resourceSpecifier; BOOL result = (resourceSpecifier != nil && - [resourceSpecifier length] > 2 && - [[self ks_domains] count] >= 2); + resourceSpecifier.length > 2 && + [self ks_domains].count >= 2); return result; } @@ -110,7 +110,7 @@ + (NSURL *)ks_URLWithPath:(NSString *)path relativeToURL:(NSURL *)baseURL isDire kCFStringEncodingUTF8); // Work around 10.6 bug by effectively "faulting in" the base URL - if ([path isAbsolutePath] && [baseURL isFileURL]) [baseURL absoluteString]; + if (path.absolutePath && baseURL.fileURL) baseURL.absoluteString; NSURL *result = [self URLWithString:(__bridge NSString *)encodedPath relativeToURL:baseURL]; NSAssert(result, @"path wasn't escaped properly somehow: %@", path); @@ -126,7 +126,7 @@ + (NSURL *)ks_URLWithPath:(NSString *)path relativeToURL:(NSURL *)baseURL isDire + (NSString *)ks_fileURLStringWithPath:(NSString *)path; { NSURL *URL = [[NSURL alloc] initFileURLWithPath:path]; - NSString *result = [URL absoluteString]; + NSString *result = URL.absoluteString; return result; } @@ -196,26 +196,26 @@ - (BOOL)ks_isSubpathOfURL:(NSURL *)aURL; // File URLs are treated specially to handle 'localhost' versus '///' and symlinks - if ([self isFileURL] && [aURL isFileURL]) + if (self.fileURL && aURL.fileURL) { // Resolve aliases for local paths - NSString *myPath = [[self path] stringByResolvingSymlinksInPath]; - NSString *otherPath = [[aURL path] stringByResolvingSymlinksInPath]; + NSString *myPath = self.path.stringByResolvingSymlinksInPath; + NSString *otherPath = aURL.path.stringByResolvingSymlinksInPath; result = [myPath ks_isSubpathOfPath:otherPath]; } else { - NSString *scheme = [self scheme]; - NSString *otherScheme = [aURL scheme]; + NSString *scheme = self.scheme; + NSString *otherScheme = aURL.scheme; if (scheme && otherScheme && [scheme compare:otherScheme options:NSCaseInsensitiveSearch] == NSOrderedSame) { - NSString *myHost = [self host]; - NSString *otherHost = [aURL host]; + NSString *myHost = self.host; + NSString *otherHost = aURL.host; if (myHost && otherHost && [myHost compare:otherHost options:NSCaseInsensitiveSearch] == NSOrderedSame) { - NSString *myPath = [[self standardizedURL] path]; - NSString *otherPath = [[aURL standardizedURL] path]; + NSString *myPath = self.standardizedURL.path; + NSString *otherPath = aURL.standardizedURL.path; if (myPath && otherPath) { @@ -252,7 +252,7 @@ - (NSString *)ks_stringRelativeToURL:(NSURL *)URL // If the scheme, host or port differs, there is no possible relative path. Schemes and domains are considered to be case-insensitive. http://en.wikipedia.org/wiki/URL_normalization - NSString *myHost = [self host]; + NSString *myHost = self.host; if (!myHost) { // Host-less file URLs get special treatment, as if they're localhost. Maybe that could/should @@ -263,12 +263,12 @@ - (NSString *)ks_stringRelativeToURL:(NSURL *)URL else { // If self is an empty URL, there's no way to get to it. Falls through to here; return nil - NSString *result = [self absoluteString]; - return ([result length] ? result : nil); + NSString *result = self.absoluteString; + return (result.length ? result : nil); } } - NSString *otherHost = [URL host]; + NSString *otherHost = URL.host; if (!otherHost) { // Host-less file URLs get special treatment, as if they're localhost if (URL.isFileURL) { @@ -281,16 +281,16 @@ - (NSString *)ks_stringRelativeToURL:(NSURL *)URL if ([myHost caseInsensitiveCompare:otherHost] != NSOrderedSame) BAIL; - NSString *myScheme = [self scheme]; + NSString *myScheme = self.scheme; if (!myScheme) BAIL; - NSString *otherScheme = [URL scheme]; + NSString *otherScheme = URL.scheme; if (!otherScheme) BAIL; if ([myScheme caseInsensitiveCompare:otherScheme] != NSOrderedSame) BAIL; - NSNumber *myPort = [self port]; - NSNumber *aPort = [URL port]; + NSNumber *myPort = self.port; + NSNumber *aPort = URL.port; if (aPort != myPort && ![myPort isEqual:aPort]) // -isEqualToNumber: throws when passed nil { BAIL; @@ -318,7 +318,7 @@ - (NSString *)ks_stringRelativeToURL:(NSURL *)URL } else if (!CFURLHasDirectoryPath(absoluteURL)) // faster than -ks_hasDirectoryPath { - NSString *shortenedPath = [(__bridge NSString *)dirPath stringByDeletingLastPathComponent]; + NSString *shortenedPath = ((__bridge NSString *)dirPath).stringByDeletingLastPathComponent; CFRelease(dirPath); dirPath = CFRetain((__bridge CFTypeRef)(shortenedPath)); } @@ -333,7 +333,7 @@ - (NSString *)ks_stringRelativeToURL:(NSURL *)URL { if ([[(__bridge NSString *)myPath stringByAppendingString:@"/"] isEqualToString:(__bridge NSString *)dirPath]) { - result = [@"../" stringByAppendingString:[(__bridge NSString *)myPath lastPathComponent]]; + result = [@"../" stringByAppendingString:((__bridge NSString *)myPath).lastPathComponent]; } } @@ -352,19 +352,19 @@ - (NSString *)ks_stringRelativeToURL:(NSURL *)URL // Re-build any non-path information - NSString *parameters = [self parameterString]; + NSString *parameters = self.parameterString; if (parameters) { result = [result stringByAppendingFormat:@";%@", parameters]; } - NSString *query = [self query]; + NSString *query = self.query; if (query) { result = [result stringByAppendingFormat:@"?%@", query]; } - NSString *fragment = [self fragment]; + NSString *fragment = self.fragment; if (fragment) { result = [result stringByAppendingFormat:@"#%@", fragment]; @@ -398,9 +398,9 @@ - (BOOL)ks_isEqualToURL:(NSURL *)otherURL; BOOL result = [self isEqual:otherURL]; // For file: URLs the default check might have failed because they reference the host differently. If so, fall back to checking paths - if (!result && [self isFileURL] && [otherURL isFileURL]) + if (!result && self.fileURL && otherURL.fileURL) { - result = [[self path] isEqualToString:[otherURL path]]; + result = [self.path isEqualToString:otherURL.path]; } return result; @@ -409,32 +409,32 @@ - (BOOL)ks_isEqualToURL:(NSURL *)otherURL; - (BOOL)ks_isEqualExceptFragmentToURL:(NSURL *)anotherURL { // cover case where both are nil - return ( ([self baseURL] == [anotherURL baseURL]) || [[self baseURL] isEqual:[anotherURL baseURL]] ) + return ( (self.baseURL == anotherURL.baseURL) || [self.baseURL isEqual:anotherURL.baseURL] ) && - ( ([self scheme] == [anotherURL scheme]) || [[self scheme] isEqual:[anotherURL scheme]] ) + ( (self.scheme == anotherURL.scheme) || [self.scheme isEqual:anotherURL.scheme] ) && - ( ([self host] == [anotherURL host]) || [[self host] isEqual:[anotherURL host]] ) + ( (self.host == anotherURL.host) || [self.host isEqual:anotherURL.host] ) && - ( ([self path] == [anotherURL path]) || [[self path] isEqual:[anotherURL path]] ) + ( (self.path == anotherURL.path) || [self.path isEqual:anotherURL.path] ) && // query == parameterString? - ( ([self query] == [anotherURL query]) || [[self query] isEqual:[anotherURL query]] ) + ( (self.query == anotherURL.query) || [self.query isEqual:anotherURL.query] ) && - ( ([self parameterString] == [anotherURL parameterString]) || [[self parameterString] isEqual:[anotherURL parameterString]] ) + ( (self.parameterString == anotherURL.parameterString) || [self.parameterString isEqual:anotherURL.parameterString] ) && - ( ([self baseURL] == [anotherURL baseURL]) || [[self baseURL] isEqual:[anotherURL baseURL]] ) + ( (self.baseURL == anotherURL.baseURL) || [self.baseURL isEqual:anotherURL.baseURL] ) // less common pieces, but we gotta be careful && - ( ([self baseURL] == [anotherURL baseURL]) || [[self baseURL] isEqual:[anotherURL baseURL]] ) + ( (self.baseURL == anotherURL.baseURL) || [self.baseURL isEqual:anotherURL.baseURL] ) && - ( ([self port] == [anotherURL port]) || [[self port] isEqual:[anotherURL port]] ) + ( (self.port == anotherURL.port) || [self.port isEqual:anotherURL.port] ) && - ( ([self password] == [anotherURL password]) || [[self password] isEqual:[anotherURL password]] ) + ( (self.password == anotherURL.password) || [self.password isEqual:anotherURL.password] ) && - ( ([self user] == [anotherURL user]) || [[self user] isEqual:[anotherURL user]] ) + ( (self.user == anotherURL.user) || [self.user isEqual:anotherURL.user] ) ; } @@ -482,15 +482,15 @@ - (NSURL *)ks_URLByReplacingComponent:(CFURLComponentType)component withString:( // Grab data CFIndex length = CFURLGetBytes(absolute, NULL, 0); NSMutableData *data = [[NSMutableData alloc] initWithLength:length]; - length = CFURLGetBytes(absolute, [data mutableBytes], [data length]); + length = CFURLGetBytes(absolute, data.mutableBytes, data.length); NSAssert(length == [data length], @"CFURLGetBytes() lied to us!"); // Replace the host NSData *hostData = [string dataUsingEncoding:NSASCIIStringEncoding]; - [data replaceBytesInRange:NSMakeRange(range.location, range.length) withBytes:[hostData bytes] length:[hostData length]]; + [data replaceBytesInRange:NSMakeRange(range.location, range.length) withBytes:hostData.bytes length:hostData.length]; // Create final URL - result = CFBridgingRelease(CFURLCreateWithBytes(NULL, [data bytes], [data length], kCFStringEncodingASCII, NULL)); + result = CFBridgingRelease(CFURLCreateWithBytes(NULL, data.bytes, data.length, kCFStringEncodingASCII, NULL)); } CFRelease(absolute); @@ -535,7 +535,7 @@ - (NSString *)ks_stringByAddingPercentEscapesWithSpacesAsPlusCharacters:(BOOL)en [mutableResult replaceOccurrencesOfString:@" " withString:@"+" options:NSLiteralSearch - range:NSMakeRange(0, [mutableResult length])]; + range:NSMakeRange(0, mutableResult.length)]; result = mutableResult; } @@ -562,7 +562,7 @@ - (NSString *)ks_stringByAddingPercentEscapesWithSpacesAsPlusCharacters:(BOOL)en [mutableResult replaceOccurrencesOfString:@" " withString:@"+" options:NSLiteralSearch - range:NSMakeRange(0, [mutableResult length])]; + range:NSMakeRange(0, mutableResult.length)]; result = mutableResult; } diff --git a/KSWebLocation.h b/KSWebLocation.h index 5cad507..bc17e04 100644 --- a/KSWebLocation.h +++ b/KSWebLocation.h @@ -49,8 +49,8 @@ #pragma mark Init + (instancetype)webLocationWithURL:(NSURL *)URL; + (instancetype)webLocationWithURL:(NSURL *)URL title:(NSString *)title; -- (id)initWithURL:(NSURL *)URL; -- (id)initWithURL:(NSURL *)URL title:(NSString *)title; // Designated initializer +- (instancetype)initWithURL:(NSURL *)URL; +- (instancetype)initWithURL:(NSURL *)URL title:(NSString *)title NS_DESIGNATED_INITIALIZER; // Designated initializer #pragma mark Accessors @@ -70,5 +70,5 @@ @interface KSWebLocation (WeblocFiles) + (instancetype)webLocationWithContentsOfWeblocFile:(NSURL *)weblocURL; -- (id)initWithContentsOfWeblocFile:(NSURL *)weblocURL; +- (instancetype)initWithContentsOfWeblocFile:(NSURL *)weblocURL; @end diff --git a/KSWebLocation.m b/KSWebLocation.m index 9769e3f..73a1e65 100644 --- a/KSWebLocation.m +++ b/KSWebLocation.m @@ -42,7 +42,7 @@ + (instancetype)webLocationWithURL:(NSURL *)URL title:(NSString *)title #pragma mark Init & Dealloc -- (id)initWithURL:(NSURL *)URL title:(NSString *)name +- (instancetype)initWithURL:(NSURL *)URL title:(NSString *)name { NSParameterAssert(URL); @@ -55,12 +55,12 @@ - (id)initWithURL:(NSURL *)URL title:(NSString *)name return self; } -- (id)initWithURL:(NSURL *)URL; +- (instancetype)initWithURL:(NSURL *)URL; { return [self initWithURL:URL title:nil]; } -- (id)init +- (instancetype)init { return [self initWithURL:nil title:nil]; } @@ -83,16 +83,16 @@ - (NSString *)description; { return [NSString stringWithFormat: @"%@ %@ %@", - [super description], - [[self URL] absoluteString], - [self title]]; + super.description, + self.URL.absoluteString, + self.title]; } #pragma mark Equality - (NSUInteger)hash { - NSUInteger result = [[self URL] hash] | [[self title] hash]; + NSUInteger result = self.URL.hash | self.title.hash; return result; } @@ -114,7 +114,7 @@ - (BOOL)isEqual:(id)anObject - (BOOL)isEqualToWebLocation:(KSWebLocation *)aWebLocation { - BOOL result = [[aWebLocation URL] isEqual:[self URL]] && [[aWebLocation title] isEqualToString:[self title]]; + BOOL result = [aWebLocation.URL isEqual:self.URL] && [aWebLocation.title isEqualToString:self.title]; return result; } @@ -130,11 +130,11 @@ - (id)copyWithZone:(NSZone *)zone - (void)encodeWithCoder:(NSCoder *)encoder { - [encoder encodeObject:[self URL] forKey:@"URL"]; - [encoder encodeObject:[self title] forKey:@"name"]; + [encoder encodeObject:self.URL forKey:@"URL"]; + [encoder encodeObject:self.title forKey:@"name"]; } -- (id)initWithCoder:(NSCoder *)decoder +- (instancetype)initWithCoder:(NSCoder *)decoder { if ((self = [super init])) { @@ -155,7 +155,7 @@ + (NSData *)readFromResourceFileAtPath:(NSString *)aPath type:(ResType) aType na @try { FSRef theFSRef; - if (noErr == FSPathMakeRef((const UInt8 *)[aPath UTF8String], &theFSRef, NULL )) + if (noErr == FSPathMakeRef((const UInt8 *)aPath.UTF8String, &theFSRef, NULL )) { fileRef = FSOpenResFile(&theFSRef, fsRdPerm); if (noErr == ResError()) @@ -174,7 +174,7 @@ + (NSData *)readFromResourceFileAtPath:(NSString *)aPath type:(ResType) aType na // Create pascal string -- assume MacRoman encoding for resource names? BOOL success = CFStringGetPascalString((CFStringRef)aName, pString, - [aName length], + aName.length, kCFStringEncodingMacRomanLatin1); if (success) { @@ -222,9 +222,9 @@ + (instancetype)webLocationWithContentsOfWeblocFile:(NSURL *)weblocURL return [[[self alloc] initWithContentsOfWeblocFile:weblocURL] autorelease]; } -- (id)initWithContentsOfWeblocFile:(NSURL *)weblocURL +- (instancetype)initWithContentsOfWeblocFile:(NSURL *)weblocURL { - NSString *weblocPath = [weblocURL path]; + NSString *weblocPath = weblocURL.path; // Use the Carbon Resource Manager to read 'url ' resource #256. // String sems to be pre ASCII, with 2-bytes converted to % escapes diff --git a/KSWebLocationPasteboardUtilities.h b/KSWebLocationPasteboardUtilities.h index ef338a3..5315226 100644 --- a/KSWebLocationPasteboardUtilities.h +++ b/KSWebLocationPasteboardUtilities.h @@ -39,7 +39,7 @@ #pragma mark Pasteboard Reading + (NSArray *)readableTypesForPasteboard:(NSPasteboard *)pasteboard; -- (id)initWithPasteboardPropertyList:(id)propertyList ofType:(NSString *)type; +- (instancetype)initWithPasteboardPropertyList:(id)propertyList ofType:(NSString *)type; #pragma mark URL Guessing @@ -60,5 +60,5 @@ // 1. last path component // 2. minus path extension // 3. any underscores converted to spaces -- (NSArray *)readWebLocations; +@property (nonatomic, readonly, copy) NSArray *readWebLocations; @end diff --git a/KSWebLocationPasteboardUtilities.m b/KSWebLocationPasteboardUtilities.m index 63e0e4c..8d1364b 100644 --- a/KSWebLocationPasteboardUtilities.m +++ b/KSWebLocationPasteboardUtilities.m @@ -52,11 +52,10 @@ + (NSArray *)readableTypesForPasteboard:(NSPasteboard *)pasteboard; withObject:pasteboard]; } - NSArray *result = [NSArray arrayWithObjects: - NSURLPboardType, + NSArray *result = @[NSURLPboardType, NSStringPboardType, NSRTFPboardType, - NSRTFDPboardType, nil]; + NSRTFDPboardType]; if (URLTypes) result = [URLTypes arrayByAddingObjectsFromArray:result]; return result; @@ -81,12 +80,12 @@ + (NSUInteger)readingOptionsForType:(NSString *)type + (NSString *)guessTitleForURL:(NSURL *)URL; { - NSString *result = [[URL ks_lastPathComponent] stringByDeletingPathExtension]; + NSString *result = URL.lastPathComponent.stringByDeletingPathExtension; result = [result stringByReplacingOccurrencesOfString:@"_" withString:@" "]; return result; } -- (id)initWithPasteboardPropertyList:(id)propertyList ofType:(NSString *)type; +- (instancetype)initWithPasteboardPropertyList:(id)propertyList ofType:(NSString *)type; { // Try with NSURL NSURL *URL = [[NSURL alloc] initWithPasteboardPropertyList:propertyList ofType:type]; @@ -100,7 +99,7 @@ - (id)initWithPasteboardPropertyList:(id)propertyList ofType:(NSString *)type; else { NSString *string = [propertyList description]; - if ([string length] <= 2048) // No point processing particularly long strings + if (string.length <= 2048) // No point processing particularly long strings { NSURL *plistURL = [KSURLFormatter URLFromString:string]; /// encodeLegally to handle accented characters if (plistURL && [plistURL ks_hasNetworkLocation]) @@ -142,16 +141,16 @@ + (NSURL *)URLFromPasteboard:(NSPasteboard *)pboard NSString *string = [pboard stringForType:NSStringPboardType]; NSArray *checkResults = [spellChecker checkString:string - range:NSMakeRange(0, [string length]) + range:NSMakeRange(0, string.length) types:(NSTextCheckingTypes)NSTextCheckingTypeLink // cast to suppress oddity of SDK options:nil inSpellDocumentWithTag:0 orthography:NULL wordCount:NULL]; - if ([checkResults count]) + if (checkResults.count) { - result = [[checkResults objectAtIndex:0] URL]; + result = [checkResults[0] URL]; } } } @@ -164,15 +163,14 @@ + (NSURL *)URLFromPasteboard:(NSPasteboard *)pboard + (NSArray *)webLocationPasteboardTypes { - return [NSArray arrayWithObjects: - @"WebURLsWithTitlesPboardType", + return @[@"WebURLsWithTitlesPboardType", @"BookmarkDictionaryListPboardType", - kUTTypeURL, // contains the target URL when dragging webloc + (id)kUTTypeURL, // contains the target URL when dragging webloc NSFilenamesPboardType, NSURLPboardType, NSStringPboardType, NSRTFPboardType, - NSRTFDPboardType, nil]; + NSRTFDPboardType]; } /* Retrieve URLs and their titles from the pasteboard for the "BookmarkDictionaryListPboardType" type @@ -182,21 +180,21 @@ + (NSArray *)webLocationsWithBookmarkDictionariesPasteboardPropertyList:(id)prop NSArray *result = nil; NSArray *arrayFromData = propertyList; - if (arrayFromData && [arrayFromData isKindOfClass:[NSArray class]] && [arrayFromData count] > 0) + if (arrayFromData && [arrayFromData isKindOfClass:[NSArray class]] && arrayFromData.count > 0) { - NSDictionary *objectInfo = [arrayFromData objectAtIndex:0]; + NSDictionary *objectInfo = arrayFromData[0]; if ([objectInfo isKindOfClass:[NSDictionary class]]) { - NSString *URLString = [objectInfo objectForKey:@"URLString"]; + NSString *URLString = objectInfo[@"URLString"]; NSURL *URL = [KSURLFormatter URLFromString:URLString]; /// encodeLegally to handle accented characters if (URL) { - NSString *title = [[objectInfo objectForKey:@"URIDictionary"] objectForKey:@"title"]; + NSString *title = objectInfo[@"URIDictionary"][@"title"]; if (!title) title = [[self class] guessTitleForURL:URL]; KSWebLocation *webLoc = [[KSWebLocation alloc] initWithURL:URL title:title]; - result = [NSArray arrayWithObject:webLoc]; + result = @[webLoc]; [webLoc release]; } } @@ -214,25 +212,25 @@ + (NSArray *)webLocationsWithWebURLsWithTitlesPasteboardPropertyList:(id)propert // Bail if we haven't been handed decent data NSArray *rawDataArray = propertyList; - if (rawDataArray && [rawDataArray isKindOfClass:[NSArray class]] && [rawDataArray count] >= 2) + if (rawDataArray && [rawDataArray isKindOfClass:[NSArray class]] && rawDataArray.count >= 2) { // Get the array of URLs and their titles - NSArray *URLStrings = [rawDataArray objectAtIndex:0]; - NSArray *URLTitles = [rawDataArray objectAtIndex:1]; + NSArray *URLStrings = rawDataArray[0]; + NSArray *URLTitles = rawDataArray[1]; // Run through each URL - result = [NSMutableArray arrayWithCapacity:[URLStrings count]]; + result = [NSMutableArray arrayWithCapacity:URLStrings.count]; NSInteger i; - for (i=0; i<[URLStrings count]; i++) + for (i=0; i