无法在捆绑 CocoaPods 中加载 NIB
Could not load NIB in bundle CocoaPods
我已经从 cocoapods 安装了 PSCarouselView 到我的项目中。当我尝试使用这个库时,我遇到了运行时错误。错误提示无法加载 nib 文件...
我检查了库,发现 nib 文件已正确注册。而且我还检查了目标成员资格并选中了复选框。然后我检查 podspec 文件,找不到任何关于该库的 podspec,然后我为该库创建了一个 pod 规范文件。使用下面的命令
pod spec create [PSCarouselView]https://github.com/DeveloperPans/PSCarouselView
然后我可以检查podspec文件并添加以下命令;
s.resources = "Resources/*.xib"
s.resource_bundles = {
'PSCarouselView' => [
'Pod/**/*.xib'
实际上,在我将此命令添加到 podspec 之前,xib 文件已复制到我的项目中。我只是拼命尝试...
并且xib文件用下面显示的代码注册;
[self registerNib:[UINib nibWithNibName:@"PSCarouselCollectionCell" bundle:nil] forCellWithReuseIdentifier:@"PSCarouselCollectionCell"];
标识符也定义正确;
所以现在我很绝望如何解决这个问题。
我在下面分享错误;
MyTestApp[18232:1316081] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle </Users/adminuser/Library/Developer/CoreSimulator/Devices/464AB3F8-3C31-4473-AD1B-F554F3F59CDB/data/Containers/Bundle/Application/B4A5F964-A6E0-4357-878F-11A299FD1ACA/MyTestApp.app> (loaded)' with name 'PSCarouselCollectionCell''
*** First throw call stack:
(
0 CoreFoundation 0x0000000101598d85 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010100cdeb objc_exception_throw + 48
2 CoreFoundation 0x0000000101598cbd +[NSException raise:format:] + 205
3 UIKit 0x00000001023dec89 -[UINib instantiateWithOwner:options:] + 499
4 UIKit 0x000000010289f553 -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:viewCategory:] + 716
5 UIKit 0x000000010289febc -[UICollectionView dequeueReusableCellWithReuseIdentifier:forIndexPath:] + 169
6 PSCarouselView 0x0000000100b13b92 -[PSCarouselView collectionView:cellForItemAtIndexPath:] + 114
7 UIKit 0x000000010288e08f -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:isFocused:] + 483
8 UIKit 0x0000000102891d96 -[UICollectionView _updateVisibleCellsNow:] + 4988
9 UIKit 0x0000000102896575 -[UICollectionView layoutSubviews] + 258
10 PSCarouselView 0x0000000100b13108 -[PSCarouselView layoutSubviews] + 328
11 UIKit 0x00000001020d1980 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 703
12 QuartzCore 0x0000000105065c00 -[CALayer layoutSublayers] + 146
13 QuartzCore 0x000000010505a08e _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
14 UIKit 0x00000001020c1205 -[UIView(Hierarchy) layoutBelowIfNeeded] + 1129
15 UIKit 0x00000001021f65c5 -[UINavigationController _layoutViewController:] + 1613
16 UIKit 0x00000001021f53b4 -[UINavigationController _layoutTopViewController] + 268
17 UIKit 0x00000001021f26bd -[UINavigationController navigationTransitionView:didEndTransition:fromView:toView:] + 856
18 UIKit 0x000000010249cc2c -[UINavigationTransitionView _notifyDelegateTransitionDidStopWithContext:] + 422
19 UIKit 0x000000010249cfa2 -[UINavigationTransitionView _cleanupTransition] + 788
20 UIKit 0x00000001020a47ff -[UIViewAnimationState sendDelegateAnimationDidStop:finished:] + 241
21 UIKit 0x00000001020a1706 +[UIViewAnimationState popAnimationState] + 314
22 UIKit 0x000000010249c8dd -[UINavigationTransitionView transition:fromView:toView:] + 2640
23 UIKit 0x00000001021f7651 -[UINavigationController _startTransition:fromViewController:toViewController:] + 3291
24 UIKit 0x00000001021f7c4d -[UINavigationController _startDeferredTransitionIfNeeded:] + 890
25 UIKit 0x00000001021f8d0b -[UINavigationController __viewWillLayoutSubviews] + 57
26 UIKit 0x00000001023a7503 -[UILayoutContainerView layoutSubviews] + 248
27 UIKit 0x00000001020d1980 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 703
28 QuartzCore 0x0000000105065c00 -[CALayer layoutSublayers] + 146
29 QuartzCore 0x000000010505a08e _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
30 QuartzCore 0x0000000105059f0c _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
31 QuartzCore 0x000000010504e3c9 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 277
32 QuartzCore 0x000000010507c086 _ZN2CA11Transaction6commitEv + 486
33 QuartzCore 0x000000010507c7f8 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 92
34 CoreFoundation 0x00000001014bdc37 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
35 CoreFoundation 0x00000001014bdba7 __CFRunLoopDoObservers + 391
36 CoreFoundation 0x00000001014b311c CFRunLoopRunSpecific + 524
37 UIKit 0x0000000102011f21 -[UIApplication _run] + 402
38 UIKit 0x0000000102016f09 UIApplicationMain + 171
39 MyTestApp 0x0000000100a598b2 main + 114
40 libdyld.dylib 0x0000000107d9a92d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
您需要包含 pod 的捆绑包。
例如在我的代码中(基于集合视图的货币符号选择器):
public class BGSCurrencyPickerVC: UIViewController,UICollectionViewDelegate {
public override func viewDidLoad() {
super.viewDidLoad()
// Register cell classes
let podBundle = NSBundle(path: NSBundle(forClass: BGSCurrencyPickerVC.self).pathForResource("BLAMCurrencyPicker", ofType: "bundle")!)
self.collectionView!.registerNib( UINib(nibName: "BGSCurrencyPickerCVCell", bundle: podBundle), forCellWithReuseIdentifier: "BGSCurrencyPickerCVCell")
查看示例 Collection View based swift cocoapod
再举个例子,Currency Picker cocoapod
对于Swift 3:
let podBundle = Bundle(path: Bundle(for: BLAMPaymentCVView.self).path(forResource: "BLAMPayment", ofType: "bundle")!)
如果与您的 .xib 捆绑包(本例中为 YourView.xib)在 pods 捆绑包中,您可以通过这种方式在文件所有者 UIView 子类中检索捆绑包。
let bundle = Bundle(for: self.classForCoder)
然后实例化视图:
nib.instantiate(withOwner: self, options: nil)
let nib = UINib(nibName: "YourView", bundle: bundle)
如果您想在 pod 模块之外子类化以这种方式加载的 UIView,最好使用下面的内容,因为 'self.classForCoder' 将为您提供子类所在的包。
let bundle = Bundle(for: <FileOwnerUIViewSubclass>.classForCoder())
我已经从 cocoapods 安装了 PSCarouselView 到我的项目中。当我尝试使用这个库时,我遇到了运行时错误。错误提示无法加载 nib 文件...
我检查了库,发现 nib 文件已正确注册。而且我还检查了目标成员资格并选中了复选框。然后我检查 podspec 文件,找不到任何关于该库的 podspec,然后我为该库创建了一个 pod 规范文件。使用下面的命令
pod spec create [PSCarouselView]https://github.com/DeveloperPans/PSCarouselView
然后我可以检查podspec文件并添加以下命令;
s.resources = "Resources/*.xib"
s.resource_bundles = {
'PSCarouselView' => [
'Pod/**/*.xib'
实际上,在我将此命令添加到 podspec 之前,xib 文件已复制到我的项目中。我只是拼命尝试...
并且xib文件用下面显示的代码注册;
[self registerNib:[UINib nibWithNibName:@"PSCarouselCollectionCell" bundle:nil] forCellWithReuseIdentifier:@"PSCarouselCollectionCell"];
标识符也定义正确;
所以现在我很绝望如何解决这个问题。
我在下面分享错误;
MyTestApp[18232:1316081] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle </Users/adminuser/Library/Developer/CoreSimulator/Devices/464AB3F8-3C31-4473-AD1B-F554F3F59CDB/data/Containers/Bundle/Application/B4A5F964-A6E0-4357-878F-11A299FD1ACA/MyTestApp.app> (loaded)' with name 'PSCarouselCollectionCell''
*** First throw call stack:
(
0 CoreFoundation 0x0000000101598d85 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010100cdeb objc_exception_throw + 48
2 CoreFoundation 0x0000000101598cbd +[NSException raise:format:] + 205
3 UIKit 0x00000001023dec89 -[UINib instantiateWithOwner:options:] + 499
4 UIKit 0x000000010289f553 -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:viewCategory:] + 716
5 UIKit 0x000000010289febc -[UICollectionView dequeueReusableCellWithReuseIdentifier:forIndexPath:] + 169
6 PSCarouselView 0x0000000100b13b92 -[PSCarouselView collectionView:cellForItemAtIndexPath:] + 114
7 UIKit 0x000000010288e08f -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:isFocused:] + 483
8 UIKit 0x0000000102891d96 -[UICollectionView _updateVisibleCellsNow:] + 4988
9 UIKit 0x0000000102896575 -[UICollectionView layoutSubviews] + 258
10 PSCarouselView 0x0000000100b13108 -[PSCarouselView layoutSubviews] + 328
11 UIKit 0x00000001020d1980 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 703
12 QuartzCore 0x0000000105065c00 -[CALayer layoutSublayers] + 146
13 QuartzCore 0x000000010505a08e _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
14 UIKit 0x00000001020c1205 -[UIView(Hierarchy) layoutBelowIfNeeded] + 1129
15 UIKit 0x00000001021f65c5 -[UINavigationController _layoutViewController:] + 1613
16 UIKit 0x00000001021f53b4 -[UINavigationController _layoutTopViewController] + 268
17 UIKit 0x00000001021f26bd -[UINavigationController navigationTransitionView:didEndTransition:fromView:toView:] + 856
18 UIKit 0x000000010249cc2c -[UINavigationTransitionView _notifyDelegateTransitionDidStopWithContext:] + 422
19 UIKit 0x000000010249cfa2 -[UINavigationTransitionView _cleanupTransition] + 788
20 UIKit 0x00000001020a47ff -[UIViewAnimationState sendDelegateAnimationDidStop:finished:] + 241
21 UIKit 0x00000001020a1706 +[UIViewAnimationState popAnimationState] + 314
22 UIKit 0x000000010249c8dd -[UINavigationTransitionView transition:fromView:toView:] + 2640
23 UIKit 0x00000001021f7651 -[UINavigationController _startTransition:fromViewController:toViewController:] + 3291
24 UIKit 0x00000001021f7c4d -[UINavigationController _startDeferredTransitionIfNeeded:] + 890
25 UIKit 0x00000001021f8d0b -[UINavigationController __viewWillLayoutSubviews] + 57
26 UIKit 0x00000001023a7503 -[UILayoutContainerView layoutSubviews] + 248
27 UIKit 0x00000001020d1980 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 703
28 QuartzCore 0x0000000105065c00 -[CALayer layoutSublayers] + 146
29 QuartzCore 0x000000010505a08e _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
30 QuartzCore 0x0000000105059f0c _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
31 QuartzCore 0x000000010504e3c9 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 277
32 QuartzCore 0x000000010507c086 _ZN2CA11Transaction6commitEv + 486
33 QuartzCore 0x000000010507c7f8 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 92
34 CoreFoundation 0x00000001014bdc37 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
35 CoreFoundation 0x00000001014bdba7 __CFRunLoopDoObservers + 391
36 CoreFoundation 0x00000001014b311c CFRunLoopRunSpecific + 524
37 UIKit 0x0000000102011f21 -[UIApplication _run] + 402
38 UIKit 0x0000000102016f09 UIApplicationMain + 171
39 MyTestApp 0x0000000100a598b2 main + 114
40 libdyld.dylib 0x0000000107d9a92d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
您需要包含 pod 的捆绑包。
例如在我的代码中(基于集合视图的货币符号选择器):
public class BGSCurrencyPickerVC: UIViewController,UICollectionViewDelegate {
public override func viewDidLoad() {
super.viewDidLoad()
// Register cell classes
let podBundle = NSBundle(path: NSBundle(forClass: BGSCurrencyPickerVC.self).pathForResource("BLAMCurrencyPicker", ofType: "bundle")!)
self.collectionView!.registerNib( UINib(nibName: "BGSCurrencyPickerCVCell", bundle: podBundle), forCellWithReuseIdentifier: "BGSCurrencyPickerCVCell")
查看示例 Collection View based swift cocoapod
再举个例子,Currency Picker cocoapod
对于Swift 3:
let podBundle = Bundle(path: Bundle(for: BLAMPaymentCVView.self).path(forResource: "BLAMPayment", ofType: "bundle")!)
如果与您的 .xib 捆绑包(本例中为 YourView.xib)在 pods 捆绑包中,您可以通过这种方式在文件所有者 UIView 子类中检索捆绑包。
let bundle = Bundle(for: self.classForCoder)
然后实例化视图:
nib.instantiate(withOwner: self, options: nil)
let nib = UINib(nibName: "YourView", bundle: bundle)
如果您想在 pod 模块之外子类化以这种方式加载的 UIView,最好使用下面的内容,因为 'self.classForCoder' 将为您提供子类所在的包。
let bundle = Bundle(for: <FileOwnerUIViewSubclass>.classForCoder())