Skip to content

Commit 40bb9f4

Browse files
committed
Fix force unwrapping
1 parent 6fadb33 commit 40bb9f4

6 files changed

+41
-31
lines changed

.swiftlint.yml

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ included: # paths to include during linting. `--path` is ignored if present.
33
excluded: # paths to ignore during linting. Takes precedence over `included`.
44
- Carthage
55
- Pods
6+
disabled_rules:
7+
- type_name
68

79
# configurable rules can be customized from this configuration file
810
# binary rules can set their severity level

Cartfile.resolved

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
github "krzyzanowskim/CryptoSwift" "0.6.0"
2-
github "hyperoslo/Cache" "f4f1398fe02c762630a802b5e5ae9c334beafed9"
2+
github "hyperoslo/Cache" "0246e9e9e8faae93f2c83d15824789577f4720bf"

Sources/Shared/Fetcher.swift

+13-11
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ class Fetcher {
77
case failure(Error)
88
}
99

10-
enum Error {
10+
enum Failure: Error {
1111
case invalidResponse
1212
case invalidStatusCode
1313
case invalidData
1414
case invalidContentLength
1515
case conversionError
1616
}
1717

18-
let URL: Foundation.URL
18+
let url: URL
1919
var task: URLSessionDataTask?
2020
var active = false
2121

@@ -25,8 +25,8 @@ class Fetcher {
2525

2626
// MARK: - Initialization
2727

28-
init(URL: Foundation.URL) {
29-
self.URL = URL
28+
init(url: URL) {
29+
self.url = url
3030
}
3131

3232
// MARK: - Fetching
@@ -37,40 +37,42 @@ class Fetcher {
3737
DispatchQueue.global(qos: DispatchQoS.QoSClass.background).async { [weak self] in
3838
guard let weakSelf = self, weakSelf.active else { return }
3939

40-
weakSelf.task = weakSelf.session.dataTask(with: weakSelf.URL, completionHandler: {
40+
weakSelf.task = weakSelf.session.dataTask(with: weakSelf.url, completionHandler: {
4141
[weak self] data, response, error -> Void in
4242

4343
guard let weakSelf = self, weakSelf.active else { return }
4444

4545
if let error = error {
46-
weakSelf.complete { completion(.failure(error as! Fetcher.Error)) }
46+
weakSelf.complete { completion(.failure(error)) }
4747
return
4848
}
4949

5050
guard let httpResponse = response as? HTTPURLResponse else {
51-
weakSelf.complete { completion(.failure(Error.invalidResponse)) }
51+
weakSelf.complete { completion(.failure(Failure.invalidResponse)) }
5252
return
5353
}
5454

5555
guard httpResponse.statusCode == 200 else {
56-
weakSelf.complete { completion(.failure(Error.invalidStatusCode)) }
56+
weakSelf.complete { completion(.failure(Failure.invalidStatusCode)) }
5757
return
5858
}
5959

6060
guard let data = data, httpResponse.validateLength(data) else {
61-
weakSelf.complete { completion(.failure(Error.invalidContentLength)) }
61+
weakSelf.complete { completion(.failure(Failure.invalidContentLength)) }
6262
return
6363
}
6464

6565
guard let decodedImage = Image.decode(data) else {
66-
weakSelf.complete { completion(.failure(Error.conversionError)) }
66+
weakSelf.complete { completion(.failure(Failure.conversionError)) }
6767
return
6868
}
6969

7070
let image = preprocess(decodedImage)
7171

7272
if weakSelf.active {
73-
weakSelf.complete { completion(Result.success(image)) }
73+
weakSelf.complete {
74+
completion(Result.success(image))
75+
}
7476
}
7577
})
7678

Sources/Shared/ImageView+Imaginary.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ public typealias Preprocess = (Image) -> Image
44

55
extension ImageView {
66

7-
public func setImage(_ URL: Foundation.URL?,
7+
public func setImage(_ url: URL?,
88
placeholder: Image? = nil,
99
preprocess: @escaping Preprocess = { image in return image },
1010
completion: ((Image?) -> ())? = nil) {
1111
image = placeholder
1212

13-
guard let URL = URL else { return }
13+
guard let url = url else { return }
1414

15-
let key = URL.absoluteString
15+
let key = url.absoluteString
1616

1717
if let fetcher = fetcher {
1818
fetcher.cancel()
@@ -35,7 +35,7 @@ extension ImageView {
3535
Configuration.preConfigure?(weakSelf)
3636
}
3737

38-
weakSelf.fetcher = Fetcher(URL: URL)
38+
weakSelf.fetcher = Fetcher(url: url)
3939

4040
weakSelf.fetcher?.start(preprocess) { [weak self] result in
4141
guard let weakSelf = self else { return }

Sources/iOS/Decompressor.swift

+15-13
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,27 @@ struct Decompressor {
55
static func decompress(_ data: Data, scale: CGFloat = 1) -> Image {
66
guard let image = Image(data: data) else { return Image() }
77

8-
let imageRef = image.cgImage
8+
guard let imageRef = image.cgImage, let colorSpaceRef = imageRef.colorSpace else {
9+
return image
10+
}
911

10-
if imageRef?.alphaInfo != .none { return image }
12+
if imageRef.alphaInfo != .none { return image }
1113

12-
let colorSpaceRef = imageRef?.colorSpace
13-
let width = imageRef?.width
14-
let height = imageRef?.height
14+
let width = imageRef.width
15+
let height = imageRef.height
1516
let bytesPerPixel: Int = 4
16-
let bytesPerRow: Int = bytesPerPixel * width!
17+
let bytesPerRow: Int = bytesPerPixel * width
1718
let bitsPerComponent: Int = 8
1819

1920
let context = CGContext(data: nil,
20-
width: width!,
21-
height: height!,
22-
bitsPerComponent: bitsPerComponent,
23-
bytesPerRow: bytesPerRow,
24-
space: colorSpaceRef!,
25-
bitmapInfo: CGBitmapInfo().rawValue)
26-
context?.draw(imageRef!, in: CGRect(x: 0, y: 0, width: CGFloat(width!), height: CGFloat(height!)))
21+
width: width,
22+
height: height,
23+
bitsPerComponent: bitsPerComponent,
24+
bytesPerRow: bytesPerRow,
25+
space: colorSpaceRef,
26+
bitmapInfo: CGBitmapInfo().rawValue)
27+
28+
context?.draw(imageRef, in: CGRect(x: 0, y: 0, width: CGFloat(width), height: CGFloat(height)))
2729

2830
guard let imageRefWithoutAlpha = context?.makeImage() else {
2931
return image

Sources/iOS/Drawers/Drawers.swift

+6-2
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,22 @@ public struct TintDrawer: ImageDrawer {
1111
}
1212

1313
public func draw(_ image: UIImage, context: CGContext, rect: CGRect) {
14+
guard let cgImage = image.cgImage else {
15+
return
16+
}
17+
1418
context.setBlendMode(.normal)
1519
UIColor.black.setFill()
1620
context.fill(rect)
1721

1822
context.setBlendMode(.normal)
19-
context.draw(image.cgImage!, in: rect)
23+
context.draw(cgImage, in: rect)
2024

2125
context.setBlendMode(.color)
2226
tintColor.setFill()
2327
context.fill(rect)
2428

2529
context.setBlendMode(.destinationIn)
26-
context.draw(image.cgImage!, in: rect)
30+
context.draw(cgImage, in: rect)
2731
}
2832
}

0 commit comments

Comments
 (0)