简体   繁体   中英

Reachability in Objective-C to Swift

I am trying to convert my reachability function. After bridging Objective-c .h and .m file. Now I a have a function that I call to check in reachability in Objective-C that I want to convert in swift here is the code in Objective-C

-(void)reachabilityCheck
    {
        @try {
            Reachability *__autoreleasing reach = [Reachability reachabilityForInternetConnection];
            if (reach.currentReachabilityStatus) {
                self.isReachable = YES;
            }
            else
            {
                self.isReachable =  NO;
            }
        }
        @catch (NSException *exception) {
            //// [Global writeToLogFile:[exception description]];
        }
        @finally {

        }

I call this function in Objective-C like this

[self reachabilityCheck];

I also written TryCatch in C and also included in bridge header. My function with try catch now looks like this

func reachabiltyCheck()
    {
        TryCatch.try({
            // try something

            }, catch: { (error) in
                println("\(error.description)")

            }, finally: {
                // close resources

        })
    }

Now at the first line in Reachability *__autoreleasing reach = [Reachability reachabilityForInternetConnection]; I don't know how to convert that in swift and also what *_autorealeasing means here ?

Edit 1 If I make function like this it gives me error

func reachabiltyCheck()
    {
        TryCatch.try({
            // try something
            var reach: Reachability = Reachability.reachabilityForInternetConnection();
            var b = reach.currentReachabilityStatus();

            if(b)  // Error : Type 'NetworkStatus' does not conform to protocol 'BooleanType' 
            {
                self.isReachable = Yes;
            }
            else
            {
                self.isReachable = Yes;
            }

            }, catch: { (error) in
                println("\(error.description)")

            }, finally: {
                // close resources

        })

if I print b value it prints (Enum Value) what is the problem ?

Swift uses Automatic Reference Counting by default, you don't need to explicitly state that it should be autorelease d. In fact, if your Objective-C project uses ARC, you wouldn't have had to use autoreleasing either.

After trying for few days finally I converted the Objective-C function to swift

func reachabiltyCheck()
    {
        TryCatch.try({
            // try something
            var reach: Reachability = Reachability.reachabilityForInternetConnection();
            let status:Int = reach.currentReachabilityStatus().hashValue
            if(status==1)
            {
                self.isReachable = true;
            }
            else
            {
                self.isReachable = false;
            }

            }, catch: { (error) in
                println("\(error.description)")

            }, finally: {
                // close resources

        })
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM