@@ -10,6 +10,7 @@ import UIKit
10
10
11
11
public enum LayoutError : Error {
12
12
case fileNotFound( String )
13
+ case classNotFound( String )
13
14
}
14
15
15
16
public typealias ViewOfTypeForID = ( String , String ) -> UIView ?
@@ -23,7 +24,9 @@ public class Layout {
23
24
private let layout : MarkupLayout
24
25
25
26
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
+ }
27
30
let data = try Data ( contentsOf: url)
28
31
let decoder = JSONDecoder ( )
29
32
self . layout = try decoder. decode ( MarkupLayout . self, from: data)
@@ -37,24 +40,26 @@ public class Layout {
37
40
views: views)
38
41
}
39
42
40
- private func add( views: [ String : MarkupElement ] , to root: UIView ) {
43
+ private func add( views: [ String : MarkupElement ] , to root: UIView ) throws {
41
44
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
+ }
43
48
self . didCreateView ( v, id)
44
49
v. translatesAutoresizingMaskIntoConstraints = false
45
50
v. tag = id. hash
46
51
self . views [ id] = v
47
52
root. addSubview ( v)
48
53
if let children = view. children {
49
54
// recursion
50
- self . add ( views: children, to: v)
55
+ try self . add ( views: children, to: v)
51
56
}
52
57
}
53
58
}
54
59
55
- public func inflate( in root: UIView ) {
60
+ public func inflate( in root: UIView ) throws {
56
61
root. translatesAutoresizingMaskIntoConstraints = false
57
- self . add ( views: layout. views, to: root)
62
+ try self . add ( views: layout. views, to: root)
58
63
self . metrics = layout. metrics
59
64
for (id, constraint) in layout. constraints {
60
65
self . addConstraint ( id: id, visualFormat: constraint)
0 commit comments