b.stevens.photo
b.stevens.photo

Reputation: 934

How to use swift convenience init from SwiftSVG

New to swift, go easy on me.

I'm trying to implement SwiftSVG - https://github.com/mchoe/SwiftSVG#install and in its documentation its using the below code, I'm not sure how to implement this though and not sure what to search for online to learn more about it. Where do I put the convenience init?

// Declaration
convenience init(pathString: String)

// Example
let triangle = UIView(pathString: "M75 0 l75 200 L0 200 Z")
self.addSubview(triangle)

Currently I've got a struct like the one below which makes sense to me but I'm not sure how to properly implement the convenience init, everywhere I put it throws an error...

convenience init(pathString: String)

struct SVGImage: UIViewRepresentable {

    var svgName: String

    func makeUIView(context: Context) -> UIView {
      let svgView = UIView(pathString: "M75 0 l75 200 L0 200 Z")
      return svgView
    }

    func updateUIView(_ uiView: UIView, context: Context) {

    }
}

Upvotes: 0

Views: 482

Answers (1)

jnpdx
jnpdx

Reputation: 52367

The convenience init is already built in to the library (https://github.com/mchoe/SwiftSVG/blob/master/SwiftSVG/SVG%20Extensions/UIView%2BSVG.swift) -- you don't have to define it yourself.

However, Xcode won't know about the convenience init and it'll cause a complication error unless the library is imported.

Put import SwiftSVG at the top of the file.

Upvotes: 1

Related Questions