Skip to content

Commit de07cc8

Browse files
authored
Merge pull request #17 from Squarespace/avyazovoy/example-project-improvements
Example project improvements
2 parents bbe56a6 + d6ee24d commit de07cc8

8 files changed

Lines changed: 50 additions & 87 deletions

File tree

.github/fixed_podspecs/SwiftyJSON.podspec

Lines changed: 0 additions & 17 deletions
This file was deleted.

.github/workflows/ci.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ jobs:
2929
run: |
3030
bundle exec pod lib lint \
3131
--allow-warnings \
32-
--external-podspecs=".github/fixed_podspecs/**/*.podspec" \
3332
--validation-dir="${{ steps.define_validation_dir.outputs.path }}" \
3433
--no-clean
3534

Examples/Podfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ target 'SimpleSourceExample' do
44
use_frameworks!
55

66
pod 'SimpleSource', :path => '..', :testspecs => ['Tests']
7-
pod 'SwiftyJSON', '~> 5.0'
87
end
98

109
def inherit_project_deployment_target(installer)

Examples/Podfile.lock

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,15 @@ PODS:
55
- SimpleSource/Tests (3.0.0):
66
- Nimble (~> 12.0)
77
- Quick (~> 7.0)
8-
- SwiftyJSON (5.0.1)
98

109
DEPENDENCIES:
1110
- SimpleSource (from `..`)
1211
- SimpleSource/Tests (from `..`)
13-
- SwiftyJSON (~> 5.0)
1412

1513
SPEC REPOS:
1614
trunk:
1715
- Nimble
1816
- Quick
19-
- SwiftyJSON
2017

2118
EXTERNAL SOURCES:
2219
SimpleSource:
@@ -26,8 +23,7 @@ SPEC CHECKSUMS:
2623
Nimble: f8a8219d16f176429b951e8f7e72df5c23ceddc0
2724
Quick: d32871931c05547cb4e0bc9009d66a18b50d8558
2825
SimpleSource: 611bfe53fe55132415a3c72a9a0126f9aa846d8b
29-
SwiftyJSON: 2f33a42c6fbc52764d96f13368585094bfd8aa5e
3026

31-
PODFILE CHECKSUM: a5deddba5204624da3222bdb26450e64fabfabc5
27+
PODFILE CHECKSUM: 5ebdafbbdb9020f19e05fb40cff22a580d64a844
3228

3329
COCOAPODS: 1.13.0

Examples/SimpleSourceExample.xcodeproj/xcshareddata/xcschemes/SimpleSourceExample.xcscheme

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@
4040
buildConfiguration = "Debug"
4141
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
4242
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
43-
shouldUseLaunchSchemeArgsEnv = "YES">
43+
shouldUseLaunchSchemeArgsEnv = "YES"
44+
codeCoverageEnabled = "YES">
4445
<MacroExpansion>
4546
<BuildableReference
4647
BuildableIdentifier = "primary"
Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,20 @@
11
import Foundation
22
import CoreData
3-
import SwiftyJSON
43

