简体   繁体   English

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

[英]Why is Swift 3 code that's never executed throwing runtime errors?

I've been working on an iMessage extension using a tutorial from Apple's WWDC videos and encountered an strange error. 我一直在使用来自Apple WWDC视频的教程来开发iMessage扩展,但遇到一个奇怪的错误。 I started with a blank project, which built an ran just fine. 我从一个空白项目开始,该项目运行良好。 However, I added a file for my MSStickerBrowserViewController . 但是,我为MSStickerBrowserViewController添加了一个文件。 The code built, but opening the extension in the simulator crashed it. 代码已构建,但是在模拟器中打开扩展名使其崩溃。 The strange thing is, I never made an instance of the browser . 奇怪的是, 我从来没有做过浏览器的实例 Why would code that's not being executed crash? 为什么未执行的代码崩溃?

Here's the error: dyld: Library not loaded: @rpath/libswiftSwiftOnoneSupport.dylib Referenced from: /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 Reason: image not found Here's the code triggering the error: 错误是: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原因:找不到图片这是触发错误的代码:

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 {

    }*/
}

I noticed if I take out line 16, which makes a URL, the error isn't thrown. 我注意到,如果我取出第16行,即第一个URL,则不会引发该错误。

Here is the link to the WWDC video I followed. 是我关注的WWDC视频的链接。 I've double checked to make sure I followed the video exactly 我已仔细检查以确保我完全遵循了视频

I reinstalled Xcode and it worked. 我重新安装了Xcode,它起作用了。 Weird. 奇怪的。 Thanks for everyone's advice! 感谢大家的指教!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 永远不会执行此代码警告 swift equatable enum - This code will never be executed warning swift equatable enum 为什么返回0将永远不会在此代码内执行? - Why return 0 will never be executed inside this code? 代码将永远不会执行错误 - Code will never be executed error 为什么此Swift代码会出错? - Why is this Swift code getting errors? 'return' 之后的代码永远不会被执行 - Code after 'return' will never be executed 提交基于Swift的cocoapod时,在检查设备是否为模拟器时出现此错误:“ return”之后的代码将永远不会执行 - When submitting a swift based cocoapod, I get this error when checking if the device is a simulator: code after 'return' will never be executed 为什么不执行代码? - Why code is not being executed? '永远不会被执行' - 'Will never be executed' 为什么没有在第二个 URLSession.shared.dataTask 之后/内执行代码,即在初始 URLSession.shared.dataTask 的 do 块内? 迅速 - Why isn't code being executed after/within a second URLSession.shared.dataTask, that is within an initial URLSession.shared.dataTask’s do block? Swift Swift 3中永远不会执行在类HKManager(HKTutorial)中完成的函数readRunningWorkOuts - function readRunningWorkOuts with completion in class HKManager (HKTutorial) is never executed in Swift 3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM