Просмотр исходного кода

Merge pull request #156 from onevcat/refactor/lint

Fix code styles
Wei Wang 10 лет назад
Родитель
Сommit
568325a776

+ 2 - 0
.swiftlint.yml

@@ -4,3 +4,5 @@ disabled_rules:
   - variable_name
   - file_length
   - function_body_length
+  - opening_brace
+  - type_body_length

+ 0 - 5
Kingfisher-Demo/AppDelegate.swift

@@ -35,8 +35,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
 
     func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
         // Override point for customization after application launch.
-        
-        
         return true
     }
 
@@ -61,7 +59,4 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
     func applicationWillTerminate(application: UIApplication) {
         // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
     }
-
-
 }
-

+ 1 - 0
Kingfisher-Demo/ViewController.swift

@@ -56,6 +56,7 @@ extension ViewController {
     }
     
     override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
+        // swiftlint:disable force_cast
         let cell = collectionView.dequeueReusableCellWithReuseIdentifier("collectionViewCell", forIndexPath: indexPath) as! CollectionViewCell
         
         cell.cellImageView.kf_showIndicatorWhenLoading = true

+ 0 - 1
Kingfisher-tvOS-Demo/AppDelegate.swift

@@ -44,4 +44,3 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
 
 
 }
-

+ 17 - 0
Kingfisher.xcodeproj/project.pbxproj

@@ -430,6 +430,7 @@
 				D1ED2D091AD2CFA600CFC3EB /* Resources */,
 				D1ED2D511AD2D09F00CFC3EB /* Embed Frameworks */,
 				4BE2C9E61B381D42005313E5 /* Embed Watch Content */,
+				4B82F31C1BF5CFDE0089B2CE /* Swift Lint */,
 			);
 			buildRules = (
 			);
@@ -604,6 +605,20 @@
 			shellPath = /bin/sh;
 			shellScript = "#\n# Set the build number to the current git commit count if on master.\n#\ngit=`sh /etc/profile; which git`\n\nbranchName=`\"$git\" rev-parse --abbrev-ref HEAD`\nif [ $branchName = \"master\" ]; then\nappBuild=`\"$git\" rev-list --all |wc -l`\nagvtool new-version -all $appBuild\nfi\n";
 		};
+		4B82F31C1BF5CFDE0089B2CE /* Swift Lint */ = {
+			isa = PBXShellScriptBuildPhase;
+			buildActionMask = 2147483647;
+			files = (
+			);
+			inputPaths = (
+			);
+			name = "Swift Lint";
+			outputPaths = (
+			);
+			runOnlyForDeploymentPostprocessing = 0;
+			shellPath = /bin/sh;
+			shellScript = "if which swiftlint >/dev/null; then\nswiftlint\nelse\necho \"SwiftLint does not exist, download from https://github.com/realm/SwiftLint\"\nfi";
+		};
 		5A0400F84A1FF7DBA809925E /* Copy Pods Resources */ = {
 			isa = PBXShellScriptBuildPhase;
 			buildActionMask = 2147483647;
@@ -1028,6 +1043,7 @@
 				D13F49CF1BEDA53F00CE335D /* Release */,
 			);
 			defaultConfigurationIsVisible = 0;
+			defaultConfigurationName = Release;
 		};
 		D13F49DF1BEDA67C00CE335D /* Build configuration list for PBXNativeTarget "Kingfisher-tvOS" */ = {
 			isa = XCConfigurationList;
@@ -1036,6 +1052,7 @@
 				D13F49E11BEDA67C00CE335D /* Release */,
 			);
 			defaultConfigurationIsVisible = 0;
