无法转换“CGPoint!”类型的值到预期的参数类型'UnsafeMutablePointer<CGPoint>'

Posted

技术标签:

【中文标题】无法转换“CGPoint!”类型的值到预期的参数类型\'UnsafeMutablePointer<CGPoint>\'【英文标题】:Cannot convert value of type 'CGPoint!' to expected argument type 'UnsafeMutablePointer<CGPoint>'无法转换“CGPoint!”类型的值到预期的参数类型'UnsafeMutablePointer<CGPoint>' 【发布时间】:2016-03-30 11:55:15 【问题描述】:

我遇到了一个快速构建错误。

func pathRefFromText() -> CGPathRef 

        let attributed : NSAttributedString = self.attrubutedText
        let line : CTLineRef = CTLineCreateWithAttributedString(attributed as! CFAttributedStringRef)
        let runArray : CFArrayRef = CTLineGetGlyphRuns(line)
        for var runIndex = 0; runIndex < CFArrayGetCount(runArray); runIndex++ 
            let run: CTRunRef = (CFArrayGetValueAtIndex(runArray, runIndex) as! CTRunRef)
           // let runFont : CTFontRef = CFDictionaryGetValue(CTRunGetAttributes(run), kCTFontAttributeName)
            for(var runGlyphIndex = 0; runGlyphIndex < CTRunGetGlyphCount(run); runGlyphIndex++)
            
                let thisGlyphRange : CFRange = CFRangeMake(runGlyphIndex, 1)
                let glyph : CGGlyph!
                let position : CGPoint!

               // The build error comes in these two lines
                CTRunGetGlyphs(run, thisGlyphRange, glyph)
                CTRunGetPositions(run, thisGlyphRange, position)

            
        
    

我收到一个构建错误,提示无法转换“CGPoint!”类型的值到预期的参数类型 'UnsafeMutablePointer'

【问题讨论】:

【参考方案1】:

尝试使用:

var glyph : CGGlyph = CGGlyph()
var position : CGPoint = CGPoint()

CTRunGetGlyphs(run, thisGlyphRange, &glyph)
CTRunGetPositions(run, thisGlyphRange, &position)

【讨论】:

从技术上讲,这应该是var glyph: CGGlyph? = nil(尽管= nil 纯粹是可选的)。这里调用的函数没有用nullability annotations 更新,但是如果是&,它肯定会被标记为采用可选而不是非可选。该函数当然不希望您实例化一个值只是为了让它覆盖......

以上是关于无法转换“CGPoint!”类型的值到预期的参数类型'UnsafeMutablePointer<CGPoint>'的主要内容,如果未能解决你的问题,请参考以下文章

无法转换类型“(_)-> Void?”的值到预期的参数类型'(() - > Void)?

无法转换类型“[String : AnyObject]?”的值到预期的参数类型“[NSAttributedStringKey:Any]?”

无法转换类型“Meme!”的值到预期的参数类型'@noescape (Meme) throws -> Bool'

无法转换类型“Range<Int>?”的值到预期的参数类型'Range<String.Index>?

无法转换类型“[String]?”的值到预期的参数类型“字符串?”快速显示结构中的图像时

swift Xcode单元测试:无法将“Person”类型的值转换为预期的参数类型“Person”