首页 > 解决方案 > 安装 cocoapods 并将应用程序连接到 firebase 后,“在范围内找不到 AlbumArt”。Xcode 和 SwiftUI

问题描述

昨天我在我为学校项目做的音乐播放器应用程序上安装了 cocoapods 和 firebase 依赖项。我总是创建新的分支来尝试它,以防它不起作用。我正在使用最新版本的 Xcode 和最新版本的 firebase 和 mac osx Big Sur。

我的 ContentView 中有几个结构,例如 SongCell、Album、Song 和 AlbumArt。

然后我有另一个名为 PlayerView 的 swift 文件,我在其中为播放器设计了播放歌曲时的视图。我有一个 Vstack,其中显示了 Text 和 AlbumArt。在我安装 podfile 并将应用程序连接到 firebase 之前,所有这些都没有任何问题。然后突然之间我无法构建或运行该应用程序,因为 PlayerView 文件中的“范围内找不到专辑艺术”。

概括:

  1. 我在 ContentView 中有一个名为 AlbumArt 的结构

  2. 这个结构也用在 PlayerView 示例中:

AlbumArt(专辑:专辑,isWithText:假)

  1. 在我安装 cocoapods 和 firebase 之前,一切正常

  2. 现在它在 PlayerView 内“找不到范围内的 AlbumArt”

  3. 似乎我想从 ContentView 到 PlayerView 使用任何结构,我得到“在范围内找不到”,所以在使用 playerViews 中的结构时似乎是一个问题。

  4. 我该如何解决这个问题?

提前致谢。我将在下面发布 ContentView、PlayerView 和 podfile 中的代码。

内容视图中的代码:

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())
    }
    
}


}

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 () {
        
    }
    
    
}

我的播客文件:

# 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

标签: iosswiftxcodefirebaseswiftui

解决方案


您的AlbumArt结构定义是结构内部的ContentView,因此从PlayerView. 你也可以对 SongCell 做同样的事情,你不必在ContentView结构中定义它。

如果将该struct定义移动到 swift 文件的根级别,它将编译。

struct ContentView: View {
  // all your ContentView stuff
}

struct AlbumArt : View {
  // this definition here
}

推荐阅读