作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要使用 NSGlyph 作为 Swift 字典中的键
var glyph:NSGlyph //set to a glyph
var glyphDict:[NSGlyph:CGPath] //Will contain a cache of glyphs previously converted to paths
var path = glyphDict[glyph]
但我得到:
error: '[NSGlyph : CGPath]?' does not have a member named 'subscript'
所以我猜苹果还没有为 NSGlyph 定义下标?
我从 Apple 的 VectorTextLayer 示例代码中找到了这段代码,它成功地使用 CGGlyph 作为 CFDictionary 中的键。我如何调整它以使其在 Swift 字典中工作?
CGPathRef path = (CGPathRef)CFDictionaryGetValue(glyphDict, (const void *)(uintptr_t)glyph);
我知道代码正在将 CGGlyph 包装到 uintptr_t 中。我怎样才能在 Swift 中做到这一点?
最佳答案
我认为您在问题中复制了不同的代码。当您用作字典的变量是可选的时会发生该错误,因此声明为:
var glyphDict:[NSGlyph:CGPath]?
要解决这个问题,您可以使用可选链接从字典中读取:
var path = glyphDict?[glyph]
请注意,path
本身是可选的。
关于ios - 用于 Swift 字典键的 NSGliff - 使用 uintptr_t?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26904697/
我需要使用 NSGlyph 作为 Swift 字典中的键 var glyph:NSGlyph //set to a glyph var glyphDict:[NSGlyph:CGPath] //Wil
我是一名优秀的程序员,十分优秀!