Skip to content

Commit

Permalink
Merge pull request #9 from smamczak/spm-bundle-support
Browse files Browse the repository at this point in the history
SPM bundle support
  • Loading branch information
Domas Nutautas authored Oct 16, 2020
2 parents da9e839 + 2f80fa8 commit 90f1b2f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
7 changes: 6 additions & 1 deletion NibView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
//

#if canImport(NibView)
import NibView #endif
import NibView
#endif
import UIKit

open class NibView: UIView, NibLoadable {
Expand All @@ -16,6 +17,10 @@ open class NibView: UIView, NibLoadable {
return String(describing: self)
}

open class var bundle: Bundle {
return Bundle(for: self)
}

open override func awakeAfter(using aDecoder: NSCoder) -> Any? {
return nibLoader.awakeAfter(using: aDecoder, super.awakeAfter(using: aDecoder))
}
Expand Down
15 changes: 12 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
// swift-tools-version:5.1
// swift-tools-version:5.3
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let files = [
let sourceFiles = [
"Sources/NibLoadable.swift",
"Sources/NibLoader.swift",
"Sources/NibViewController.swift",
"NibView.swift"
]

let excludedFiles = [
"Example",
"Img",
"README.md",
"CODEOWNERS",
"LICENSE"
]

let package = Package(
name: "NibView",
defaultLocalization: "en",
products: [
.library(name: "NibView", targets: ["NibView"])
],
targets: [
.target(name: "NibView", path: "", sources: files)
.target(name: "NibView", path: "", exclude: excludedFiles, sources: sourceFiles)
],
swiftLanguageVersions: [.v5]
)
12 changes: 10 additions & 2 deletions Sources/NibLoadable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ import UIKit
*Optionally* provide custom nib name (defaults to type name):

class var nibName: String { return "MyCustomView" }

*Optionally* provide custom bundle (defaults to class location):

class var bundle: Bundle { return Bundle(for: self) }

# Refencing from IB

Expand Down Expand Up @@ -84,6 +88,7 @@ import UIKit
*/
public protocol NibLoadable: class {
static var nibName: String { get }
static var bundle: Bundle { get }
}

// MARK: - From Nib
Expand All @@ -94,8 +99,12 @@ public extension NibLoadable where Self: UIView {
return String(describing: self)
}

static var bundle: Bundle {
return Bundle(for: self)
}

static func fromNib() -> Self {
guard let nib = Bundle(for: self).loadNibNamed(nibName, owner: nil, options: nil) else {
guard let nib = self.bundle.loadNibNamed(nibName, owner: nil, options: nil) else {
fatalError("Failed loading the nib named \(nibName) for 'NibLoadable' view of type '\(self)'.")
}
guard let view = (nib.first { $0 is Self }) as? Self else {
Expand All @@ -104,4 +113,3 @@ public extension NibLoadable where Self: UIView {
return view
}
}

0 comments on commit 90f1b2f

Please sign in to comment.