4

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 ?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Varun Naharia
  • 5,318
  • 10
  • 50
  • 84

2 Answers2

1

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

Schemetrical
  • 5,506
  • 2
  • 26
  • 43
  • read this if you think that http://stackoverflow.com/questions/9086913/objective-c-why-is-autorelease-autoreleasepool-still-needed-with-arc – Varun Naharia May 10 '15 at 14:53
  • @VarunNaharia It has nothing to do with `__autoreleasing`. `@autoreleasepool {}` merely defines a region to release after it runs. The object releases regardless if you put `__autoreleasing` or not. – Schemetrical May 10 '15 at 14:58
  • 1
    Then why _autoreleasing exist anyway ? – Varun Naharia May 10 '15 at 15:24
  • read this http://stackoverflow.com/questions/8862023/in-which-situations-do-we-need-to-write-the-autoreleasing-ownership-qualifier – Varun Naharia May 10 '15 at 15:37
  • @VarunNaharia: `__autoreleasing` is only useful in the inner level of a pointer-to-pointer or array-of-pointers. For example, `NSString * __autoreleasing *` is different from `NSString * __strong *`. You should generally never use `__autoreleasing` at the top level of the type of a variable. – newacct May 11 '15 at 21:32
  • "In fact, if your Objective-C project uses ARC, you wouldn't have had to use autoreleasing either." Well, `__autoreleasing` is only defined in ARC; if you weren't using ARC, `__autoreleasing` would be meaningless anyway. – newacct May 11 '15 at 21:35
  • @newacct this poiter thing explains a lot but _autoreleasing is by default used in ios, in objective-c you can see that in your main method not sure about swift – Varun Naharia May 12 '15 at 02:10
  • @VarunNaharia: `__autoreleasing` is an ownership qualifier on an ARC managed pointer type. It is NEVER used by default. Autorelease pools are something else entirely. – newacct May 12 '15 at 18:46
  • have you any link to understand it completely ? – Varun Naharia May 12 '15 at 22:35
1

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

        })
    }
Varun Naharia
  • 5,318
  • 10
  • 50
  • 84