Skip to content

Commit f1d4e5f

Browse files
committed
1 parent 56bc53c commit f1d4e5f

8 files changed

Lines changed: 72 additions & 24 deletions

File tree

Disk.xcodeproj/project.pbxproj

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
10F3C7041F23D9C9006D42EF /* Disk+Data.swift in Sources */ = {isa = PBXBuildFile; fileRef = 10F3C7031F23D9C9006D42EF /* Disk+Data.swift */; };
2424
10F3C7061F23D9E0006D42EF /* Disk+[Data].swift in Sources */ = {isa = PBXBuildFile; fileRef = 10F3C7051F23D9E0006D42EF /* Disk+[Data].swift */; };
2525
10F3C7081F23DAF2006D42EF /* Message.swift in Sources */ = {isa = PBXBuildFile; fileRef = 10F3C7071F23DAF2006D42EF /* Message.swift */; };
26+
261E777E2178916800B7DFB6 /* DiskErrors.m in Sources */ = {isa = PBXBuildFile; fileRef = 261E777D2178916800B7DFB6 /* DiskErrors.m */; };
27+
261E777F21789EB900B7DFB6 /* DiskErrors.h in Headers */ = {isa = PBXBuildFile; fileRef = 261E777C2178912C00B7DFB6 /* DiskErrors.h */; settings = {ATTRIBUTES = (Public, ); }; };
2628
/* End PBXBuildFile section */
2729

2830
/* Begin PBXContainerItemProxy section */
@@ -55,6 +57,8 @@
5557
10F3C7031F23D9C9006D42EF /* Disk+Data.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Disk+Data.swift"; sourceTree = "<group>"; };
5658
10F3C7051F23D9E0006D42EF /* Disk+[Data].swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Disk+[Data].swift"; sourceTree = "<group>"; };
5759
10F3C7071F23DAF2006D42EF /* Message.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Message.swift; sourceTree = "<group>"; };
60+
261E777C2178912C00B7DFB6 /* DiskErrors.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = DiskErrors.h; sourceTree = "<group>"; };
61+
261E777D2178916800B7DFB6 /* DiskErrors.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = DiskErrors.m; sourceTree = "<group>"; };
5862
/* End PBXFileReference section */
5963

