0

Newer to SwiftUI coding, and can't seem to figure out this whole initializer thing. I keep getting this error when putting a variable into my API call. I'm trying to get the values of a TextField to be what the term the API calls for. This is accessing MusicKit with a CatalogueRequest. It works when you manual input a name into the request, but not any variables. Any help would be appreciated! !(https://i.stack.imgur.com/QMWzq.png)(https://i.stack.imgur.com/kWdyH.png)

import SwiftUI 
import MusicKit

struct SearchView: View {
    @State var songs = [songInfo]()
    @State var searchQuery = ""
var body: some View {
    VStack {
        TextField("Search", text: $searchQuery,
    onCommit: {
fetchMusic()
        })
            .padding()
        NavigationView {
            List(songs) { song in
                HStack {
                    AsyncImage(url: song.imageURL)
                        .frame(width: 75, height: 75,     alignment: .center)
                    VStack(alignment: .leading) {
                        Text(song.name)
                            .font(.title3)
                        Text(song.artist)
                            .font(.footnote)
                    }
                    .padding()
                }
                
            }
        }
        .onAppear {
            fetchMusic()
    }
    }
}


private let request: MusicCatalogSearchRequest = {
    var request = MusicCatalogSearchRequest(term: "Cruel     Summer", types: [Song.self])
    request.limit = 25
    return request
}()

private func fetchMusic() {
    Task {
        //Request permission
        let status = await MusicAuthorization.request()
        switch status {
        case .authorized:
            do {
                let result = try await     request.response()
                self.songs = result.songs.compactMap({
                    return .init(name: $0.title,
                                 artist: $0.artistName,
                                 artworkColor:     $0.artwork?.backgroundColor,
                                 imageURL:     $0.artwork?.url(width: 75, height: 75),
                                 imageURLSmall:     $0.artwork?.url(width: 256, height: 256),
                                 imageURLLarge:     $0.artwork?.url(width: 1028, height: 1028))
                })
                print(String(describing: songs[0]))
            } catch {
                print(String(describing: error))
            }
        default: break
            
        }
    }
}
}

#Preview {
    SearchView()
}

I've tried doing manual initializers, explicitly defining it as a string, and keep getting more and more errors.

Boomarov
  • 29
  • 4
  • `private let request` -> `private lazy var`?, or maybe rather `private var request: MusicCatalogSearchRequest {}` (remove the "=" and the `()`)? – Larme Jun 26 '23 at 16:15
  • Doing so repeatedly crashes my Xcode – Boomarov Jun 26 '23 at 16:19
  • Does ```request``` have to be a computed variable? If not, change it to a function i.e. ```private func getMusicCatalogRequest() -> MusicCatalogSearchRequest { return MusicCatalogSearchRequest(term: searchQuery, types: [Song.self]) }``` – ZachtheBoB Jun 26 '23 at 16:47
  • You need to [edit] your question to include all relevant code in the form of a [mcve] in order to make the question on-topic. – Dávid Pásztor Jun 26 '23 at 17:18

1 Answers1

1

I can't see the request property being used anywhere outside the fetchMusic function so I would make it a local variable inside that function instead.

private func fetchMusic() {
    Task {
        //Request permission
        let status = await MusicAuthorization.request()
        switch status {
        case .authorized:
            var request = MusicCatalogSearchRequest(term: searchQuery, types: [Song.self])
            request.limit = 25
            do {
                //... rest of code



Joakim Danielson
  • 43,251
  • 5
  • 22
  • 52
  • Yes that worked! I scoured the internet and couldn't find anything for this hiccup, it held me from progressing. Thank you so much! – Boomarov Jul 06 '23 at 02:16