Xcode 12 and SVG from a string/text

694 Views Asked by At

I'm trying to manipulate SVG using simple XML codes to add SVG into Xcode using the framework SVGKit.

But I got a fatal error:

Fatal error: Unexpectedly found nil while unwrapping an Optional value: file aiSoccer/DetailViewController.swift, line 86

Here is the code:

let dataSvg = "<svg width=\"100\" height=\"100\"><circle cx=\"50\" cy=\"50\" r=\"40\" stroke=\"green\" stroke-width=\"4\" fill=\"yellow\" /></svg>".data(using: .utf8)
    
let receivedIcon: SVGKImage = SVGKImage(data: dataSvg) // LINE 86

self.imageView.image = receivedIcon.uiImage

I'm a noob with Xcode/Swift, can you help me?

1

There are 1 best solutions below

2
On

You can't receive the Icon due to dataSvg is nil. You can use try for checking the nil as below:

     let dataSvg = "<svg width=\"100\" height=\"100\"><circle cx=\"50\" cy=\"50\" r=\"40\" stroke=\"green\" stroke-width=\"4\" fill=\"yellow\" /></svg>".data(using: .utf8)
     let data = try? Data(contentsOf: dataSvg)
     let receivedIcon: SVGKImage = SVGKImage(data: data)