2

I'm try to use the validate medicare code in How do I validate an Australian Medicare number? thread,

I replace following line

let expression =try!NSRegularExpression(pattern:pattern, options: NSRegularExpressionOptions.CaseInsensitive)

with code below,

var expression :NSRegularExpression

do {
    expression = NSRegularExpression(pattern: pattern, options: NSRegularExpressionOptions.CaseInsensitive) 
} catch {
    // Handling error
    println("error medicare val")
}

But I'm getting errors as

Expected while in do-while block

and

brace block of statements is an unused clausure

I'm not sure how to solve this error. I'm using Xcode 6.2.

Update

I tried Parth's answer and I got following errors,

consecutive statements on a line must be separated by ';' for this line

expression = try NSRegularExpression(pattern: pattern, options: NSRegularExpressionOptions.CaseInsensitive)

Also

} catch let error as NSError {
            // Handling error
            print("error medicare val\(error)")
        }

code got

Expected while in do-while block

brace block of statements is an unused clausure

Expected expression

consecutive statements on a line must be separated by ';'

Community
  • 1
  • 1
Zusee Weekin
  • 1,348
  • 2
  • 18
  • 41
  • Xcode 6.2 comes with Swift 1.1 and the answer you have found in the linked article is written in Swift 2.1 (or 2.0, I'm not sure). There may be many other parts needing fix even if you have fixed that do-try-catch part, and recently it is very hard to find articles written for Swift 1. Can't you upgrade your mac and update your Xcode to the latest released version? – OOPer Dec 18 '16 at 05:21
  • @OOPer Thanks for your help. You are correct. I fixed changed few lines in that code but still have errors. Yes It is very difficult to find articles for this version but unfortunately I cannot upgrade it this time. – Zusee Weekin Dec 18 '16 at 05:27

2 Answers2

0

Change the code like below to get the error object

var expression: NSRegularExpression!

do {
     expression = try NSRegularExpression(pattern: pattern, options: NSRegularExpressionOptions.CaseInsensitive)
   } catch let error as NSError {
  // Handling error
  print("error medicare val\(error)")
}
Parth Adroja
  • 13,198
  • 5
  • 37
  • 71
0

As I wrote in my comment, it's very hard to downgrade codes to the ancient Swift 1.x, once used in a mythic era, long long time ago...

So, this single line may be the last thing I can help you to downgrade...

//Swift 2.x code
let expression = try! NSRegularExpression(pattern:pattern, options: NSRegularExpressionOptions.CaseInsensitive)

The equivalent to the line in Swift 1.x would be:

//Swift 1.x code
let expression = NSRegularExpression(pattern: pattern, options: nil, error: nil)!

A few points:

  • Error handling with do-try-catch was first introduced in Swift 2. You, in Swift 1, cannot use any of the three keywords do, try and catch for error handling. You need to use error: parameter in Swift 1.x.
  • If pattern is a valid regex pattern, the initializer never throws error. You have no need to catch it, meaning error: can be nil.
  • The pattern does not contain letters, so you have no need to specify NSRegularExpressionOptions.CaseInsensitive, in Swift 1.x, "no options" is specified by nil.

Xcode 6 is the oldest Xcode which can build apps for App Store as for now, and the requirement would be changed to Xcode 7 at any time in the near future. If you cannot finish your app until then, you cannot submit the app to the App Store. I strongly recommend to update your Xcode with any effort you can, now.

OOPer
  • 47,149
  • 6
  • 107
  • 142