0

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?

aheze
  • 24,434
  • 8
  • 68
  • 125
Uncoke
  • 1,832
  • 4
  • 26
  • 58

1 Answers1

-1

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)
zeytin
  • 5,545
  • 4
  • 14
  • 38
  • Thanks for helping me: I see this error: Fatal error: *Unexpectedly found nil while unwrapping an Optional value* on the first line – Uncoke Feb 07 '21 at 18:00
  • 1
    I got a different error: *Cannot convert value of type 'Data?' to expected argument type 'URL'* – Uncoke Feb 07 '21 at 18:20