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

标签 ios swift swift3 imessage

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

这是错误:dyld: Library not loaded: @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 原因:找不到图片 这是触发错误的代码:

import UIKit
import Messages

class MyStickerBrowserViewController: MSStickerBrowserViewController {
    var stickers = [MSSticker]()
    func changeBrowserViewBackgroundColor(color: UIColor) {
        stickerBrowserView.backgroundColor = color
    }
    func loadStickers() {
        createSticker(asset: "forest", localizedDescription: "forest sticker")
    }
    func createSticker(asset: String, localizedDescription: String) {
        guard let stickerPath = Bundle.main().pathForResource(asset, ofType: "png") else {
            print("couldn't create the sticker path for", 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) -> Int {

    }

    override func stickerBrowserView(_ stickerBrowserView: MSStickerBrowserView, stickerAt index: Int) -> MSSticker {

    }*/
}

我注意到,如果我删除第 16 行,它构成了一个 URL,则不会抛出错误。

Here是我关注的 WWDC 视频的链接。我已经仔细检查以确保我完全按照视频进行了操作

最佳答案

我重新安装了 Xcode,它工作了。诡异的。谢谢大家的建议!

关于ios - 为什么从未执行过的 Swift 3 代码会抛出运行时错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38275785/

相关文章:

ios - 如何下载 pdf 文件,在 iPhone 的 UITableViewCell 中显示进度条和当前状态

ios - 如何保护从用户设备上的服务器下载的数据?

ios - 如何在 Swift3 中使用 View Controller 作为侧边菜单

ios - 如何在半屏上呈现 ViewController

ios - 在 iOS 上使用自动布局如何指定 View 应占用尽可能多的空间?

swift - 使用 MGSwipeTableCell 时自定义字体?

ios - 如何在代码中设置 NSLayoutConstraint 常量的变体?

swift - 如何根据图像调整 UIImaveView 的大小?

swift - 通用大中央调度

ios - Swift - 使用 UIBezierPath 裁剪图像