- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在制作一个 pod (MySDK),并希望从 CocoaPods
生成的单独资源包中加载 Assets 。
但是,我无法让它工作。
这是我尝试加载 Storyboard的方式:
let storyBoard = UIStoryboard(name: "SDK", bundle: Bundle(identifier:"org.cocoapods.SchedJoulesSDK"))
这给出了错误:
'Could not find a storyboard named 'SDK' in bundle
bundle 添加到 Xcode 中:
我的 podspec
看起来像这样:
s.resource_bundles = {
'MySDK' => ['SDK/*/*.{xib,storyboard,xcassets}']
}
有什么想法吗?
最佳答案
如果您使用 resource
或 resources
在 CocoaPods PodSpec 文件中,您告诉 Cocoapods 这些是您的库将在运行时加载的资源文件。
如果你的库是作为一个动态框架构建的,这些文件只是复制到该框架的资源文件夹路径,一切都会好起来的。然而,如果您的库是作为静态库构建的,这些库将被复制到主应用程序包 (.app
) 的资源文件夹中,这可能是一个问题,因为该主应用程序可能已经有一个资源该名称或另一个 Pod 可能具有该名称的资源,在这种情况下,这些文件将相互覆盖。 Pod 是构建为动态框架还是静态库,不是由 PodSpec 指定的,而是由集成 Pod 的应用程序在 Podfile 中指定的。
因此对于有资源的Pod,强烈推荐使用resource_bundles
相反!
在你的例子中,行
s.resource_bundles = {
'MySDK' => ['SDK/*/*.{xib,storyboard,xcassets}'] }
告诉 CocoaPods 创建一个名为 MySDK (MySDK.bundle
) 的资源包,并将所有匹配该模式的文件放入该资源包中。如果您的 Pod 是作为框架构建的,则此包位于您的框架包的资源文件夹中;如果它是作为静态库构建的,bundle 会被复制到主应用程序 bundle 的资源文件夹中,如果您将 bundle 命名为与 Pod 相同的方式,这应该是安全的(您不应该将其命名为“MySDK”,而不是“SchedJoulesSDK”)。
这个 bundle 将具有与您的 Pod 相同的标识符,但是当构建动态框架时,您的框架 bundle 也将具有该标识符,然后它是未定义的行为当您通过标识符加载它时正在加载哪个 bundle(目前是外包总是在我的测试中获胜)。
正确的代码应该是这样的(虽然没有测试):
// Get the bundle containing the binary with the current class.
// If frameworks are used, this is the frameworks bundle (.framework),
// if static libraries are used, this is the main app bundle (.app).
let myBundle = Bundle(for: Self.self)
// Get the URL to the resource bundle within the bundle
// of the current class.
guard let resourceBundleURL = myBundle.url(
forResource: "MySDK", withExtension: "bundle")
else { fatalError("MySDK.bundle not found!") }
// Create a bundle object for the bundle found at that URL.
guard let resourceBundle = Bundle(url: resourceBundleURL)
else { fatalError("Cannot access MySDK.bundle!") }
// Load your resources from this bundle.
let storyBoard = UIStoryboard(name: "SDK", bundle: resourceBundle)
因为 resourceBundle
不能在运行时改变,所以只创建它一次(例如在应用程序启动时或当你的框架被初始化时)并将它存储到一个全局变量(或全局类属性)中是安全的,因此您可以在需要时始终使用它(bundle 对象也几乎不使用任何 RAM 内存,因为它只封装元数据):
final class SchedJoulesSDK {
static let resourceBundle: Bundle = {
let myBundle = Bundle(for: SchedJoulesSDK.self)
guard let resourceBundleURL = myBundle.url(
forResource: "MySDK", withExtension: "bundle")
else { fatalError("MySDK.bundle not found!") }
guard let resourceBundle = Bundle(url: resourceBundleURL)
else { fatalError("Cannot access MySDK.bundle!") }
return resourceBundle
}()
}
该属性是惰性初始化的(这是 static let
属性的默认值,不需要 lazy
关键字)并且系统确保这种情况只发生一次,作为 let
属性一旦初始化就不能改变。请注意,您不能在该上下文中使用 Self.self
,您需要使用实际的类名。
在您的代码中,您现在可以在任何需要的地方使用该包:
let storyBoard = UIStoryboard(name: "SDK",
bundle: SchedJoulesSDK.resourceBundle)
关于ios - 在 CocoaPods 中使用资源包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50625036/
通过终端,您可以使用命令 - “SetFile -a B 文件名” 以编程方式,我认为我应该通过[[NSFileManager defaultManager] createDirectoryAtPat
嗨,正在尝试书中的一些示例:Practical Graph mining with R对于子图挖掘: library(subgraphMining) library(igraph) graph1 =
代码中的相同问题: class Foo { int getIntProperty () { ... } CustomObject getObjectProperty () { ... }
所以这可能是一个愚蠢的问题,但它已经困扰我一段时间了。 使用 React,我创建了两个组件(Buttons.js 和 Message.js),每个组件都有一个导出。但是,现在我希望将这两个组件用作 n
从今天早上开始,我发现我无法再从某个范围安装任何 NPM 包(或任何具有依赖项的包)。例如,如果我输入 npm i webpack 我会收到以下错误... npm ERR! code E401 npm
我在这里搜索过,Angular 2, @ngtools/webpack, AOT ,但对我不起作用。我运行了 npm install 命令。我正在做的是创建一个新的 Angular 2 项目。当我运行
情况: 我有一个 Swift 包,将其命名为 lib。 lib 位于其自己的存储库中。在lib的仓库中,有一堆本地包;也就是说,这些包是在 lib 中定义的,使用本地路径依赖格式 .package(p
我想在工作中学习和使用nodejs,但是在使用 de npm 命令安装模块/包时遇到网络问题。我是否可以使用我的家用计算机构建完整的 Node js 包,然后将其安装在另一台计算机(我的工作场所计算机
我需要将一些 .tar.bz2 格式的非 Python 包转换为 Anaconda/miniConda .egg 文件并安装它们。为此,我需要一个适用于 Windows 的 bld.bat 文件。互联
我需要共享库文件 libthrift-0.9.3.so 作为其他包的依赖项。我在构建 thrift-0.9.3 包时看到编译问题(我确实从 https://thrift.apache.org/down
我尝试在 R 版本 3.5.0 中安装“arcgisbinding”包。但是我失败了,得到以下错误和警告。 Installing package into ‘C:/Users/Lenovo/Docum
我尝试在 R 版本 3.5.0 中安装“arcgisbinding”包。但是我失败了,得到以下错误和警告。 Installing package into ‘C:/Users/Lenovo/Docum
我试图在 flutter 中测试这个应用程序,但我无法运行该应用程序,因为出现此错误“名称‘Page’在库‘package:burn_off/widgets/page.dart’和‘package’中
试图理解和学习如何编写包...用我一直使用的东西进行测试,记录... 您能帮我理解为什么“日志”变量不起作用...并且屏幕上没有日志记录吗? 谢谢! 主要文件: #!/opt/local/bin/py
我尝试运行此使用 Google 云的代码。 import signal import sys from google.cloud import language, exceptions # creat
我想知道是否有人找到了一个很好的 R 包来分析眼动追踪数据? 我遇到了 eyetrackR,但据我所知,没有可用的英文支持文档: http://read.psych.uni-potsdam.de/pm
我正在 R 上制作一个包。我有两个函数共享一个变量(全局)。 如何将其导入到包中? 例如, m<-0 f<-function() { m <- m+1 } g<-function() { m <- m
我用 C 为 Lua 编写了很多模块。每个模块都包含一个 Lua 用户数据类型,我像这样加载和使用它们: A = require("A") B = require("B") a = A.new(3,{
我正在尝试在 R 中的 Ubuntu 上安装 xlsx 包,以便使用允许在 R 中插入链接然后将它们导出到 Excel 的功能。 话虽如此,我根本无法安装该软件包。 显然它必须与 rJava 一起使用
我想在 Haskell 中做一些蒙特卡洛分析。我希望能够编写这样的代码: do n <- poisson lambda xs <- replicateM n $ normal mu sigma
我是一名优秀的程序员,十分优秀!