首页 > 解决方案 > Xcode 12 and SVG from a string/text

问题描述

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?

标签: swiftxcodesvgkit

解决方案


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)

推荐阅读