Skip to content

Commit 847da7e

Browse files
authored
Merge pull request #163 from kirillyakimovich/master
Typos and Playground fixes
2 parents 1878b76 + fe9ef7e commit 847da7e

File tree

9 files changed

+147
-136
lines changed

9 files changed

+147
-136
lines changed

Cache.xcodeproj/project.pbxproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,11 @@
198198
D5291D821C283C7000B702C9 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
199199
D5291DA01C28405900B702C9 /* UIImage+ExtensionsTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIImage+ExtensionsTests.swift"; sourceTree = "<group>"; };
200200
D5291DA21C2841D200B702C9 /* NSImage+ExtensionsTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "NSImage+ExtensionsTests.swift"; sourceTree = "<group>"; };
201-
D5643E361C43F2CC00582E17 /* HybridCache.playground */ = {isa = PBXFileReference; lastKnownFileType = file.playground; path = HybridCache.playground; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
202-
D59A19EB1EE82B44009F6AEE /* SpecializedCache.playground */ = {isa = PBXFileReference; lastKnownFileType = file.playground; path = SpecializedCache.playground; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
201+
D5643E361C43F2CC00582E17 /* Storage.playground */ = {isa = PBXFileReference; lastKnownFileType = file.playground; path = Storage.playground; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
203202
D5A138C01EB29BFA00881A20 /* UIImage+Extensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIImage+Extensions.swift"; sourceTree = "<group>"; };
204203
D5A138C31EB29C2100881A20 /* NSImage+Extensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "NSImage+Extensions.swift"; sourceTree = "<group>"; };
205204
D5DC59E01C20593E003BD79B /* Cache.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Cache.framework; sourceTree = BUILT_PRODUCTS_DIR; };
205+
EBAACA991FBC369300FA206E /* SimpleStorage.playground */ = {isa = PBXFileReference; lastKnownFileType = file.playground; path = SimpleStorage.playground; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
206206
/* End PBXFileReference section */
207207

208208
/* Begin PBXFrameworksBuildPhase section */
@@ -439,8 +439,8 @@
439439
D5643E341C43F2CC00582E17 /* Playgrounds */ = {
440440
isa = PBXGroup;
441441
children = (
442-
D5643E361C43F2CC00582E17 /* HybridCache.playground */,
443-
D59A19EB1EE82B44009F6AEE /* SpecializedCache.playground */,
442+
EBAACA991FBC369300FA206E /* SimpleStorage.playground */,
443+
D5643E361C43F2CC00582E17 /* Storage.playground */,
444444
);
445445
path = Playgrounds;
446446
sourceTree = "<group>";

Playgrounds/HybridCache.playground/Contents.swift

Lines changed: 0 additions & 72 deletions
This file was deleted.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//: Playground - noun: a place where people can play
2+
import PlaygroundSupport
3+
import UIKit
4+
import Cache
5+
6+
struct User: Codable {
7+
let id: Int
8+
let firstName: String
9+
let lastName: String
10+
11+
var name: String {
12+
return "\(firstName) \(lastName)"
13+
}
14+
}
15+
16+
let diskConfig = DiskConfig(name: "UserCache")
17+
let memoryConfig = MemoryConfig(expiry: .never, countLimit: 10, totalCostLimit: 10)
18+
19+
let storage = try! Storage(diskConfig: diskConfig, memoryConfig: memoryConfig)
20+
21+
let user = User(id: 1, firstName: "John", lastName: "Snow")
22+
let key = "\(user.id)"
23+
24+
// Add objects to the cache
25+
try storage.setObject(user, forKey: key)
26+
27+
// Fetch object from the cache
28+
storage.async.object(ofType: User.self, forKey: key) { result in
29+
switch result {
30+
case .value(let user):
31+
print(user.name)
32+
case .error(let error):
33+
print(error)
34+
}
35+
}
36+
37+
// Remove object from the cache
38+
try storage.removeObject(forKey: key)
39+
40+
// Try to fetch removed object from the cache
41+
storage.async.object(ofType: User.self, forKey: key) { result in
42+
switch result {
43+
case .value(let user):
44+
print(user.name)
45+
case .error:
46+
print("no such object")
47+
}
48+
}
49+
50+
// Clear cache
51+
try storage.removeAll()
52+
53+
PlaygroundPage.current.needsIndefiniteExecution = true

Playgrounds/HybridCache.playground/contents.xcplayground renamed to Playgrounds/SimpleStorage.playground/contents.xcplayground

File renamed without changes.

Playgrounds/SpecializedCache.playground/Contents.swift

Lines changed: 0 additions & 58 deletions
This file was deleted.
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
//: Playground - noun: a place where people can play
2+
import PlaygroundSupport
3+
import UIKit
4+
import Cache
5+
6+
struct Helper {
7+
static func image(_ color: UIColor = .red, size: CGSize = .init(width: 1, height: 1)) -> UIImage {
8+
UIGraphicsBeginImageContextWithOptions(size, false, 1)
9+
10+
let context = UIGraphicsGetCurrentContext()
11+
context?.setFillColor(color.cgColor)
12+
context?.fill(CGRect(x: 0, y: 0, width: size.width, height: size.height))
13+
14+
let image = UIGraphicsGetImageFromCurrentImageContext()
15+
UIGraphicsEndImageContext()
16+
17+
return image!
18+
}
19+
20+
static func data(length: Int) -> Data {
21+
var buffer = [UInt8](repeating: 0, count: length)
22+
return Data(bytes: &buffer, count: length)
23+
}
24+
}
25+
26+
// MARK: - Storage
27+
28+
let diskConfig = DiskConfig(name: "Mix")
29+
30+
let storage = try! Storage(diskConfig: diskConfig)
31+
32+
// We already have Codable conformances for:
33+
// String, UIImage, NSData and NSDate (just for fun =)
34+
35+
let string = "This is a string"
36+
let image = Helper.image()
37+
let imageWrapper = ImageWrapper(image: image)
38+
let newImage = imageWrapper.image
39+
let data = Helper.data(length: 64)
40+
let date = Date(timeInterval: 100000, since: Date())
41+
42+
// Add objects to the cache
43+
try storage.setObject(string, forKey: "string")
44+
try storage.setObject(imageWrapper, forKey: "imageWrapper")
45+
try storage.setObject(data, forKey: "data")
46+
try storage.setObject(date, forKey: "date")
47+
//
48+
//// Get objects from the cache
49+
let cachedString = try? storage.object(ofType: String.self, forKey: "string")
50+
print(cachedString)
51+
52+
storage.async.object(ofType: ImageWrapper.self, forKey: "imageWrapper") { result in
53+
if case .value(let imageWrapper) = result {
54+
let image = imageWrapper.image
55+
print(image)
56+
}
57+
}
58+
59+
storage.async.object(ofType: Data.self, forKey: "data") { result in
60+
if case .value(let data) = result {
61+
print(data)
62+
}
63+
}
64+
65+
storage.async.object(ofType: Data.self, forKey: "data") { result in
66+
if case .value(let data) = result {
67+
print(data)
68+
}
69+
}
70+
71+
storage.async.object(ofType: Date.self, forKey: "date") { result in
72+
if case .value(let date) = result {
73+
print(date)
74+
}
75+
}
76+
77+
// Clean the cache
78+
try storage.async.removeAll(completion: { (result) in
79+
if case .value = result {
80+
print("Cache cleaned")
81+
}
82+
})
83+
84+
PlaygroundPage.current.needsIndefiniteExecution = true

Playgrounds/SpecializedCache.playground/contents.xcplayground renamed to Playgrounds/Storage.playground/contents.xcplayground

File renamed without changes.

Playgrounds/Storage.playground/playground.xcworkspace/contents.xcworkspacedata

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ storage.async.setObject("Oslo", forKey: "my favorite city") { result in
210210
switch result {
211211
case .value:
212212
print("saved successfully")
213-
case .error:
213+
case .error(let error):
214214
print(error)
215215
}
216216
}
@@ -220,7 +220,7 @@ storage.async.object(ofType: String.self, forKey: "my favorite city") { result i
220220
switch result {
221221
case .value(let city):
222222
print("my favorite city is \(city)")
223-
case .error:
223+
case .error(let error):
224224
print(error)
225225
}
226226
}

0 commit comments

Comments
 (0)