swift - 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?
解决方案
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)
推荐阅读
- jquery - Sweet Alert 2 复选框值与 sweetalert2 确认值冲突
- r - 特征图 R 显示 NULL
- google-apps-marketplace - 内部域部署引发错误
- android - 带有阴影动画的安卓号码
- node.js - NPM 使用 React + Node + 同时运行构建如何?
- angular - Angular Material:在我的 mat-selection-list 中,我正在尝试使用一组对象动态创建 mat-list-options 但出现错误?
- javascript - 获取 HTML 元素的 value 参数
- r - Rstudio git问题“错误:无法从'rpostback-askpass'读取askpass响应”
- ios - *** 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“致命:提供的标识符不是唯一的。” ***
- jquery - 如何在 Jquery 中选择没有子元素的元素?