菜鸟教程小白 发表于 2022-12-11 16:56:25

ios - 为什么从未执行过的 Swift 3 代码会引发运行时错误?


                                            <p><p>我一直在使用 Apple 的 WWDC 视频中的教程开发 iMessage 扩展程序,但遇到了一个奇怪的错误。我从一个空白项目开始,它运行得很好。但是,我为我的 <code>MSStickerBrowserViewController</code> 添加了一个文件。代码已构建,但在模拟器中打开扩展使其崩溃。奇怪的是,<em>我从来没有创建浏览器的实例</em>。为什么没有执行的代码会崩溃?</p>

<p>这是错误:dyld:未加载库:@rpath/libswiftSwiftOnoneSupport.dylib
引用自:/Users/alextyshka/Library/Developer/CoreSimulator/Devices/BF34F16D-3CEF-4C7D-8D9A-D3D4B463F293/data/Containers/Bundle/Application/75E2E14B-E76B-4EC7-9528-7CE38864B55D/BlankMessages.app/PlugIns/MessagesExtension.appex/MessagesExtension
原因:找不到图片
这是触发错误的代码:</p>

<pre><code>import UIKit
import Messages

class MyStickerBrowserViewController: MSStickerBrowserViewController {
    var stickers = ()
    func changeBrowserViewBackgroundColor(color: UIColor) {
      stickerBrowserView.backgroundColor = color
    }
    func loadStickers() {
      createSticker(asset: &#34;forest&#34;, localizedDescription: &#34;forest sticker&#34;)
    }
    func createSticker(asset: String, localizedDescription: String) {
      guard let stickerPath = Bundle.main().pathForResource(asset, ofType: &#34;png&#34;) else {
            print(&#34;couldn&#39;t create the sticker path for&#34;, asset)
            return
      }
      let stickerURL = URL(fileURLWithPath: stickerPath) //This is the line that seems to be causing the error.
      let sticker: MSSticker
      do {
            try sticker = MSSticker(contentsOfFileURL: stickerURL, localizedDescription: localizedDescription)
            stickers.append(sticker)
            } catch {
                print(error)
            return
      }
    }
    /*
    override func numberOfStickers(in stickerBrowserView: MSStickerBrowserView) -&gt; Int {

    }

    override func stickerBrowserView(_ stickerBrowserView: MSStickerBrowserView, stickerAt index: Int) -&gt; MSSticker {

    }*/
}
</code></pre>

<p>我注意到,如果我取出生成 URL 的第 16 行,则不会引发错误。 </p>

<p> <a href="https://developer.apple.com/videos/play/wwdc2016/204/" rel="noreferrer noopener nofollow">Here</a>是我关注的 WWDC 视频的链接。我已经仔细检查以确保我完全按照视频进行了</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我重新安装了 Xcode,它工作正常。诡异的。谢谢大家的建议!</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 为什么从未执行过的 Swift 3 代码会引发运行时错误?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/38275785/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/38275785/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 为什么从未执行过的 Swift 3 代码会引发运行时错误?