-1

I just downloaded Xcode 7 and ran my app through the 1.0 to 2.0 migrator and received five errors, two with the calculator, two with the iPhone LED toggle and one in the mail composer in my app.

For the caluclator, I received errors for the .toInt, saying "toInt is unavailable, use the Int() initializer. I marked the lines with the dashes.

@IBAction func launchEmail(sender: AnyObject) {

@IBOutlet weak var Screen: UILabel!
var firstNumber = Int()
var secondNumber = Int()
var isTypingNumber = false
var result = Int()
var operation = ""


@IBAction func number(sender: AnyObject) {
    let number = sender.currentTitle
    if isTypingNumber == true {
        Screen.text = Screen.text! + number!!
    } else {
        Screen.text = number;
    }
    isTypingNumber = true
}


@IBAction func operation(sender: AnyObject) {
    isTypingNumber = false
    ---- firstNumber = Screen.text!.toInt()!----------
    operation = sender.currentTitle!!
}


@IBAction func equals(sender: AnyObject) {
    ---- secondNumber = Screen.text!.toInt()! ----------
    if operation == "+" {
        result = firstNumber + secondNumber
    } else if operation == "-" {
        result = firstNumber - secondNumber
    } else if operation == "X" {
        result = firstNumber * secondNumber
    } else {
        result = firstNumber / secondNumber
    }
    Screen.text = "\(result)"
}


@IBAction func clear(sender: AnyObject) {
    firstNumber = 0
    secondNumber = 0
    isTypingNumber = false
    result = 0
    Screen.text = "\(result)"
}

For the phones LED, I had two errors. One was (on the first line with dashes) saying, "Cannot invoke 'LockForConfiguration' with an argument type of (nil). On the second (line dashed) it said "Extra argument 'error' in call"

@IBAction func toggleFlash(sender: AnyObject) {

    if let device = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo) where device.hasTorch {

            ---- device.lockForConfiguration(nil) -------
            if (device.torchMode == AVCaptureTorchMode.On) {
            device.torchMode = AVCaptureTorchMode.Off

        } else {
            ---- device.setTorchModeOnWithLevel(1.0, error: nil) -------
        }
        device.unlockForConfiguration()
}
}

Finally for the mail composer, On the dashed line, I got an error the said "'MFMailComposerResult' does not have a member named 'value'." @IBAction func launchEmail(sender: AnyObject) {

    if (MFMailComposeViewController.canSendMail()) {

        let emailTitle = "Utilibox Feedback"
        let messageBody = "I am experiencing the following issues:"
        let toRecipents = ["utiliboxfeedback@gmail.com"]

        let mc:MFMailComposeViewController = MFMailComposeViewController()

        mc.mailComposeDelegate = self
        mc.setSubject(emailTitle)
        mc.setMessageBody(messageBody, isHTML: false)
        mc.setToRecipients(toRecipents)

        self.presentViewController(mc, animated: true, completion: nil)

    } else {
        print("No email account found.")
    }
}

func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {

    switch result.value {

    case MFMailComposeResultCancelled.value:
            print("Cancelled")
    case MFMailComposeResultSaved.value:
            print("Saved")
    case MFMailComposeResultSent.value:
            print("Sent")
    case MFMailComposeResultFailed.value:
            print("Failed")
    default:
        break

    }
    self.dismissViewControllerAnimated(true, completion: nil)
}

What exact things should I change these to to make them work. I appreciate all help. THANK YOU SO SO MUCH :) :)

John
  • 1
  • 1
  • 3
  • 1
    The changed integer conversion and the changed error handling (using exceptions) are documented in the Xcode 7 beta release notes. Your first question is a duplicate of http://stackoverflow.com/questions/30739460/toint-removed-in-swift-2. (And isn't the compiler message "toInt is unavailable, use the Int() initializer" sufficient to google for the solution?) – Generally, your question is too broad as it contains 3 different problems. – Martin R Jul 01 '15 at 20:09
  • You forgot the dashed lines in the last example. – Qbyte Jul 01 '15 at 23:52
  • To show appreciation for the help, you should accept Qbyte's answer. – Jeff Sep 28 '15 at 23:56

1 Answers1

0

For the first error you have to use the initializer (failable) of Int instead of .toInt() like so:

firstNumber = Int(Screen.text!)!

The second error comes from the new error handling model in Swift 2 where many functions which handled errors with inout parameters now thows:

if you are sure that no error is thrown (if yes a runtime error is raised) use:

try! device.lockForConfiguration()
try! device.setTorchModeOnWithLevel(1.0)

if an error can be thrown but you want to ignore it and continue with the execution (like now):

do {
    try device.lockForConfiguration()
} catch {}

if (device.torchMode == AVCaptureTorchMode.On) {
    device.torchMode = AVCaptureTorchMode.Off
} else {
    do {
        try device.setTorchModeOnWithLevel(1.0)
    } catch {}
}
device.unlockForConfiguration()

if you want to return directly after any error is thrown:

do {
    try device.lockForConfiguration()

    if (device.torchMode == AVCaptureTorchMode.On) {
        device.torchMode = AVCaptureTorchMode.Off
    } else {
        try device.setTorchModeOnWithLevel(1.0)
    }
    device.unlockForConfiguration()
} catch {
    // here you can use an automatically created "error" variable
    // notice that "error" can be from both functions
    return
}

Unfortunately I don't know a fix for the third error.

Qbyte
  • 12,753
  • 4
  • 41
  • 57