Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 另一个函数中的swift wrap函数_Ios_Swift_Decorator_Word Wrap - Fatal编程技术网

Ios 另一个函数中的swift wrap函数

Ios 另一个函数中的swift wrap函数,ios,swift,decorator,word-wrap,Ios,Swift,Decorator,Word Wrap,我有一门课要检查我在这里找到的互联网连接: 在我的方法中,我使用它: override func viewWillAppear(animated: Bool) { super.viewWillAppear(animated) if Reachability.isConnectedToNetwork() == false { let alert = UIAlertView(title: "No Internet Connection

我有一门课要检查我在这里找到的互联网连接:

在我的方法中,我使用它:

     override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        if Reachability.isConnectedToNetwork() == false {
            let alert = UIAlertView(title: "No Internet Connection", message: "Make sure your device is connected to the internet.", delegate: nil, cancelButtonTitle: "OK")
            alert.show()
            return
        }
    }
但是我能做一个装饰师或者写点什么吗

@check_internet_connection
override func viewWillAppear(animated: Bool) {

}
例如,将其用于类中的所有方法:

@check_internet_connection
class MyClass: UIViewController {
    ...
}

继承方法

您可以创建
ReachabilityAwareViewController
作为基类

class ReachabilityAwareViewController: UIViewController{

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        if Reachability.isConnectedToNetwork() == false {
            // your code goes here
        }
    }
}
并创建子类,该子类继承来自
可达性yawareviewcontroller
的所有行为

class myViewController: ReachabilityAwareViewController{
    // ..
}

组合方法

这种方法看起来更像装饰器。使用协议扩展

protocol ReachabilityAware{
    func checkReachibility()
}

extension ReachabilityAware where Self: UIViewController{

    func checkReachibility(){
        if Reachability.isConnectedToNetwork() == false {
            let alertController = UIAlertController(title: "No Internet Connection", message: "Make sure your device is connected to the internet.", preferredStyle: .Alert)
            let okAction = UIAlertAction(title: "OK", style: .Cancel){ action in }
            alertController.addAction(okAction)
            presentViewController(alertController, animated: true, completion: nil)
        }
    }
}

class myViewController: UIViewController, ReachabilityAware{

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        checkReachibility()
    }
}
在Swift中,这些被称为。目前(从Swift 2.1开始),您无法定义自己的

为什么不编写一个全局函数来处理这个问题呢

// In global scope
func check_internet_connection() -> Bool {
    if Reachability.isConnectedToNetwork() == false {
        let alert = UIAlertView(title: "No Internet Connection", message: "Make sure your device is connected to the internet.", delegate: nil, cancelButtonTitle: "OK")
        alert.show()
        return false
    } else {
        return true
    }
}

…

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    if !check_internet_connection() { return }
}