我正在尝试在基于Swift的新应用程序中使用Tony Million的Reachability。我在Obj C中编写的另一个应用程序中实现了它,但是我遇到了在Swift中获取正确语法的问题。代码块如下:
override func viewDidLoad() {
super.viewDidLoad()
messageText.text = ""
var reach: Reachability = Reachability(hostName: "www.apple.com")
NSNotificationCenter.defaultCenter().addObserver(self, selector: "reachabilityChanged", name: kReachabilityChangedNotification, object: nil)
reach.reachableBlock = Reachability()
{
dispatch_async(dispatch_get_main_queue(), {
self.messageText.text = "Enter search criteria...";
})
}
reach.unreachableBlock = Reachability()
{
dispatch_async(dispatch_get_main_queue(), {
self.messageText.text = "Attempting to contact network...";
})
}
reach.startNotifier()
}
AND
func reachabilityChanged(note: NSNotification)
{
var reach: Reachability = Reachability()
if(reach.isReachable())
{
messageText.text = "Enter search criteria...";
}
else
{
messageText.text = "Attempting to contact network...";
}
}
我的问题是第一,我的“可达”和“无法访问”的“块”不是正确的语法,我对这些块的正确语法是什么感到茫然。我的第二个问题是“reachabilityChanged”功能。我收到一个错误,指出“ - [_ TtC9icdDRPlus20SearchViewController reachabilityChanged]:无法识别的选择器发送到实例”我再次感到茫然。考虑我的Obj C代码如下:
-(void)viewDidLoad
{
[super viewDidLoad];
NSString *popUpShownOnce = @"YES";
NSInteger swipeCount = 0;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:popUpShownOnce forKey:@"popDisplayed"];
[defaults setInteger:swipeCount forKey:@"showswipearrows"];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(reachabilityChanged:)
name:kReachabilityChangedNotification
object:nil];
[[UIApplication sharedApplication] setStatusBarHidden:NO];
Reachability * reach = [Reachability reachabilityWithHostname:@"somesite.com"];
reach.reachableBlock = ^(Reachability * reachability)
{
dispatch_async(dispatch_get_main_queue(), ^{
searchForText.placeholder = @"Enter search criteria...";
});
};
reach.unreachableBlock = ^(Reachability * reachability)
{
dispatch_async(dispatch_get_main_queue(), ^{
searchForText.placeholder = @"Attempting to contact network...";
});
};
[reach startNotifier];
}
-(void)reachabilityChanged:(NSNotification*)note
{
Reachability * reach = [note object];
if([reach isReachable])
{
searchForText.placeholder = @"Enter search criteria...";
}
else
{
searchForText.placeholder = @"Attempting to contact network...";
}
}
非常感谢协助。问题如下: 1.从Obj C到Swift的可到达和无法到达的块的正确翻译是什么? 2.如果我的addObserver调用正确,为什么我会收到无法识别的选择器错误?如果不正确,那么正确的呼叫是什么?
提前致谢。
答案 0 :(得分:2)
闭包的正确语法是{ (<params>) -> <return type> in <statements> }
:
reach.reachableBlock = { (reachability) in
dispatch_async(dispatch_get_main_queue(), {
self.messageText.text = "Enter search criteria...";
})
}
使用NSNotificationCenter
,看起来你错过了选择器参数中的冒号:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "reachabilityChanged:", name: kReachabilityChangedNotification, object: nil)
答案 1 :(得分:2)
在进行@Austin推荐的更改后,您的错误是SCNetworkReachabilitySetCallback中的self.reachabilityRef为NULL。
尝试:
func reachabilityChanged(note: NSNotification!) {
var reach: Reachability! = Reachability(reachabilityRef: note.object as SCNetworkReachability)
if(reach.isReachable()) {
messageText.text = "Enter search criteria...";
} else {
messageText.text = "Attempting to contact network...";
}
}
更新:
似乎SCNetworkReachability目前还没有完全使用Swift:根据这个:https://twitter.com/marksands/status/474717606004273152
答案 2 :(得分:0)
我让它像这样工作:
var reachability: Reachability?
override func viewDidLoad() {
super.viewDidLoad()
// Setup reachability
reachability = Reachability(hostName: "www.google.com")
reachability!.reachableBlock = { (reach) in
dispatch_async(dispatch_get_main_queue(), {
self.titleLabel.attributedText = Utilities.myAttributedText("Online", mySize: 18, myFont: "HelveticaNeue", myColor: UIColor.whiteColor())
self.titleLabel.sizeToFit()
})
}
reachability!.unreachableBlock = { (reach) in
dispatch_async(dispatch_get_main_queue(), {
self.titleLabel.attributedText = Utilities.myAttributedText("Offline", mySize: 18, myFont: "HelveticaNeue", myColor: UIColor.whiteColor())
self.titleLabel.sizeToFit()
})
}
reachability!.startNotifier()
}
答案 3 :(得分:0)
这对我有用:
//Reachability
myReachabilityInstance = Reachability(hostName: "www.google.com")
myReachabilityInstance?.reachableOnWWAN = false
NSNotificationCenter.defaultCenter().addObserver(self, selector: "reachabilityDidChangeMethod", name: kReachabilityChangedNotification, object: nil)
myReachabilityInstance?.startNotifier()