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?

4

1 回答 1

0

dataSvg由于是零,您无法收到图标。您可以try用于检查零,如下所示:

     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)
于 2021-02-07T17:53:32.797 回答