Skip to content
This repository was archived by the owner on Sep 19, 2024. It is now read-only.

Commit 115fca0

Browse files
committed
Cleanup + FileResource methods on NSBundle
1 parent 4c70388 commit 115fca0

8 files changed

+64
-23
lines changed

Library/Core/FileResource.swift

-4
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@ public struct FileResource {
1818
/// Extension of the file on disk
1919
public let pathExtension: String
2020

21-
public var url: NSURL? {
22-
return bundle?.URLForResource(name, withExtension: pathExtension, subdirectory: nil, localization: nil)
23-
}
24-
2521
public init(bundle: NSBundle?, name: String, pathExtension: String) {
2622
self.bundle = bundle
2723
self.name = name

Library/Core/Validatable.swift

+10-1
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,29 @@
88

99
import Foundation
1010

11+
/// Error thrown during validation
1112
public struct ValidationError: ErrorType, CustomStringConvertible {
13+
/// Human readable description
1214
public let description: String
1315

1416
public init(description: String) {
1517
self.description = description
1618
}
1719
}
1820

19-
/// Validates this entity and throws if it encounters a invalid situation, a validatable should also validate it sub-validatables if it has any. Only things that can't be validated compile time, but result in an invalid situation should be validated
2021
public protocol Validatable {
22+
/**
23+
Validates this entity and throws if it encounters a invalid situation, a validatable should also validate it sub-validatables if it has any.
24+
25+
- throws: If there the configuration error a ValidationError is thrown
26+
*/
2127
static func validate() throws
2228
}
2329

2430
extension Validatable {
31+
/**
32+
Validates this entity and asserts if it encounters a invalid situation, a validatable should also validate it sub-validatables if it has any. In -O builds (the default for Xcode's Release configuration), validation is not evaluated, and there are no effects.
33+
*/
2534
public static func assertValid() {
2635
assert( theRealAssert() )
2736
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//
2+
// NSBundle+FileResource.swift
3+
// R.swift.Library
4+
//
5+
// Created by Mathijs Kadijk on 10-01-16.
6+
// Copyright © 2016 Mathijs Kadijk. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
public extension NSBundle {
12+
/**
13+
Returns the file URL for the given resource (R.file.*).
14+
15+
- parameter resource: The resource to get the file URL for (R.file.*).
16+
17+
- returns: The file URL for the resource file (R.file.*) or nil if the file could not be located.
18+
*/
19+
public func URLForResource(resource: FileResource) -> NSURL? {
20+
return URLForResource(resource.name, withExtension: resource.pathExtension, subdirectory: nil, localization: nil)
21+
}
22+
23+
/**
24+
Returns the full pathname for the resource (R.file.*).
25+
26+
- parameter resource: The resource file to get the path for (R.file.*).
27+
28+
- returns: The full pathname for the resource file (R.file.*) or nil if the file could not be located.
29+
*/
30+
public func pathForResource(resource: FileResource) -> String? {
31+
return pathForResource(resource.name, ofType: resource.pathExtension, inDirectory: nil, forLocalization: nil)
32+
}
33+
}

Library/UIKit/NibResource+UIKit.swift

+3-12
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,14 @@ import Foundation
1010

1111
public extension NibResource {
1212
/**
13-
Create a new instance of this nib file
14-
15-
- returns: A new instance of this nib
16-
*/
17-
public func initialize() -> UINib {
18-
return UINib.init(nibName: name, bundle: bundle)
19-
}
20-
21-
/**
22-
Instantiate the nib to get the top-level object from this nib
13+
Instantiate the nib to get the top-level objects from this nib
2314

2415
- parameter ownerOrNil: The owner, if the owner parameter is nil, connections to File's Owner are not permitted.
2516
- parameter options: Options are identical to the options specified with -[NSBundle loadNibNamed:owner:options:]
2617

2718
- returns: An array containing the top-level objects from the NIB
2819
*/
29-
public func instantiateWithOwner(ownerOrNil: AnyObject?, options optionsOrNil: [NSObject : AnyObject]?) -> [AnyObject] {
30-
return initialize().instantiateWithOwner(ownerOrNil, options: optionsOrNil)
20+
public func instantiateWithOwner(ownerOrNil: AnyObject?, options optionsOrNil: [NSObject : AnyObject]? = nil) -> [AnyObject] {
21+
return UINib(resource: self).instantiateWithOwner(ownerOrNil, options: optionsOrNil)
3122
}
3223
}

Library/UIKit/UICollectionView+ReuseIdentifierProtocol.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public extension UICollectionView {
5050
- parameter nibResource: A nib resource (R.nib.*) containing a object of type UICollectionViewCell that has a reuse identifier
5151
*/
5252
public func registerNib<Resource: NibResource where Resource: ReuseIdentifierProtocol, Resource.ReusableType: UICollectionViewCell>(nibResource: Resource) {
53-
registerNib(nibResource.initialize(), forCellWithReuseIdentifier: nibResource.identifier)
53+
registerNib(UINib(resource: nibResource), forCellWithReuseIdentifier: nibResource.identifier)
5454
}
5555

5656
/**
@@ -68,6 +68,6 @@ public extension UICollectionView {
6868
- parameter nibResource: A nib resource (R.nib.*) containing a object of type UICollectionReusableView. that has a reuse identifier
6969
*/
7070
public func registerNib<Resource: NibResource where Resource: ReuseIdentifierProtocol, Resource.ReusableType: UICollectionReusableView>(nibResource: Resource, forSupplementaryViewOfKind kind: String) {
71-
registerNib(nibResource.initialize(), forSupplementaryViewOfKind: kind, withReuseIdentifier: nibResource.identifier)
71+
registerNib(UINib(resource: nibResource), forSupplementaryViewOfKind: kind, withReuseIdentifier: nibResource.identifier)
7272
}
7373
}

Library/UIKit/UINib+NibResource.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88

99
import UIKit
1010

11-
extension UINib {
11+
public extension UINib {
1212
/**
1313
Returns a UINib object initialized to the nib file of the specified resource (R.nib.*).
1414

1515
- parameter resource: The resource (R.nib.*) to load
1616

1717
- returns: The initialized UINib object. An exception is thrown if there were errors during initialization or the nib file could not be located.
1818
*/
19-
convenience init(resource: NibResource) {
19+
public convenience init(resource: NibResource) {
2020
self.init(nibName: resource.name, bundle: resource.bundle)
2121
}
2222
}

Library/UIKit/UITableView+ReuseIdentifierProtocol.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public extension UITableView {
6363
- parameter nibResource: A nib resource (R.nib.*) containing a table view cell that has a reuse identifier
6464
*/
6565
public func registerNib<Resource: NibResource where Resource: ReuseIdentifierProtocol, Resource.ReusableType: UITableViewCell>(nibResource: Resource) {
66-
registerNib(nibResource.initialize(), forCellReuseIdentifier: nibResource.identifier)
66+
registerNib(UINib(resource: nibResource), forCellReuseIdentifier: nibResource.identifier)
6767
}
6868

6969
/**
@@ -72,6 +72,6 @@ public extension UITableView {
7272
- parameter nibResource: A nib resource (R.nib.*) containing a view that has a reuse identifier
7373
*/
7474
public func registerNibForHeaderFooterView<Resource: NibResource where Resource: ReuseIdentifierProtocol, Resource.ReusableType: UIView>(nibResource: Resource) {
75-
registerNib(nibResource.initialize(), forHeaderFooterViewReuseIdentifier: nibResource.identifier)
75+
registerNib(UINib(resource: nibResource), forHeaderFooterViewReuseIdentifier: nibResource.identifier)
7676
}
7777
}

R.swift.Library.xcodeproj/project.pbxproj

+12
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
D543F9CC1C1499AB00D16A0C /* UIStoryboardSegue+StoryboardSegueIdentifierProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = D543F9CB1C1499AB00D16A0C /* UIStoryboardSegue+StoryboardSegueIdentifierProtocol.swift */; };
2020
D543F9CF1C149C0A00D16A0C /* TypedStoryboardSegueInfo+UIStoryboardSegue.swift in Sources */ = {isa = PBXBuildFile; fileRef = D543F9CE1C149C0A00D16A0C /* TypedStoryboardSegueInfo+UIStoryboardSegue.swift */; };
2121
D5588CAB1C3F9DBE00912F97 /* UINib+NibResource.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5588CAA1C3F9DBE00912F97 /* UINib+NibResource.swift */; };
22+
D56DC7731C42B65C00623437 /* NSBundle+FileResource.swift in Sources */ = {isa = PBXBuildFile; fileRef = D56DC7721C42B65C00623437 /* NSBundle+FileResource.swift */; };
2223
D57E1EB31C3D762300DDA68F /* FontResource.swift in Sources */ = {isa = PBXBuildFile; fileRef = D57E1EB21C3D762300DDA68F /* FontResource.swift */; };
2324
D57E1EB51C3D774000DDA68F /* UIFont+FontResource.swift in Sources */ = {isa = PBXBuildFile; fileRef = D57E1EB41C3D774000DDA68F /* UIFont+FontResource.swift */; };
2425
D57E1EB71C3E482A00DDA68F /* StoryboardResource.swift in Sources */ = {isa = PBXBuildFile; fileRef = D57E1EB61C3E482A00DDA68F /* StoryboardResource.swift */; };
@@ -54,6 +55,7 @@
5455
D543F9CB1C1499AB00D16A0C /* UIStoryboardSegue+StoryboardSegueIdentifierProtocol.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIStoryboardSegue+StoryboardSegueIdentifierProtocol.swift"; sourceTree = "<group>"; };
5556
D543F9CE1C149C0A00D16A0C /* TypedStoryboardSegueInfo+UIStoryboardSegue.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "TypedStoryboardSegueInfo+UIStoryboardSegue.swift"; sourceTree = "<group>"; };
5657
D5588CAA1C3F9DBE00912F97 /* UINib+NibResource.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UINib+NibResource.swift"; sourceTree = "<group>"; };
58+
D56DC7721C42B65C00623437 /* NSBundle+FileResource.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "NSBundle+FileResource.swift"; sourceTree = "<group>"; };
5759
D57E1EB21C3D762300DDA68F /* FontResource.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FontResource.swift; sourceTree = "<group>"; };
5860
D57E1EB41C3D774000DDA68F /* UIFont+FontResource.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIFont+FontResource.swift"; sourceTree = "<group>"; };
5961
D57E1EB61C3E482A00DDA68F /* StoryboardResource.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StoryboardResource.swift; sourceTree = "<group>"; };
@@ -121,6 +123,14 @@
121123
path = Core;
122124
sourceTree = "<group>";
123125
};
126+
D56DC7711C42B62E00623437 /* Foundation */ = {
127+
isa = PBXGroup;
128+
children = (
129+
D56DC7721C42B65C00623437 /* NSBundle+FileResource.swift */,
130+
);
131+
path = Foundation;
132+
sourceTree = "<group>";
133+
};
124134
D59246441C117A54007F94C7 = {
125135
isa = PBXGroup;
126136
children = (
@@ -143,6 +153,7 @@
143153
isa = PBXGroup;
144154
children = (
145155
D543F9CD1C1499CF00D16A0C /* Core */,
156+
D56DC7711C42B62E00623437 /* Foundation */,
146157
D543F9C21C14987000D16A0C /* UIKit */,
147158
D59246511C117A55007F94C7 /* Rswift.h */,
148159
D59246531C117A55007F94C7 /* Info.plist */,
@@ -285,6 +296,7 @@
285296
D5E435A91C3CFB460091090C /* NibResource+UIKit.swift in Sources */,
286297
D543F9CF1C149C0A00D16A0C /* TypedStoryboardSegueInfo+UIStoryboardSegue.swift in Sources */,
287298
D543F9C41C1498FB00D16A0C /* UITableView+ReuseIdentifierProtocol.swift in Sources */,
299+
D56DC7731C42B65C00623437 /* NSBundle+FileResource.swift in Sources */,
288300
D53F19241C229D7200AE2FAD /* Validatable.swift in Sources */,
289301
);
290302
runOnlyForDeploymentPostprocessing = 0;

0 commit comments

Comments
 (0)