Code Monkey home page Code Monkey logo

codableguided's Introduction

Codable Guided Project

  • One problem with grouping multiple types in an array and trying to work with them is you have to treat each type differently in certain cases. One of those cases is encoding/decoding (since we can't encode/decode Protocols)

  • In the case of a model controller, it's often times better to create two model controllers and use them each to manage their own type

  • Let's make a new group called Model Controllers

  • Let's make a new file in our Model Controllers group

    • call it VehicleController
    • give it a class property vehicles: [Vehicle]
    • Let's get ready for persistence by creating our persistent URL
      • let persistentFilename = "Car.plist"
      • var persistentURL: URL?
    • lets stub out our CRUD methods (just stub them!)
      • ๐Ÿ›‘ Use Marks
      • func addCarToList(car: Car)
      • func loadList
      • func updateList
      • func deleteCarFromList(car: Car)
      • private func checkPersistence()
        • Implement
      • Implement loadList
      • ๐Ÿ›‘ Decodable (talk about how every type in the chain needs to conform to Decodable)
      • ๐Ÿ›‘ Go to Car struct, make it conform to Decodable
        • ๐Ÿ›‘ hit the error sign and see what properties aren't Decodable
      • Conform all types to Decodable in CarParts.swift
        • make engine and body conform to String
        • Talk about how there are other ways of conforming custom types that we'll learn about later
        • But we also need to clean up our engine and body if we're ever going to display that information to the user so we might as well just make the rawValue String. Giving it this rawValue associates the enum with the String type
        • ๐Ÿ›‘ Build, build succeeds
      • Implement saveToPersistentStore()
        • ๐Ÿ›‘ Encodable error (same as Decodable, but luckily there's a built-in protocol that encompasses both)
        • Conform all CarParts to Codable
          • ๐Ÿ›‘ Build, ask if anyone knows what happened (Car needs to be Codable)
          • Conform Car to Codable
          • ๐Ÿ›‘ Build, build succeeded
      • Implement addCarToList
      • Implement updateCar(car: Car)
        • Try to implement first: where by comparing the cars
        • Oops, we need a unique Identifier for our vehicle (first: where
        • Talk about UUID and implement it
        • varid: UUID = UUID()
          • This will give us a new unique ID anytime a Car is created
      • It's Equatable out of the box, but the default implementation is to equate every property. We want to be able to pass a changed Car into our update array and still be able to easily find it.
      • Implement static func == in Car
      • Finish implementing updateCar(car: Car)
      • Implement deleteCarFromList(car: Car)
        • ๐Ÿ›‘ talk about firstIndex(of: ... ) and how we get that now where before we had to implement a different function
      • Delete Vehicle array
        • refactor to use carController
          • Instantiate to empty CarController()
          • change number of rows
          • change cellForRowAt
          • change cell property to car
  • Check the time if it's before 10:15 - let's spend another 15-20 minutes setting up our model controller for the Beaters (same as above)

  • If it's not, let's talk about JSON

    • JavaScript Object Notation
    • ๐Ÿ›‘ JSON is just an encoded String - it's encoded in utf-8 โ† that can be important to remember!
    • Actually maps out to JavaScript objects (and Swift with a little work!)
    • Open https://www.mockaroo.com
    • change type to JSON
    • show example of JSON output
    • Open JSONPlayground
  • Check the time if it's before 10:45 - want to practice basic persistence or encoding/decoding JSON? We'll work with JSON more tomorrow and make our first network calls, but we're going to drop persistence to make things simpler to work with

codableguided's People

Contributors

froggomad avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.