UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Day 69 Challenge - Task 2

Forums > 100 Days of Swift

The task 2 from day 69 challenge is:

"You're already receiving the URL of the site the user is on, so use UserDefaults to save the user's JavaScript for each site. You should convert the URL to a URL object in order to use its host property."

I'm not entirely sure what I'm supposed to do here, more particularly - what am I supposed to save? What's "the user's JavaScript for each site" here?

I would be very grateful if anyone broke this down for me.

3      

You can enter JavaScript code to run on each site, so this JS code is what you should save to UserDefaults along with some identifier that it belongs to particular site.

3      

Hi Filip,

Thanks for the explanation! I'll try my best and get back to you later :D

3      

Alrighty, so what I did is I created a custom 'scriptSaved' codable class:

import Foundation

class scriptSaved: NSObject, Codable {
    var script: String?
    var website: String?

    init (script: String, website: String) {
        self.script = script
        self.website = website
    }
}

Each time a script is being injected, it's being saved as a string, along with the website host that is opened at that time.

When I load and print the saved scripts, I get something like that:

**Optional("alert(\"Hello boi!\");") Optional("www.google.com")
Optional("alert(\This is an apple\");") Optional("www.apple.com")
Optional("alert(\"Why have you even entered this website\");") Optional("www.doodie.com")**

Sooo... under the hood I think it's fine, at least there is some file where I can find the basic info of which website got which scripts injected. I'm not sure though if it's all that I was supposed to do here - I was thinking whether I should implement some method so as to load the scripts attached to the website. If that's the thing, I'm not quite sure how to do that.

As always, any help will be greatly appreciated!

3      

Why the NSObject inheritance? Also I would recommend using struct for the model. And the properties should be non-optional and let to better follow convention and have code that is easier to reason about aka you know it cannot change and since it is a struct it cannot be mutated from elsewhere.

4      

Well, to be absolutely honest:

  1. Why NSObject inheritance - cause this is the only one I am aware of that does its job in this particular case :D Took it all from Paul's tutorials, nowhere else.
  2. I am still wrapping my head around the differences between classes and structs, but as classes are more common in 100 Days of Swift projects, I feel safer sticking to those, for now.
  3. Thanks for the advice with the optionals and non-optionals, I'm still in my early days of catching good practice, so I appreciate it even more :)

Anyway, I think that the challenge can be considered done, the code is still quite rough around the edges and for some reason I am able to load only one saved script for each website, but overall I am more content than disappointed. The bar button is working, the custom class containing a website and a script is saved to an array every time I inject a new script, and the table view is working with the saved data as well. If anyone has any questions concerning this particular challenge, I will be more than happy to help - as long as I know the answer :D

3      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.