Skip to content

Commit ed5e95c

Browse files
committed
added classNotFound exception
1 parent 5a70ee0 commit ed5e95c

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

Layout.swift

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import UIKit
1010

1111
public enum LayoutError: Error {
1212
case fileNotFound(String)
13+
case classNotFound(String)
1314
}
1415

1516
public typealias ViewOfTypeForID = (String, String) -> UIView?
@@ -23,7 +24,9 @@ public class Layout {
2324
private let layout: MarkupLayout
2425

2526
public init(name: String) throws {
26-
guard let url = Bundle.main.url(forResource: name, withExtension: "json") else { throw LayoutError.fileNotFound("\(name).json") }
27+
guard let url = Bundle.main.url(forResource: name, withExtension: "json") else {
28+
throw LayoutError.fileNotFound("\(name).json")
29+
}
2730
let data = try Data(contentsOf: url)
2831
let decoder = JSONDecoder()
2932
self.layout = try decoder.decode(MarkupLayout.self, from: data)
@@ -37,24 +40,26 @@ public class Layout {
3740
views: views)
3841
}
3942

40-
private func add(views: [String: MarkupElement], to root: UIView) {
43+
private func add(views: [String: MarkupElement], to root: UIView) throws {
4144
for (id, view) in views {
42-
guard let v = self.viewOfType(view.type, id) else { continue }
45+
guard let v = self.viewOfType(view.type, id) else {
46+
throw LayoutError.classNotFound(view.type)
47+
}
4348
self.didCreateView(v, id)
4449
v.translatesAutoresizingMaskIntoConstraints = false
4550
v.tag = id.hash
4651
self.views[id] = v
4752
root.addSubview(v)
4853
if let children = view.children {
4954
// recursion
50-
self.add(views: children, to: v)
55+
try self.add(views: children, to: v)
5156
}
5257
}
5358
}
5459

55-
public func inflate(in root: UIView) {
60+
public func inflate(in root: UIView) throws {
5661
root.translatesAutoresizingMaskIntoConstraints = false
57-
self.add(views: layout.views, to: root)
62+
try self.add(views: layout.views, to: root)
5863
self.metrics = layout.metrics
5964
for (id, constraint) in layout.constraints {
6065
self.addConstraint(id: id, visualFormat: constraint)

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ For further customization of views and constraints, implement some or all lambda
6363
try? Layout(name: "layout").configure {
6464
// customize format options
6565
$0.formatOptions = { (id) -> NSLayoutConstraint.FormatOptions in [] }
66-
// provide view yourself (if the class does not have default initializer) or fall back to the default implementation
66+
// provide view yourself or fall back to the default implementation
6767
$0.viewOfType = { (type, id) -> UIView? in return Layout.view(of: type) }
6868
// configure a newly created view
6969
$0.didCreateView = { (view, id) -> Void in return }

0 commit comments

Comments
 (0)