Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

ForEach “implicitly-synthesized declarations” Error in Swift Tik Tok clone :(

cj_

New Coder
These errors

"Cannot declare entity named '$reel'; the '$' prefix is reserved for implicitly-synthesized declarations"

"Generic struct 'ForEach' requires that 'Binding<[Reel]>' conform to 'RandomAccessCollection'"

Both errors are on this ForEach

Code:
ForEach($reels) { $reel in

          ReelsPlayer(reel: $reel)
}

Is this because my videos are a custom type, and therefore swift is not able to ID them which is giving me the 'RandomAccessCollection' error?

I have a TabView {} and am trying to make each page a new video similar to tik tok or vine. It works when I pass in dummy data but when I pass from my array of videos I get those errors.

The View:

Code:
import SwiftUI

import AVKit

struct ReelsView: View {

  @State var currentReel = ""

  // extracting AVPlayer from media file

  @State var reels: [Reel] = MediaFileJSON.map { item -> Reel in   

    let url = Bundle.main.path(forResource: item.url, ofType: "MP4") ?? ""   

    let player = AVPlayer(url: URL(fileURLWithPath: url))

    return Reel(player: player, mediaFile: item)

  }

  var body: some View {

    // setting width and height for rotated view

    GeometryReader { proxy in

      let size = proxy.size

      // vertical page tab view

      TabView(selection: $currentReel) {

        ForEach($reels) { $reel in // **ERRORS HERE**

          ReelsPlayer(reel: $reel)

          // setting width

          .frame(width: size.width)

          .padding()

          // rotate content

          .rotationEffect(.init(degrees: -90))

        }

      }

      // Rotate View

      .rotationEffect(.init(degrees: 90))

      // setting height as width

      .frame(width: size.height)

      .tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))

      // setting max width

      .frame(width: size.width)

    }

  }

}

struct ReelsView_Previews: PreviewProvider {

  static var previews: some View {

    ContentView()

  }

}

struct ReelsPlayer: View, Identifiable {

  var id: ObjectIdentifier

  @Binding var reel: Reel

  var body: some View {

    ZStack {

    }

  }

}

MediaFile struct:

Code:
struct MediaFile: Identifiable {

  var id = UUID().uuidString

  var url: String

  var title: String

  var isExpanded: Bool = false

}

var MediaFileJSON = [

  MediaFile(url: "Reel1", title: "Apple AirTag......."),

  MediaFile(url: "Reel2", title: "this is the second."),

  MediaFile(url: "Reel3", title: "the third"),

  MediaFile(url: "Reel4", title: "wooooooooooooo ya"),

  MediaFile(url: "Reel5", title: "ay ayayayayay ayay lsdfk sl"),

  MediaFile(url: "Reel6", title: "this is the last one....."),

]

Reel struct:

Code:
struct Reel: Identifiable {

  var id: String = UUID().uuidString

  var player: AVPlayer?

  var mediaFile: MediaFile

}
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom