我有一个创建cfattributedStringRef的obj-c类:

- (CFAttributedStringRef)createAttributedStringWithByteRange:(NSRange)byteRange
{
  UInt8 bytes[byteRange.length];
  [self.data getBytes:&bytes range:byteRange];

  CFStringRef string = CFStringCreateWithBytes(NULL, bytes, byteRange.length, kCFStringEncodingUTF8, (byteRange.location == 0));

  return CFAttributedStringCreate(NULL, string, (__bridge CFDictionaryRef)(self.textAttributes));
}

我试图从一个swift对象调用这个函数,它应该创建一个cttypesetter:
let stringToDraw = self.storage.createAttributedStringWithByteRange(self.line.range)
let typesetter = CTTypesetterCreateWithAttributedString(stringToDraw)

这给了我这个构建错误:
error: cannot convert the expression's type 'CTTypesetter!' to type '$T3'
let typesetter = CTTypesetterCreateWithAttributedString(stringToDraw)
                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

有人知道我做错了什么吗?

最佳答案

我发现了问题,构建错误误导了我。
实际问题是前一行我的obj-c方法将CFAttributedStringRef桥返回到Unmanaged<CFAttributedString>并且需要将其作为管理对象引入swift,然后才能通过takeRetainedValue()方法使用:

let stringToDraw = self.storage.createAttributedStringWithByteRange(self.line.range).takeRetainedValue()
let typesetter = CTTypesetterCreateWithAttributedString(stringToDraw)

10-05 20:03