Kodeco Forums

Intro to Object-Oriented Design in Swift: Part 2/2

Learn the basics of object-oriented design in Swift. In this second part, you'll learn about polymorphism, initialization, and some common design patterns for dealing with objects.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/2122-intro-to-object-oriented-design-in-swift-part-2-2

Change this:
let value = textFields?[0].text.toInt()

to:
let value = Int(textFields[0].text!)

in Swift 2.1

As of Aug 8 2016 now replace this function in file UIAlertController+Convenience
for xcode 7.3.1

    class func alertControllerWithNumberInput(title:String,     message:String, buttonTitle:String, handler:(Int?)->Void) -> UIAlertController {
    let controller = UIAlertController(title: title, message: message, preferredStyle: .Alert)
    
    controller.addTextFieldWithConfigurationHandler { $0.keyboardType = .NumberPad }
    
    controller.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil))
    
    controller.addAction(UIAlertAction(title: buttonTitle, style: .Default) { action in
        let textFields = controller.textFields as? [UITextField]!
        let value = Int(textFields![0].text!)
        handler(value)
        } )
    
    return controller
}

Tutorial: https://www.raywenderlich.com/81953/intro-object-oriented-design-swift-part-2

states:
Open up Vehicle.swift and at the end of the file add the following stand-alone code outside of the Vehicle definition:

// MARK: An extension to make Vehicle printable

extension Vehicle : Printable {
var description:String {
return vehicleTitle + โ€œ\nโ€ + vehicleDetails
}
}

Code worked well prior to this, but after entering this I get an error on the extension line:
"Use of undeclared type โ€˜Printableโ€™

Iโ€™ve been able to convert everything in the tutorial before this to Swift 3, but this one area is tripping me up. Apologies if Iโ€™ve missed something obvious.
Thanks,
John

Finally found a solution: It seems Printable is replaced by CustomStringConvertible

So in the code, change the line:
extension Vehicle : Printable {

to

extension Vehicle: CustomStringConvertible {

1 Like

Thanks for this article. I like how you put it together.
The only issue is that the example for Adapter is rather a Decorator.
An Adapter changes the interface of a type to make it compatible with another interface; whereas a Decorator enhances a type without changing the existing interface.
You even mentioned that protocol extensions are a good fit to implement the Decorator pattern. :slight_smile:
Now, it would be nice to also update the code, since Printable is not available anymore in Swift 3.0. Replace it with CustomStringConvertible as others also suggested.

This tutorial is getting really long in the tooth and definitely could use an update. (Perhaps a rewrite.) :sweat_smile:

Point taken on Adapter vs Decorator. Thanks! Really needs to be updated.

There is an updated version of this tutorial: https://www.raywenderlich.com/160728/object-oriented-programming-swift. Please post your questions or issues regarding it over there. Thank you! :]