[英]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.