Month: March 2020

  • Swift Protocols Aren’t Scary

    For people new to Swift, or coding in general, there are some keywords that might seem daunting. Optionals. Generics. Sometimes you can explain these in a simple way. Other times, exposure to them might scare learners.

    Protocols might seem like a big concept for some. If you look at how many protocols exist in various frameworks, you might think, “Wow, that’s a lot to learn! When will I use all of these?”

    For instance, the Int type conforms to 11 different protocols. Do you need to know them all?

    What is a protocol?

    A protocol is essentially a blueprint. It defines functionality. This can be requiring the existing of certain properties or functions. Something that conforms to a protocol is required to provide the required elements.

    For instance, I could declare a protocol that requires the conforming object, whether a class, struct, or enum, to have a name property.

    protocol Nameable {
        var name: String { get set }
    }

    If I have a struct representing, say, an instrument, I can have it conform to that protocol, which will require the presence of a name property.

    struct Guitar: Nameable {
        var type: String
        
        var name: String // Required because of protocol
    }

    Protocol conformance can be declared when you create your type, or it can be done in an extension. However, extensions cannot contain stored properties, so you can only add support for required functions in extensions.

    protocol DataSource {
        func numberOfItems(in section: Int) -> Int
        func dataToShow(at indexPath: IndexPath) -> Data
    }
    
    class DataManager {
        // Your class stuff here
    }
    
    extension DataManager: DataSource {
        func numberOfItems(in section: Int) -> Int {
            // Return the number of items in each section
        }
        
        func dataToShow(at indexPath: IndexPath) -> Data {
            // Return some data to show for each row/section
        }
    }

    When would I use one?

    You would use a protocol when you want to require functionality for a type. When would you use this?

    With the exception of very basic iOS applications, you are likely to come across a protocol and not even realize it. Consider the following example:

    class MyTableViewController: UIViewController {
        
        var tableView: UITableView!
        
        override func viewDidLoad() {
            super.viewDidLoad()
            
            tableView.dataSource = self
            tableView.delegate = self
        }
    }

    Here, we have a view controller that we’ve created. It has a table view in it. To have a working table view, we need to have a data source and delegate. In simple examples, you would see what we have here: the class itself conforms as a datasource and delegate.

    To do that, we’d have to have our class conform to 2 different protocols. One tells the requirements for the data source, the other tells the requirements to be a delegate.

    extension MyTableViewController: UITableViewDataSource {
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            // Return number of rows in each section
        }
        
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            // Return the table view cell for each part of the table view
        }
        
        
    }
    
    extension MyTableViewController: UITableViewDelegate {
        // Add support for the delegate methods you want to support
    }
    

    So here, we’ve told our class to conform to two protocols so that it can satisfy the requirements of the table view.

    They’re just blueprints

    When we look at the above example of the UITableView, when it’s looking for something to be a data source, it’s wanting something that conforms to UITableViewDataSource. It’s basically saying, “If you conform to these blueprints, you, too, can be a data source!”

    Don’t look at the presence of ALL protocols available to use as daunting. Remember: it’s just saying there are dozens of blueprint options out there. But they’re only used for specific reasons. UITableView wants its data source to conform to a certain protocol. UIScrollView’s delegate has certain requirements.

    And what about what you might make? Would you ever want to make a protocol? Sure. If you ever want to require your types have certain properties or functions, you would create your own protocol. You’d basically be drafting your own blueprints in Swift.

    That’s all protocols are. Blueprints. They’re not scary at all.

  • iPhone SE 2 Expectations

    For months, rumors have circulated that Apple is doing another iPhone SE. But what is Apple’s goal with it? What can we reasonably expect, and why?

    Is It Size?

    Some think the SE 2 (or whatever it will be called; more on that later) is Apple’s chance to release another smaller phone into the line up. While it does give them the opportunity, that doesn’t seem like the role the SE 2 is to play. The reason for that is because that wasn’t the primary goal of the first SE.

    Let’s look back at the original iPhone SE. It launched in the spring of 2016, roughly six months after the release of the iPhone 6S. When the iPhone 6S launched, the iPhone 5S, a then 2 year old phone, was the most affordable option, coming in around $449. The SE ended up replacing it, coming in at $399.

    The SE retained the beloved 4-inch screen size that many loved. And while size made it an attractive purchase, there was another benefit. They were able to pack in the same features and specs of the iPhone 6S into the smaller design. But wait: didn’t it cost less than the iPhone 5s? How could they do that?

    Reusability

    Being based on the older design, it makes sense that, after having manufactured the 5-series for a couple of years, the cost of building the SE, as well as R&D, had been recouped more than a new flagship. The tooling required to build most of the SE were thus already working as a well oiled machine. Savings were easily found there, which could result in a lower price.

    The rumored iPhone SE 2 will be no different. It’s likely to look like an existing design (iPhone 8, I’d say), but with improved internals. Yes, that means a newer set of internal components had to be designed to fit into that enclosure. But the cost of producing that enclosure no doubt will allow for a low price.

    The one “downside” to people owning such a phone: It won’t look as flash as the iPhone 11 Pro (or whatever phones are debuted later this year). But for the audience Apple is likely targeting with this phone, I don’t think they care so much about flashy. It’s about getting the most bang for your buck.

    I know I would love to see another $399 iPhone with the latest specs, and I know many others would, too. Let’s see what happens after one is announced.