+			defaultConfigurationName = Release;
 		};
 		D1ED2D061AD2CFA600CFC3EB /* Build configuration list for PBXProject "Kingfisher" */ = {
 			isa = XCConfigurationList;

+ 8 - 16
Kingfisher/ImageCache.swift

@@ -146,7 +146,7 @@ public extension ImageCache {
     - parameter key:          Key for the image.
     */
     public func storeImage(image: UIImage, originalData: NSData? = nil, forKey key: String) {
-        storeImage(image, originalData: originalData,forKey: key, toDisk: true, completionHandler: nil)
+        storeImage(image, originalData: originalData, forKey: key, toDisk: true, completionHandler: nil)
     }
     
     /**
@@ -409,12 +409,9 @@ extension ImageCache {
                 
                 var diskCacheSize: UInt = 0
                 
-                if let fileEnumerator = self.fileManager.enumeratorAtURL(diskCacheURL,
-                    includingPropertiesForKeys: resourceKeys,
-                    options: NSDirectoryEnumerationOptions.SkipsHiddenFiles,
-                    errorHandler: nil) {
-                        
-                    for fileURL in fileEnumerator.allObjects as! [NSURL] {
+                if let fileEnumerator = self.fileManager.enumeratorAtURL(diskCacheURL, includingPropertiesForKeys: resourceKeys, options: NSDirectoryEnumerationOptions.SkipsHiddenFiles, errorHandler: nil),
+                                 urls = fileEnumerator.allObjects as? [NSURL] {
+                    for fileURL in urls {
                             
                         do {
                             let resourceValues = try fileURL.resourceValuesForKeys(resourceKeys)
@@ -584,13 +581,9 @@ public extension ImageCache {
             let resourceKeys = [NSURLIsDirectoryKey, NSURLTotalFileAllocatedSizeKey]
             var diskCacheSize: UInt = 0
             
-            if let fileEnumerator = self.fileManager.enumeratorAtURL(diskCacheURL,
-                includingPropertiesForKeys: resourceKeys,
-                options: NSDirectoryEnumerationOptions.SkipsHiddenFiles,
-                errorHandler: nil) {
-                    
-                    for fileURL in fileEnumerator.allObjects as! [NSURL] {
-                        
+            if let fileEnumerator = self.fileManager.enumeratorAtURL(diskCacheURL, includingPropertiesForKeys: resourceKeys, options: NSDirectoryEnumerationOptions.SkipsHiddenFiles, errorHandler: nil),
+                             urls = fileEnumerator.allObjects as? [NSURL] {
+                    for fileURL in urls {
                         do {
                             let resourceValues = try fileURL.resourceValuesForKeys(resourceKeys)
                             // If it is a Directory. Continue to next file URL.
@@ -651,7 +644,7 @@ extension UIImage {
 }
 
 extension Dictionary {
-    func keysSortedByValue(isOrderedBefore:(Value, Value) -> Bool) -> [Key] {
+    func keysSortedByValue(isOrderedBefore: (Value, Value) -> Bool) -> [Key] {
         var array = Array(self)
         array.sortInPlace {
             let (_, lv) = $0
@@ -664,4 +657,3 @@ extension Dictionary {
         }
     }
 }
-

+ 0 - 1
Kingfisher/ImageTransition.swift

@@ -99,4 +99,3 @@ public enum ImageTransition {
         }
     }
 }
-

+ 1 - 1
Kingfisher/KingfisherOptions.swift

@@ -29,7 +29,7 @@ import Foundation
 /**
 *  Options to control Kingfisher behaviors.
 */
-public struct KingfisherOptions : OptionSetType {
+public struct KingfisherOptions: OptionSetType {
 
     public let rawValue: UInt
     

+ 2 - 2
Kingfisher/KingfisherOptionsInfo.swift

@@ -46,7 +46,7 @@ public enum KingfisherOptionsInfoItem {
     case Transition(ImageTransition)
 }
 
-func ==(a: KingfisherOptionsInfoItem, b: KingfisherOptionsInfoItem) -> Bool {
+func == (a: KingfisherOptionsInfoItem, b: KingfisherOptionsInfoItem) -> Bool {
     switch (a, b) {
     case (.Options(_), .Options(_)): return true
     case (.TargetCache(_), .TargetCache(_)): return true
@@ -66,4 +66,4 @@ extension CollectionType where Generator.Element == KingfisherOptionsInfoItem {
         
         return (index != nil) ? self[index!] : nil
     }
-}
+}

+ 1 - 1
Kingfisher/Resource.swift

@@ -34,4 +34,4 @@ public struct Resource {
         self.downloadURL = downloadURL
         self.cacheKey = cacheKey ?? downloadURL.absoluteString
     }
-}
+}

+ 23 - 23
Kingfisher/String+MD5.swift

@@ -43,7 +43,7 @@ extension String {
 }
 
 /** array of bytes, little-endian representation */
-func arrayOfBytes<T>(value:T, length:Int? = nil) -> [UInt8] {
+func arrayOfBytes<T>(value: T, length: Int? = nil) -> [UInt8] {
     let totalBytes = length ?? (sizeofValue(value) * 8)
     
     let valuePointer = UnsafeMutablePointer<T>.alloc(1)
@@ -51,7 +51,7 @@ func arrayOfBytes<T>(value:T, length:Int? = nil) -> [UInt8] {
     
     let bytesPointer = UnsafeMutablePointer<UInt8>(valuePointer)
     var bytes = [UInt8](count: totalBytes, repeatedValue: 0)
-    for j in 0..<min(sizeof(T),totalBytes) {
+    for j in 0..<min(sizeof(T), totalBytes) {
         bytes[totalBytes - 1 - j] = (bytesPointer + j).memory
     }
     
@@ -87,7 +87,7 @@ class HashBase {
     }
     
     /** Common part for hash calculation. Prepare header data. */
-    func prepare(len:Int = 64) -> NSMutableData {
+    func prepare(len: Int = 64) -> NSMutableData {
         let tmpMessage: NSMutableData = NSMutableData(data: self.message)
         
         // Step 1. Append Padding Bits
@@ -123,22 +123,22 @@ class MD5 : HashBase {
         6, 10, 15, 21,  6, 10, 15, 21,  6, 10, 15, 21,  6, 10, 15, 21]
     
     /** binary integer part of the sines of integers (Radians) */
-    private let k: [UInt32] = [0xd76aa478,0xe8c7b756,0x242070db,0xc1bdceee,
-        0xf57c0faf,0x4787c62a,0xa8304613,0xfd469501,
-        0x698098d8,0x8b44f7af,0xffff5bb1,0x895cd7be,
-        0x6b901122,0xfd987193,0xa679438e,0x49b40821,
-        0xf61e2562,0xc040b340,0x265e5a51,0xe9b6c7aa,
-        0xd62f105d,0x2441453,0xd8a1e681,0xe7d3fbc8,
-        0x21e1cde6,0xc33707d6,0xf4d50d87,0x455a14ed,
-        0xa9e3e905,0xfcefa3f8,0x676f02d9,0x8d2a4c8a,
-        0xfffa3942,0x8771f681,0x6d9d6122,0xfde5380c,
-        0xa4beea44,0x4bdecfa9,0xf6bb4b60,0xbebfbc70,
-        0x289b7ec6,0xeaa127fa,0xd4ef3085,0x4881d05,
-        0xd9d4d039,0xe6db99e5,0x1fa27cf8,0xc4ac5665,
-        0xf4292244,0x432aff97,0xab9423a7,0xfc93a039,
-        0x655b59c3,0x8f0ccc92,0xffeff47d,0x85845dd1,
-        0x6fa87e4f,0xfe2ce6e0,0xa3014314,0x4e0811a1,
-        0xf7537e82,0xbd3af235,0x2ad7d2bb,0xeb86d391]
+    private let k: [UInt32] = [0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee,
+                               0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,
+                               0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be,
+                               0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,
+                               0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa,
+                               0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8,
+                               0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed,
+                               0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a,
+                               0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c,
+                               0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,
+                               0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x4881d05,
+                               0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
+                               0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039,
+                               0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1,
+                               0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1,
+                               0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391]
     
     private let h:[UInt32] = [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476]
     
@@ -157,7 +157,7 @@ class MD5 : HashBase {
         let chunkSizeBytes = 512 / 8 // 64
         var leftMessageBytes = tmpMessage.length
         for (var i = 0; i < tmpMessage.length; i = i + chunkSizeBytes, leftMessageBytes -= chunkSizeBytes) {
-            let chunk = tmpMessage.subdataWithRange(NSRange(location: i, length: min(chunkSizeBytes,leftMessageBytes)))
+            let chunk = tmpMessage.subdataWithRange(NSRange(location: i, length: min(chunkSizeBytes, leftMessageBytes)))
             
             // break chunk into sixteen 32-bit words M[j], 0 ≤ j ≤ 15
             var M:[UInt32] = [UInt32](count: 16, repeatedValue: 0)
@@ -175,9 +175,9 @@ class MD5 : HashBase {
             // Main loop
             for j in 0..<k.count {
                 var g = 0
-                var F:UInt32 = 0
+                var F: UInt32 = 0
                 
-                switch (j) {
+                switch j {
                 case 0...15:
                     F = (B & C) | ((~B) & D)
                     g = j
@@ -218,4 +218,4 @@ class MD5 : HashBase {
         
         return buf.copy() as! NSData;
     }
-}
+}

+ 1 - 1
Kingfisher/ThreadHelper.swift

@@ -34,4 +34,4 @@ func dispatch_async_safely_main_queue(block: ()->()) {
             block()
         }
     }
-}
+}

+ 2 - 3
Kingfisher/UIButton+Kingfisher.swift

@@ -207,7 +207,7 @@ public extension UIButton {
                 
                 dispatch_async_safely_main_queue {
                     if let sSelf = self {
-                        if (imageURL == sSelf.kf_webURLForState(state) && image != nil) {
+                        if imageURL == sSelf.kf_webURLForState(state) && image != nil {
                             sSelf.setImage(image, forState: state)
                         }
                         completionHandler?(image: image, error: error, cacheType: cacheType, imageURL: imageURL)
@@ -456,7 +456,7 @@ public extension UIButton {
             completionHandler: { [weak self] image, error, cacheType, imageURL in
                 dispatch_async_safely_main_queue {
                     if let sSelf = self {
-                        if (imageURL == sSelf.kf_backgroundWebURLForState(state) && image != nil) {
+                        if imageURL == sSelf.kf_backgroundWebURLForState(state) && image != nil {
                             sSelf.setBackgroundImage(image, forState: state)
                         }
                         completionHandler?(image: image, error: error, cacheType: cacheType, imageURL: imageURL)
@@ -590,4 +590,3 @@ public extension UIButton {
         return kf_setBackgroundImageWithURL(URL, forState: state, placeholderImage: placeholderImage, optionsInfo: [.Options(options)], progressBlock: progressBlock, completionHandler: completionHandler)
     }
 }
-

+ 2 - 5
Kingfisher/UIImage+Extension.swift

@@ -49,7 +49,7 @@ extension NSData {
             buffer[2] == jpgHeaderIF[0]
         {
             return .JPEG
-        }else if buffer[0] == gifHeader[0] &&
+        } else if buffer[0] == gifHeader[0] &&
             buffer[1] == gifHeader[1] &&
             buffer[2] == gifHeader[2]
         {
@@ -176,13 +176,10 @@ extension UIImage {
             images.append(UIImage(CGImage: imageRef, scale: scale, orientation: .Up))
         }
         
-        if (frameCount == 1) {
+        if frameCount == 1 {
             return images.first
         } else {
             return UIImage.animatedImageWithImages(images, duration: duration <= 0.0 ? gifDuration : duration)
         }
     }
 }
-
-
-

+ 0 - 1
Kingfisher/UIImageView+Kingfisher.swift

@@ -359,4 +359,3 @@ public extension UIImageView {
     }
     
 }
-

+ 5 - 1
KingfisherTests/ImageCacheTests.swift

@@ -187,7 +187,11 @@ class ImageCacheTests: XCTestCase {
 
                 XCTAssert(noti.object === self.cache, "The object of notification should be the cache object.")
                 
-                let hashes = noti.userInfo?[KingfisherDiskCacheCleanedHashKey] as! [String]
+                guard let hashes = noti.userInfo?[KingfisherDiskCacheCleanedHashKey] as? [String] else {
+                    XCTFail("The clean disk cache notification should contains Strings in key 'KingfisherDiskCacheCleanedHashKey'")
+                    expectation.fulfill()
+                    return
+                }
                 
                 XCTAssertEqual(1, hashes.count, "There should be one and only one file cleaned")
                 XCTAssertEqual(hashes.first!, self.cache.hashForKey(testKeys[0]), "The cleaned file should be the stored one.")

+ 1 - 1
KingfisherTests/ImageExtensionTests.swift

@@ -32,7 +32,7 @@ class ImageExtensionTests: XCTestCase {
         format = testImageGIFData.kf_imageFormat
         XCTAssertEqual(format, ImageFormat.GIF)
         
-        let raw = [1,2,3,4,5,6,7,8]
+        let raw = [1, 2, 3, 4, 5, 6, 7, 8]
         format = NSData(bytes: raw, length: 8) .kf_imageFormat
         XCTAssertEqual(format, ImageFormat.Unknown)
     }