skip to Main Content

Yesterday I installed cocoapods and the firebase dependencies on my musicplayer app I am doing for a school project. I always create new branches to try it out in case it does not work. I am using the latest version of Xcode and the latest versions of firebase with mac osx Big Sur.

I have a couple of structs in my ContentView for example SongCell, Album, Song & AlbumArt.

I then have another swift file called PlayerView where I designed the view for the player when you play songs. There I have a Vstack where the Text and AlbumArt are being displayed. All this worked without any problems until I installed the podfile and connected the app to firebase. Then all of sudden I cannot build or run the app because ‘AlbumArt cannot be found in scope’ inside the PlayerView file.

Summary:

  1. I have a struct called AlbumArt inside the ContentView

  2. This struct is also used inside the PlayerView example:

AlbumArt(album: album, isWithText: false)

  1. Before I installed cocoapods and firebase, everything worked fine

  2. Now it ‘cannot find AlbumArt in scope’ inside the PlayerView

  3. It seems like whatever struct I want to use from ContentView into the PlayerView, I get the ‘cannot find in scope’ so it seems to be a problem when using the structs inside the playerViews.

  4. How do I solve this?

Thanks in advance. I will post the code from ContentView, PlayerView and the podfile below.

Code in ContentView:

import SwiftUI
import Firebase



struct Album : Hashable {
    var id = UUID()
    var name : String
    var image : String
    var songs : [Song]
}

struct Song : Hashable {
    var id = UUID()
    var name : String
    var time : String
    
}

struct ContentView: View {

    @ObservedObject var data : MyData
    @State private var currentAlbum : Album?
    
    
    var body: some View {
        NavigationView{
            ScrollView{
                ScrollView(.horizontal, showsIndicators: false, content: {
                    LazyHStack{
                        ForEach(self.data.albums, id:.self, content: {
                            album in
                                AlbumArt(album: album, isWithText: true).onTapGesture {
                                    self.currentAlbum = album
                                }
                    })
                    }
                            
                })
                LazyVStack{
                    if self.data.albums.first == nil {
                        EmptyView()
                        
                    }else {
                        ForEach((self.currentAlbum?.songs ?? self.data.albums.first?.songs) ??
                                            [Song(name:"Song 1", time: "3:11"),
                                              Song(name:"Song 2", time: "3:11"),
                                              Song(name:"Song 3", time: "3:11"),
                                              Song(name:"Song 4", time: "3:11"),
                                              Song(name:"Song 5", time: "3:11"),
                                              Song(name:"Song 6", time: "3:11")],
                        id: .self,
                        content: {
                        song in
                            SongCell(album: currentAlbum ?? self.data.albums.first!, song: song)
                })
                    
                }
                    
                
            }.navigationTitle("Sinemark")
        }
    }
}

struct AlbumArt : View{
    var album : Album
    var isWithText : Bool
    var body: some View {
        ZStack (alignment: .bottom, content: {
        
            Image(album.image)
                .resizable()
                .aspectRatio(contentMode: .fill)
                .frame(width: 170, height: 200, alignment: .center)
            
            if isWithText == true {
            ZStack {
                Blur(style: .dark)
                Text(album.name).foregroundColor(.white)
            }.frame(height: 60, alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/)
                
        }
    }).frame(width: 170, height: 200,alignment: .center) .clipped().cornerRadius(20).shadow(radius: 10).padding(20)
            
                
    }
}

struct SongCell : View{
    var album : Album
    var song : Song
    var body: some View{
        NavigationLink(
            destination: PlayerView(album: album, song: song), label: {
                    HStack{
                            ZStack{
                                Circle().frame(width: 50, height: 50, alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/).foregroundColor(/*@START_MENU_TOKEN@*/.blue/*@END_MENU_TOKEN@*/)
                                Circle().frame(width: 20, height: 20, alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/).foregroundColor(.white)
                            }
                            Text(song.name).bold()
                            Spacer()
                            Text(song.time)
                        }.padding(20)
            
            }).buttonStyle(PlainButtonStyle())
    }
    
}


}

Code in PlayerView:

import Foundation
import SwiftUI




struct PlayerView : View {
    var album : Album
    var song : Song
    

    
    @State var isPlaying : Bool = false
    
    var body: some View {
        ZStack {
            Image(album.image).resizable().edgesIgnoringSafeArea(.all)
            Blur(style: .dark).edgesIgnoringSafeArea(.all)
            VStack{
        
                Spacer()
                AlbumArt(album: album, isWithText: false)
                Text(song.name).font(.title).fontWeight(.light).foregroundColor(.white)
                Spacer()
                ZStack {
                    
                    Color.white.cornerRadius(20).shadow(radius: 10)
                    
                    HStack {
                        
                        Button(action: self.previous, label: {
                            Image(systemName: "arrow.left.circle").resizable()
                        }).frame(width: 50, height: 50, alignment: .center).foregroundColor(Color.black.opacity(0.2))
                        
                        Button(action: self.playPause, label: {
                            Image(systemName: isPlaying ? "pause.circle.fill" : "play.circle.fill").resizable()
                        }).frame(width: 70, height: 70, alignment: .center).padding()
                        
                        Button(action: self.next, label: {
                            Image(systemName: "arrow.right.circle").resizable()
                        }).frame(width: 50, height: 50, alignment: .center).foregroundColor(Color.black.opacity(0.2))
                            
                        }
                    
                }.padding().edgesIgnoringSafeArea(.bottom).frame(height: 200, alignment: .center)
                }
            }
        }
        
    func playPause() {
        self.isPlaying.toggle()
        
    }
    
    func next () {
        
    }
    
    func previous () {
        
    }
    
    
}

My podfile:

# Uncomment the next line to define a global platform for your project
  platform :ios, '14.4'

target 'BandApp_MusicPlayer' do
  # Comment the next line if you don't want to use dynamic frameworks
  use_frameworks!

  # Pods for BandApp_MusicPlayer
    pod 'Firebase/Firestore'
    pod 'Firebase/Storage'
    pod 'Firebase/Analytics'
    

  target 'BandApp_MusicPlayerTests' do
    inherit! :search_paths
    # Pods for testing
  end

  target 'BandApp_MusicPlayerUITests' do
    # Pods for testing
  end

end

2

Answers


  1. I think I see you problem – you have placeholder code in your AlbumArt class:

    /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/

    It appears in this part of your code:

    if isWithText == true {
        ZStack {
            Blur(style: .dark)
            Text(album.name).foregroundColor(.white)
        }.frame(height: 60, alignment: .center) <--- RIGHT HERE
    }
    

    Let me know if that fixes it!

    Login or Signup to reply.
  2. Your AlbumArt struct definition is internal to the ContentView struct, so it is out of scope from the perspective of PlayerView. You might as well do the same for SongCell as well, you don’t have to define it inside the ContentView struct.

    If you move that struct definition to the root level of your swift file, it will compile.

    struct ContentView: View {
      // all your ContentView stuff
    }
    
    struct AlbumArt : View {
      // this definition here
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search