是否可以从 swift 包加载 nib 文件?

Pra*_*der 4 package-managers ios uicollectionview swift-package-manager ios14

我创建了一个 UICollectionView Xib 文件和类文件作为 swift 包。类文件中的方法是可以访问的,但我在项目中加载 nib 文件时遇到错误。

包链接: https: //github.com/PranayChander/package1

项目链接: https: //github.com/PranayChander/packman

导入 UIKit 导入包1

class ViewController: UIViewController {

    @IBOutlet weak var collectionView: UICollectionView!
    override func viewDidLoad() {
        super.viewDidLoad()
        collectionView.register(UINib(nibName: "PackageCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "cell")
    }
}

extension ViewController: UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 10
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! PackageCollectionViewCell
        cell.configureCell(title: "Pram", subtitle: "dasd")
        return cell
    }
}


import UIKit

open class PackageCollectionViewCell: UICollectionViewCell {
    @IBOutlet open weak var titleLabel: UILabel!
    @IBOutlet open weak var subtitleLabel: UILabel!
    
    open override func awakeFromNib() {
        super.awakeFromNib()
    }
    
    open func configureCell(title: String, subtitle: String) {
        self.titleLabel.text = title
        self.subtitleLabel.text = subtitle
    }
}
Run Code Online (Sandbox Code Playgroud)

Stach Trace:2020-12-21 14:20:37.806065 + 0530 packman [4341:195092] *** 由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'无法在捆绑包中加载NIB:'NSBundle </Users/ pranaychander/library/developer/coreSimulator/devices/b98131CA-B121-42B2-A0F5-FC48066C3179/data/data/contains/bundle/bundle/bundle/bundle/95a42061-d137-4f37-4f35-94ce-58b894ce'tablection n Nagile'spacement and procection。 '' *** 第一个抛出调用堆栈: ( 0 CoreFoundation 0x00007fff20420af6 __exceptionPreprocess + 242 1 libobjc.A.dylib 0x00007fff20177e78 objc_exception_throw + 48 2 CoreFoundation 0x00007fff204209d4 -[NSException initWithCoder:] + 0 3 UI KitCore 0x00007fff24290813 -[UINib instantiateWithOwner:选项:] + 495 4 UIKitCore 0x00007fff23d864db -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:viewCategory:] + 907 5 UIKitCore 0x00007fff23d86bdb -[UICollectionView dequeueReusableCellWithReuseIdentifier:forIndexPath:] + 88 6 包装工0x000000010e766d19 $s7packman14ViewControllerC010collectionB0_13cellForItemAtSo012UICollectionB4CellCSo0iB0C_10Foundation9IndexPathVtF + 313 7 packman 0x000000010e766ef5 $s7packman14ViewControllerC010collectionB0_13cellForItemAtSo0 12UICollectionB4CellCSo0iB0C_10Foundation9IndexPathVtFTo + 165 8 UIKitCore 0x00007fff23d71546 -[ UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:isFocused:notify:] + 410 9 UIKitCore 0x00007fff23d713a6 -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:] + 31 10 UIKitCore 0x00007fff23d769cc - [UICollectionView _updateVisibleCellsNow:] + 6148 11 UIKitCore 0x00007fff23d7bc48 -[UICollectionView布局子视图] + 351 12 UIKitCore 0x00007fff24bf25b8 -[UIView(CALayerDelegate)layoutSublayersOfLayer:] + 2924 13 QuartzCore 0x00007fff27aa2c33 -[CALayer layoutSublayers] + 258 14 QuartzCore 0x00007fff27aa91a5 _ZN2CA5 Layer16layout_if_neededEPNS_11TransactionE + 575 15 QuartzCore 0x00007fff27ab4f47 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 65 16 QuartzCore 0x00007fff279f4408 _ZN2CA7Context18commit_trans actionEPNS_11TransactionEdPd + 496 17 QuartzCore 0x00007fff27a2b1ef _ZN2CA11Transaction6commitEv + 783 18 UIKitCore 0x00007fff246ae47e __34-[UIApplication _firstCommitBlock]_block_invoke_2 + 81 19 CoreFoundation 0x00007fff2038f120 CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK+ 12 20 CoreFoundation 0x00007fff2038e534 __CFRunLoopDoBlocks + 434 21 CoreFoundation 0x00007fff20388f44 __CFRunLoopRun + 899 22 CoreFoundation 0x00007fff203886d6 CFRunLoopRunSpecific + 567 23 GraphicsServices 0x 00007fff2bededb3 GSEventRunModal + 139 24 UIKitCore 0x00007fff24690e0b -[UIApplication _run] + 912 25 UIKitCore 0x00007fff24695cbc UIApplicationMain + 101 26 libswiftUIKit.dylib 0x00007fff54d1e5f2 $s5UIKit 17UIApplicationMainys5Int32VAD_SpySpys4Int8VGGSgSSSgAJtF + 98 27 包曼 0x000000010e7677da $sSo21UIApplicationDelegateP5UIKitE4mainyyFZ + 122 28 包曼 0x000000010e76774e $s7packman11AppDelegateC5$mainyyFZ + 46 29 包曼 0x000000010e767829 主要+ 41 30 libdyld.dylib 0x00007fff202593e9 start + 1 ) libc++abi.dylib:以 NSException 类型的未捕获异常终止 *** 终止应用程序由于未捕获的异常“NSInternalInconsistencyException”,原因:“无法在捆绑包中加载NIB:”NSBundle</Users/pranaychander/Library/Developer/CoreSimulator/Devices/B98131CA-B121-42B2-A0F5-FC48066C3179/data/Containers/Bundle /Application/95A42061-D137-4F35-94CE-58B8915189C6/packman.app>(已加载)',名称为“PackageCollectionViewCell”,以 NSException CoreSimulator 732.18.6 类型的未捕获异常终止 - 设备:iPhone 11 Pro (B98131CA-B121-42B2 -A0F5-FC48066C3179) - 运行时:iOS 14.3 (18C61) - 设备类型:iPhone 11 Pro

在此输入图像描述

fl0*_*034 7

苹果在文章《用 Swift 包捆绑资源》中表示

\n
\n

Bundle.module访问资源时始终使用。包不应该\xe2\x80\x99 对资源的确切位置做出假设。

\n
\n

所以你应该使用这个静态变量作为捆绑参数。

\n


Tre*_*vor 7

  1. 正如其他人已经提到的,捆绑包需要指定为.module. 如果您从 spm 库外部访问它,那么 Bundle 上的一些扩展可能会有所帮助。
extension Bundle {
    public static let blah = Bundle.module
}
Run Code Online (Sandbox Code Playgroud)
  1. 需要更新nib来取消选择Inherits Module From Target并手动选择视图所在的模块。

在此输入图像描述