0

I am trying to add a chart to my iWatch application, but there are all sorts of errors. I am definitely missing some knowledge, but you can see my code below and I am following the instructions here.

I am not sure where to add the code for the chart. Is it supposed to go in the View file?

The error I see when using the code below is:

Cannot convert value of type '(TeamsView) -> () -> some View' to expected argument type 'CGRect'

import SwiftUI
import YOChartImageKit

struct TeamsView: View {
    
   private var image: YOLineChartImage = {
     let image = YOLineChartImage()
     image.strokeWidth = 4.0
     image.strokeColor = UIColor(displayP3Red: 1.0, green: 0.0, blue: 0.0, alpha: 1.0)
     image.values = [0.0, 1.0, 2.0]
     image.smooth = false
     image.drawImage(frame, scale: 1)
     return image
   }()
   
   var body: some View {
        VStack{
            Text("Teams")
            List([Team(id: "1", name: "Liverpool"),
                  Team(id: "2", name: "Man Utd.")]) {
                    team in TeamRow(team: team)
                }
             image
        }
    }
}
peter flanagan
  • 9,195
  • 26
  • 73
  • 127
  • Does `YOLineChartImage` conform to `some View`, `Image`, or some other SwiftUI `View`? I just quickly looked at at a Git Repository and it seems to conform to `UIKit` classes. If so you need to create a `UIViewRepresentable` or a `UIViewControllerRepresentable` – lorem ipsum Dec 18 '20 at 22:56
  • hey @loremipsum thanks for this info, I believe this is the issue. Do you know how to do this? – peter flanagan Dec 19 '20 at 20:49
  • I don't know enough about the repository to give you exact code but if it is a `UIView` it goes into a `UIViewRepresentable` if not it goes into a `UIViewController` then wrapped into a `UIViewControllerRepresentable` there are many tutorials. The easiest way to practice the code would be to look at Apple SwiftUI tutorial "Interfacing with UIKit". Next, I would suggest you look for tutorials that wrap an `MKMapView` https://stackoverflow.com/questions/63110673/tapping-an-mkmapview-in-swiftui https://www.hackingwithswift.com/books/ios-swiftui/wrapping-a-uiviewcontroller-in-a-swiftui-view – lorem ipsum Dec 19 '20 at 21:11

1 Answers1

0

The YOLineChartImage draws a UIImage, so here is fixed code:

struct TeamsView: View {
    
   private var image: UIImage = {         // << here !!
     let image = YOLineChartImage()
     image.strokeWidth = 4.0
     image.strokeColor = UIColor(displayP3Red: 1.0, green: 0.0, blue: 0.0, alpha: 1.0)
     image.values = [0.0, 1.0, 2.0]
     image.smooth = false     
     return image.drawImage(frame, scale: 1)   // << here !!
   }()
   
   var body: some View {
        VStack{
            Text("Teams")
            List([Team(id: "1", name: "Liverpool"),
                  Team(id: "2", name: "Man Utd.")]) {
                    team in TeamRow(team: team)
                }
            Image(uiImage: image)        // << here !!
        }
    }
}
Asperi
  • 228,894
  • 20
  • 464
  • 690