Author Topic: Chapter 9 - Property Observers section, problems  (Read 6261 times)

Edward_JS

  • Newbie
  • *
  • Posts: 7
    • View Profile
Chapter 9 - Property Observers section, problems
« on: September 28, 2015, 08:29:50 PM »
I've had some issues with code that no longer works when I downloaded and installed Xcode 7 and Swift language updates.
One of them has to do with the Property Observer section, under both the mmUILabel.swift and ViewController.swift file.

In the ViewController.swift file, in the IBAction function, this is what I get:

@IBAction func convertStringToNumber(sender: AnyObject)
    {
        _ = self.txtNumber.text?.toInt()
        if let integerValue = self.txtNumber.text?.toInt()
        {
            self.lblNumber.text = "\(integerValue)"
        }
        else
        {
            self.lblNumber.text = ""
        }
    }

The error is in the lines having to do with "toInt()", which is flagged by an error in Xcode that says toInt() is not available,
and then to use Int() initializer instead. So I did that, and a new error occurs.

This time, it says "Value of Type 'String" has no member 'Int".

The same issue is in the mmUILabel.swift file, and a further error is flagged near the bottom:

required init(coder aDecoder: NSCoder)
    {
        super.init(coder: aDecoder)
    }

The statement super.init(coder: aDecoder) is flagged by Xcode with the error "a nonfailable initializer cannot chain to a failable initializer 'int(coder)' written with 'init?'.

I did not try to run the program before I installed Xcode 7 and the updated Swift language, because I hadn't gotten there yet. So I have no way to know if this is an issue with the program upgrades or was always there.

angelina

  • Global Moderator
  • *****
  • Posts: 25
    • View Profile
Re: Chapter 9 - Property Observers section, problems
« Reply #1 on: July 12, 2016, 08:56:36 PM »
Hello Edward_JS.  It appears that the issues you experienced are due to Xcode 7 updates.  To answer the first part of your question, the toInt() method is no longer supported as of Xcode 7 so you should use Int() instead.  The latest book source code contains the updated code, which has the method written as:

@IBAction func convertStringToNumber(sender: AnyObject) {

        if let integerValue = Int(self.txtNumber.text!)
        {
            self.lblNumber.text = "\(integerValue)"
        }
        else
        {
            self.lblNumber.text = ""
        }
    }

The second issue you referred to in the mmUILabel.swift file occurred because you are missing a ‘?’ after init in the method declaration.  The correct code is listed below and can also be found in the book’s latest source code.

required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
}



Angelina
Oak Leaf Enterprises, Inc.