54
/// A helper class to load some content into an `NSManagedObjectContext`.
65
struct CharacterLoader {
76
static func load(into context: NSManagedObjectContext) {
87
let dataURL = Bundle.main.url(forResource: "characters", withExtension: "json")!
98
let data = try! Data(contentsOf: dataURL)
10-
try! JSON(data: data).arrayValue.forEach {
11-
loadCharacter(json: $0, into: context)
9+
try! JSONDecoder().decode([CharacterRepresentation].self, from: data).forEach { representation in
10+
let character = Character(context: context)
11+
character.name = representation.name
12+
character.race = representation.race
1213
}
1314
}
1415

15-
private static func loadCharacter(json: JSON, into context: NSManagedObjectContext) {
16-
guard
17-
let name = json["name"].string,
18-
let race = json["race"].string
19-
else { return }
20-
21-
let character = Character(context: context)
22-
character.name = name
23-
character.race = race
16+
private struct CharacterRepresentation: Decodable {
17+
let name: String
18+
let race: String
2419
}
2520
}
Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,11 @@
11
import Foundation
2-
import SwiftyJSON
32

43
struct ColorLoader {
54

65
/// Loads colors (organized into sections) from a JSON file.
76
static func loadSections() -> [ColorSection] {
87
let dataURL = Bundle.main.url(forResource: "colors", withExtension: "json")!
98
let data = try! Data(contentsOf: dataURL)
10-
return try! JSON(data: data).arrayValue.compactMap(parseSection)
11-
}
12-
13-
// MARK: Private
14-
15-
private static func parseSection(json: JSON) -> ColorSection? {
16-
guard
17-
let title = json["title"].string,
18-
let colors = json["colors"].array
19-
else { return nil }
20-
21-
return ColorSection(title: title, items: colors.compactMap(parseItem))
22-
}
23-
24-
private static func parseItem(json: JSON) -> ColorItem? {
25-
guard
26-
let name = json["name"].string,
27-
let rgbString = json["rgb"].string,
28-
let rgb = parseRGB(rgbString: rgbString)
29-
else { return nil }
30-
31-
return ColorItem(name: name, rgb: rgb)
32-
}
33-
34-
private static func parseRGB(rgbString: String) -> (CGFloat, CGFloat, CGFloat)? {
35-
let r, g, b: CGFloat
36-
37-
if rgbString.hasPrefix("#") {
38-
let start = rgbString.index(rgbString.startIndex, offsetBy: 1)
39-
let hexColor = String(rgbString[start...])
40-
41-
if hexColor.lengthOfBytes(using: .utf8) == 6 {
42-
let scanner = Scanner(string: hexColor)
43-
var hexNumber: UInt64 = 0
44-
45-
if scanner.scanHexInt64(&hexNumber) {
46-
r = CGFloat((hexNumber & 0xff0000) >> 16) / 255
47-
g = CGFloat((hexNumber & 0x00ff00) >> 8) / 255
48-
b = CGFloat((hexNumber & 0x0000ff) >> 0) / 255
49-
50-
return (r, g, b)
51-
}
52-
}
53-
}
54-
55-
return nil
9+
return try! JSONDecoder().decode([ColorSection].self, from: data)
5610
}
5711
}

Examples/SimpleSourceExample/Colors.swift

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,16 @@ extension ColorSection: IdentifiableSection {
1313
var sectionIdentifier: String { return title }
1414
}
1515

16+
extension ColorSection: Decodable {
17+
enum CodingKeys: String, CodingKey {
18+
case title
19+
case items = "colors"
20+
}
21+
}
22+
1623
// MARK: Items
1724

18-
struct ColorItem: Equatable {
25+
struct ColorItem {
1926
let name: String
2027
let rgb: (CGFloat, CGFloat, CGFloat)
2128

@@ -24,7 +31,36 @@ struct ColorItem: Equatable {
2431
}
2532
}
2633

27-
func ==(lhs: ColorItem, rhs: ColorItem) -> Bool {
28-
return lhs.name == rhs.name && lhs.rgb == rhs.rgb
34+
extension ColorItem: Equatable {
35+
static func == (lhs: ColorItem, rhs: ColorItem) -> Bool {
36+
lhs.name == rhs.name && lhs.rgb == rhs.rgb
37+
}
2938
}
3039

40+
extension ColorItem: Decodable {
41+
enum Errors: Error {
42+
case rgbDecodingFailure
43+
}
44+
enum CodingKeys: String, CodingKey {
45+
case name
46+
case rgb
47+
}
48+
49+
init(from decoder: Decoder) throws {
50+
let container = try decoder.container(keyedBy: CodingKeys.self)
51+
name = try container.decode(String.self, forKey: .name)
52+
let scanner = try Scanner(string: container.decode(String.self, forKey: .rgb))
53+
guard
54+
scanner.scanCharacters(from: .init(charactersIn: "#")) == "#",
55+
let rgbUInt64 = scanner.scanUInt64(representation: .hexadecimal)
56+
else {
57+
print("Name: \(name)\nRGBString: \(scanner.string)")
58+
throw Errors.rgbDecodingFailure
59+
}
60+
rgb = (
61+
CGFloat((rgbUInt64 & 0xff0000) >> 16) / 255,
62+
CGFloat((rgbUInt64 & 0x00ff00) >> 8) / 255,
63+
CGFloat((rgbUInt64 & 0x0000ff) >> 0) / 255
64+
)
65+
}
66+
}

0 commit comments

Comments
 (0)