问题
I'm trying to figure out CTFrameGetLineOrigins
from here: CTFrameGetLineOrigins Got Incorrect Origins, and trying to convert the ObjC to Swift.
Obj-C:
CFArrayRef lines = CTFrameGetLines(frame);
size_t numOfLines = CFArrayGetCount(lines);
CGPoint lineOrigins[numOfLines];
CTFrameGetLineOrigins(frame, CFRangeMake(0, 0), lineOrigins);
Swift:
let lines = CTFrameGetLines(frame)
let numOfLines: size_t = CFArrayGetCount(lines)
let lineOrigins = [CGPoint](repeating: CGPoint.zero, count: numOfLines)
CTFrameGetLineOrigins(frame, CFRangeMake(0, 0), lineOrigins)
But I am getting an error for the 3rd parameter of CTFrameGetLineOrigins:
Cannot convert value of type '[CGPoint]' to expected argument type 'UnsafeMutablePointer'
How do I get past this error?
回答1:
Actually I just added &
to the parameter and made the constant a variable.
let lines = CTFrameGetLines(frame)
let numOfLines: size_t = CFArrayGetCount(lines)
var lineOrigins = [CGPoint](repeating: CGPoint.zero, count: numOfLines)
CTFrameGetLineOrigins(frame, CFRangeMake(0, 0), &lineOrigins)
回答2:
You can try
CTFrameGetLineOrigins(frame, CFRangeMake(0, 0), UnsafeMutablePointer(&lineOrigins))
or directly
CTFrameGetLineOrigins(frame, CFRangeMake(0, 0),&lineOrigins)
来源:https://stackoverflow.com/questions/52320155/how-do-you-satisfy-the-lineorigins-argument-in-ctframegetlineorigins-in-swif