Result of 'CLASS()' initializer is unused

So I have had to move a script that was running in a SwiftUI View into it's own class function so that it could run in the background when the user closes the app and also did not stop when the user went to another view.

To make this script run correctly I put it inside

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

monkey()
}

Monkey class has a simple Timer in it that repeats every few seconds.

This works well however I am getting Result of 'monkey' initializer is unused

So is there away to make the monkey not show this warning?

I'm assuming your monkey class (it should be Monkey, btw) has an initializer that has a side effect of starting the timer.

Initializers with side effects are kind of iffy. If the only purpose of this object is to start a timer, than why bother having an initializer on a class? It could have just been a free function.

I did it this way because of the following

class Monkey {
    static var playingType = "";
    static var artist = "";
    static var song = "";
    static var cover = "";
    static var advertlink = "";
    static var type = "";
    

    let timer =  Timer.scheduledTimer(withTimeInterval: 5, repeats: true) { timer in
      
        if(playingType == "radio"){
            Api().getSong(station: Api.station){ (NP) in
                if(cover != NP.imageurl)
                {
                    cover = NP.imageurl!
                    advertlink = NP.url ?? ""
                    type = NP.type ?? ""
                }
                if(song != NP.title && artist != NP.artist)
                {
                    artist = NP.artist
                    song = NP.title
                    type = NP.type ?? ""
                  
                    MusicPlayer.shared.getArtBoard(artist: artist, song: song, cover: cover, urls: NP.url ?? "", duration: 0)
                    Flurry.logEvent("Track_Change", withParameters: ["artist": artist, "song":song])
                    
                }
         
            }
    }
}

This will do (normally types are pascal cased):

_ = Monkey()

Interesting that worked, is there any documentation that explains why this works.
In theory I get it as you need assign a class to a var or let or something like that I guess it's like NodeJS say I want to use a class inside another class I can do it and use it multiple time by assigning it to a var.

it's just a shortcut for let _ = ... which is the same as let x = ... where you are not interested in the variable "x" (if you used let x = ... instead compiler would then complain that "x is not used").

Normally you'd allocate an instance variable with non static properties, besides other things it will allow you to have different Monkey objects with different properties.

This design doesn't really make sense.

You're using a class as though it's a single object. All of these state properties are static, except for Timer.

Instead, you can just make them instance properties, and make the objects make sense. Here's a rough idea to get started:

class Monkey {
    var playingType = "";
    var artist = "";
    var song = "";
    var cover = "";
    var advertlink = "";
    var type = "";
    var timer: Timer? = nil

    func start() {
        let timer = Timer.scheduledTimer(withTimeInterval: 5, repeats: true) { timer in
            if playingType == "radio" {
                Api().getSong(station: Api.station) { (NP) in
                    if cover != NP.imageurl {
                        cover = NP.imageurl!
                        advertlink = NP.url ?? ""
                        type = NP.type ?? ""
                    }

                    if song != NP.title && artist != NP.artist {
                        artist = NP.artist
                        song = NP.title
                        type = NP.type ?? ""
                    
                        MusicPlayer.shared.getArtBoard(artist: artist, song: song, cover: cover, urls: NP.url ?? "", duration: 0)
                        Flurry.logEvent("Track_Change", withParameters: ["artist": artist, "song":song])
                    }
                }
            }
        }
    }
}

You would later create a new object (which actually makes sense to have now), and you can start() it.

let monkey = Monkey()
monkey.start()

Though I fail to see what any of this has to do with monkeys lol

1 Like