6064
/* Begin PBXFrameworksBuildPhase section */
@@ -103,6 +107,8 @@
103107
103FA3761F713332004C600F /* Disk+InternalHelpers.swift */,
104108
103FA3781F713340004C600F /* Disk+VolumeInformation.swift */,
105109
104611771F27B27E00BBDB3A /* Disk+Errors.swift */,
110+
261E777C2178912C00B7DFB6 /* DiskErrors.h */,
111+
261E777D2178916800B7DFB6 /* DiskErrors.m */,
106112
109256591F247F7700B3C32A /* Disk+Helpers.swift */,
107113
10F3C6FD1F23D9A4006D42EF /* Disk+Codable.swift */,
108114
10F3C6FF1F23D9B1006D42EF /* Disk+UIImage.swift */,
@@ -132,6 +138,7 @@
132138
isa = PBXHeadersBuildPhase;
133139
buildActionMask = 2147483647;
134140
files = (
141+
261E777F21789EB900B7DFB6 /* DiskErrors.h in Headers */,
135142
10F3C6F21F23D984006D42EF /* Disk.h in Headers */,
136143
);
137144
runOnlyForDeploymentPostprocessing = 0;
@@ -187,7 +194,7 @@
187194
TargetAttributes = {
188195
10F3C6E01F23D984006D42EF = {
189196
CreatedOnToolsVersion = 9.0;
190-
LastSwiftMigration = 0900;
197+
LastSwiftMigration = 1010;
191198
};
192199
10F3C6E91F23D984006D42EF = {
193200
CreatedOnToolsVersion = 9.0;
@@ -246,6 +253,7 @@
246253
10F3C7041F23D9C9006D42EF /* Disk+Data.swift in Sources */,
247254
103FA3791F713340004C600F /* Disk+VolumeInformation.swift in Sources */,
248255
10F3C7001F23D9B1006D42EF /* Disk+UIImage.swift in Sources */,
256+
261E777E2178916800B7DFB6 /* DiskErrors.m in Sources */,
249257
104611781F27B27E00BBDB3A /* Disk+Errors.swift in Sources */,
250258
);
251259
runOnlyForDeploymentPostprocessing = 0;

DiskExample/DiskExample/ViewController.swift

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ class ViewController: UIViewController {
6666
@IBAction func retrieveTapped(_ sender: Any) {
6767
// We'll keep things simple here by using try?, but it's good practice to handle Disk with do, catch, try blocks
6868
// so you can make sure everything is going according to plan.
69-
if let retrievedPosts = try? Disk.retrieve("posts.json", from: .documents, as: [Post].self) {
69+
do {
70+
let retrievedPosts = try Disk.retrieve("posts.json", from: .documents, as: [Post].self)
7071
// If you Option+Click 'retrievedPosts' above, you'll notice that its type is [Post]
7172
// Pretty neat, huh?
7273

@@ -77,6 +78,17 @@ class ViewController: UIViewController {
7778
self.resultsTextView.text = result
7879

7980
print("Retrieved posts from disk!")
81+
} catch DiskError.noFileFound {
82+
self.resultsTextView.text = "No file saved to disk yet!"
83+
print ("No file found to retrieve posts from.")
84+
} catch let error as NSError {
85+
fatalError("""
86+
Domain: \(error.domain)
87+
Code: \(error.code)
88+
Description: \(error.localizedDescription)
89+
Failure Reason: \(error.localizedFailureReason ?? "")
90+
Suggestions: \(error.localizedRecoverySuggestion ?? "")
91+
""")
8092
}
8193
}
8294

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,8 @@ do {
403403
if Disk.exists("posts.json", in: .documents) {
404404
try Disk.remove("posts.json", from: .documents)
405405
}
406+
} catch DiskError.noFileFound {
407+
// Pattern matching to catch specific errors.
406408
} catch let error as NSError {
407409
fatalError("""
408410
Domain: \(error.domain)

Sources/Disk+Errors.swift

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,13 @@
2323
import Foundation
2424

2525
extension Disk {
26-
public enum ErrorCode: Int {
27-
case noFileFound = 0
28-
case serialization = 1
29-
case deserialization = 2
30-
case invalidFileName = 3
31-
case couldNotAccessTemporaryDirectory = 4
32-
case couldNotAccessUserDomainMask = 5
33-
case couldNotAccessSharedContainer = 6
34-
}
35-
36-
public static let errorDomain = "DiskErrorDomain"
3726

3827
/// Create custom error that FileManager can't account for
39-
static func createError(_ errorCode: ErrorCode, description: String?, failureReason: String?, recoverySuggestion: String?) -> Error {
28+
static func createError(_ errorCode: DiskError.Code, description: String?, failureReason: String?, recoverySuggestion: String?) -> DiskError {
4029
let errorInfo: [String: Any] = [NSLocalizedDescriptionKey : description ?? "",
4130
NSLocalizedRecoverySuggestionErrorKey: recoverySuggestion ?? "",
4231
NSLocalizedFailureReasonErrorKey: failureReason ?? ""]
43-
return NSError(domain: errorDomain, code: errorCode.rawValue, userInfo: errorInfo) as Error
32+
return NSError(domain: DiskErrorDomain, code: errorCode.rawValue, userInfo: errorInfo) as! DiskError
4433
}
4534
}
4635

Sources/Disk.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@
1919
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2020
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2121
// THE SOFTWARE.
22-
//
23-
//#import <UIKit/UIKit.h>
24-
//
25-
////! Project version number for Disk.
26-
//FOUNDATION_EXPORT double DiskVersionNumber;
27-
//
28-
////! Project version string for Disk.
29-
//FOUNDATION_EXPORT const unsigned char DiskVersionString[];
22+
23+
#import <Foundation/Foundation.h>
24+
#import "DiskErrors.h"
25+
26+
//! Project version number for Disk.
27+
FOUNDATION_EXPORT double DiskVersionNumber;
28+
29+
//! Project version string for Disk.
30+
FOUNDATION_EXPORT const unsigned char DiskVersionString[];
3031

3132

Sources/DiskErrors.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//
2+
// DiskErrors.h
3+
// Disk
4+
//
5+
// Created by Sven Titgemeyer on 18.10.18.
6+
// Copyright © 2018 Saoud Rizwan. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
11+
/// Domain for Disk Errors.
12+
///
13+
/// Define Errors in Objective-C to get better Bridging between Swift/Objective-C.
14+
/// Errors are imported as `DiskError` in Swift.
15+
extern NSErrorDomain const DiskErrorDomain;
16+
typedef NS_ERROR_ENUM(DiskErrorDomain, DiskError) {
17+
DiskErrorNoFileFound = 0,
18+
DiskErrorSerialization = 1,
19+
DiskErrorDeserialization = 2,
20+
DiskErrorInvalidFileName = 3,
21+
DiskErrorCouldNotAccessTemporaryDirectory = 4,
22+
DiskErrorCouldNotAccessUserDomainMask = 5,
23+
DiskErrorCouldNotAccessSharedContainer = 6
24+
};

Sources/DiskErrors.m

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//
2+
// DiskErrors.m
3+
// Disk
4+
//
5+
// Created by Sven Titgemeyer on 18.10.18.
6+
// Copyright © 2018 Saoud Rizwan. All rights reserved.
7+
//
8+
9+
#import "DiskErrors.h"
10+
11+
NSErrorDomain const DiskErrorDomain = @"DiskErrorDomain";

Tests/DiskTests.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -805,7 +805,8 @@ class DiskTests: XCTestCase {
805805
try Disk.append(oneMessage, to: "Folder/", in: .documents)
806806
let _ = try Disk.retrieve("Folder/", from: .documents, as: [Message].self)
807807
} catch let error as NSError {
808-
XCTAssert(error.code == Disk.ErrorCode.invalidFileName.rawValue)
808+
XCTAssert(error is DiskError)
809+
XCTAssert(error.code == DiskError.invalidFileName.rawValue)
809810
}
810811
}
811812

0 commit comments

Comments
 (0)