我在 swift 2.0 中开发了一个 iOS 应用程序,并使用了一个名为 Reachability 的类来确定用户是否连接到互联网。应用程序运行,但编译器停止并输出此错误:
thread 1: exc_bad_instruction(code=exc_i386_invop,subcode=0x0)
这里是代码,注释处出错
func updateInterfaceWithReachability (reachability: Reachability) {
if reachability == self.hostReachability
{
self.checkStatus(reachability)
let netStatus: NetworkStatus = reachability.currentReachabilityStatus()
let connectionRequired: Bool = reachability.connectionRequired()
var baseLabelText: NSString = ""
if connectionRequired
{
baseLabelText = NSLocalizedString("Cellular data network is available.\nInternet traffic will be routed through it after a connection is established.", comment: "Reachability text if a connection is required")
}
else
{
baseLabelText = NSLocalizedString("Cellular data network is active.\nInternet traffic will be routed through it.", comment: "Reachability text if a connection is not required")
}
}
if reachability == self.internetReachability // error displayed here
{
self.checkStatus(reachability)
}
if reachability == self.wifiReachability
{
self.checkStatus(reachability)
}
}
我也这样声明了 internetReachability
var internetReachability: Reachability!
这也是我在另一个名为 checkStatus() 的函数中初始化 internetReachability 的方式
func checkInternetConnection () {
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("reachabilityChanged"), name: kReachabilityChangedNotification, object: nil)
let remoteHostName: String = "www.apple.com"
self.hostReachability = Reachability.init(hostName: remoteHostName)
self.hostReachability.startNotifier()
self.updateInterfaceWithReachability(self.hostReachability)
self.internetReachability = Reachability.reachabilityForInternetConnection()
self.internetReachability.startNotifier()
self.updateInterfaceWithReachability(self.internetReachability)
self.wifiReachability = Reachability.reachabilityForLocalWiFi()
self.wifiReachability.startNotifier()
self.updateInterfaceWithReachability(self.wifiReachability)
}
不明白错误是什么以及如何修复它。帮助表示赞赏。
最佳答案
代码在给定行崩溃,因为您将变量声明为隐式解包可选,这意味着在这种情况下该值被解包以便能够将其与 可达性 进行比较。但是因为您还没有为 internetReachability 设置任何东西,它仍然是 nil 并且代码崩溃,因为 nil 无法解包。
要解决这个问题,您必须要么必须
nilvar internetReachability: Reachability? 并在整个代码中处理可选值internetReachability 之前调用方法checkInternetConnection。幸运的是,通过简单地重新排列您的代码,这里为您提供了最后一个选项的简单版本:
func checkInternetConnection () {
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("reachabilityChanged"), name: kReachabilityChangedNotification, object: nil)
let remoteHostName: String = "www.apple.com"
self.hostReachability = Reachability.init(hostName: remoteHostName)
self.internetReachability = Reachability.reachabilityForInternetConnection()
self.wifiReachability = Reachability.reachabilityForLocalWiFi()
self.hostReachability.startNotifier()
self.internetReachability.startNotifier()
self.wifiReachability.startNotifier()
self.updateInterfaceWithReachability(self.internetReachability)
self.updateInterfaceWithReachability(self.wifiReachability)
self.updateInterfaceWithReachability(self.hostReachability)
}
这样您就可以确保在 self.updateInterfaceWithReachability 尝试访问它们之前都设置了三种可达性类型。
关于ios - 收到此错误 : thread 1: exc_bad_instruction(code=exc_i386_invop, subcode=0x0),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35070103/
在我的mac上安装几个东西时遇到这个问题,我认为这个问题来自将我的豹子升级到雪豹。我认为这个问题也与macports有关。/usr/local/lib/libz.1.dylib,filewasbuiltfori386whichisnotthearchitecturebeinglinked(x86_64)有什么想法吗?更新更具体地说,这发生在安装nokogirigem时日志看起来像:xslt_stylesheet.c:127:warning:passingargument1of‘Nokogiri_wrap_xml_document’withdifferentwidthduetoproto
我已经安装了DeviseonRails4.2.0,一切似乎都在工作,我使用了以下指南:http://sourcey.com/rails-4-omniauth-using-devise-with-twitter-facebook-and-linkedin/我的设计模块是:devise:database_authenticatable,:registerable,:confirmable,:recoverable,:rememberable,:trackable,:validatable,:omniauthable唯一的问题是,如果我尝试通过转到注册页面创建一个新帐户,然后在输入我的电子邮
行为:Ruby1.9.2p180因“非法指令”而失败,没有其他详细信息。Ruby1.9.1p378运行完全没有问题。失败发生在pin=fronto.index(k)行中,仅在某些迭代中发生。from和into都是对象数组,by是该对象的属性(x或y)。代码:defadd_from_to_byfrom,into,bynto=into.sort_by{|k|k.send(by)}fronto=(from+nto).sort_by{|k|k.send(by)}dict={}nto.each{|k|dict[k]=[]}nto.eachdo|k|pin=fronto.index(k)up=pi
可以不调用Thread#join吗?在这种情况下,我不关心线程是否爆炸-我只希望Unicorn继续处理。classMyMiddlewaredefinitialize(app)@app=appenddefcall(env)t=Thread.new{sleep1}t.join#isitokifIskipthis?@app.callenvendend我会得到“僵尸线程”或类似的东西吗? 最佳答案 不调用join完全没问题-事实上,多线程代码通常根本不需要join。如果您需要阻塞直到新线程完成,您应该只调用join。您不会得到“僵尸”线程。
我在C++世界生活了很多年,而我才刚刚开始使用Ruby。我有一个类,我想做一个线程。在Ruby中,从Thread派生类是错误的吗?我看到的例子使用了下面的概念。Thread.new{}这样做会错吗?classMyThreadend 最佳答案 我认为这确实是一个关于领域建模的问题。如果您想扩展/增强线程的行为方式——例如添加调试或性能输出,那么您正在做的事情没有任何问题,但我认为这不是您想要的。您可能想在您的领域中使用事件对象对某些概念进行建模。在那种情况下,标准Ruby方法更好,因为它允许您在不改变领域模型的情况下实现这一目标。继承
乍一看,我以为新的ruby2.0Thread.handle_interrupt会解决我所有的异步中断问题,但除非我弄错了,否则我无法让它做我想做的事(我的问题在最后和标题中)。从文档中,我可以看到如何避免在某个block中接收中断,将它们推迟到另一个block。这是一个示例程序:duration=ARGV.shift.to_it=Thread.newdoThread.handle_interrupt(RuntimeError=>:never)do5.times{putc'-';sleep1}Thread.handle_interrupt(RuntimeError=>:immedia
在Ruby中,Thread#run和Thread#wakup有什么区别?RDoc指定scheduler不使用Thread#wakeup调用,但这是什么意思?何时使用唤醒与运行的示例?谢谢。编辑:我看到Thread#wakup导致线程变为可运行状态,但如果在执行Thread#run之前它不会执行(无论如何都会唤醒线程),它有什么用?有人可以提供一个示例,其中wakeup做了一些有意义的事情吗?出于好奇=) 最佳答案 这里有一个例子来说明它的含义(来自here的代码示例):线程唤醒thread=Thread.newdoThread.st
我正在研究Ruby(1.9.3-p0)中的并发性,并创建了一个非常简单的I/O密集型代理任务。首先,我尝试了非阻塞方法:require'rack'require'rack/fiber_pool'require'em-http'require'em-synchrony'require'em-synchrony/em-http'proxy=lambda{|*|result=EM::Synchrony.syncEventMachine::HttpRequest.new('http://google.com').get[200,{},[result.response]]}useRack::Fi
在线程外部定义的局部变量似乎从内部可见,因此Thread.new的以下两种用法似乎是相同的:a=:fooThread.new{putsa}#=>:fooThread.new(a){|a|putsa}#=>:foodocument举个例子:arr=[]a,b,c=1,2,3Thread.new(a,b,c){|d,e,f|arr[1,2,3]但由于a、b、c在创建的线程内部是可见的,所以这也应该与:arr=[]a,b,c=1,2,3Thread.new{d,e,f=a,b,c;arr[1,2,3]有区别吗?什么时候需要将局部变量作为参数传递给Thread.new?
调用Thread.join会阻塞当前(主)线程。然而,当主线程退出时,不调用join会导致所有生成的线程被杀死。如何在不阻塞主线程的情况下在Ruby中生成持久性子线程?这是连接的典型用法。foriin1..100doputs"Creatingthread#{i}"t=Thread.new(i)do|j|sleep1puts"Thread#{j}done"endt.joinendputs"#{Thread.list.size}threads"这给出了Creatingthread1Thread1doneCreatingthread2Thread2done...1threads但是我正在寻找