CTFrameGetLineOrigins( ) Issue occurred when I translated the project from OC to Swift

57 views Asked by At

My Situation:

I am trying to translate an open source project, written in Objective-C, to Swift. I have been obstructed by an CGPoint Struct issue for several days.

CGPoint lineOrigins[numberOfLines];  

// An array includes some CGPoint elements which written in OC.

After solved it, I am not sure whether it correct because complier thrown none error.

var lineOrigins = [CGPoint](count: numberOfLines, repeatedValue: CGPointZero)

// The Swift Code translated from above OC Code.

Another CTFrameGetLineOrigins() Issue just showed in my project.

Part of Objective-C Code:

- (void)drawFramesetter:(CTFramesetterRef)framesetter
   attributedString:(NSAttributedString *)attributedString
          textRange:(CFRange)textRange
             inRect:(CGRect)rect
            context:(CGContextRef)c
{
                      .
                      .
                      .
CFArrayRef lines = CTFrameGetLines(frame);
NSInteger numberOfLines = CFArrayGetCount(lines);
CGPoint lineOrigins[numberOfLines];
CTFrameGetLineOrigins(frame, CFRangeMake(0, numberOfLines), lineOrigins);
                      .
                      .
                      .
}

Part of Swift Code:

func drawFramesetter(framesetter: CTFramesetterRef,
attributedString _attributedString: NSAttributedString,
textRange _textRange: CFRange,
inRect rect: CGRect, context c: CGContextRef) {
                      .
                      .
                      .
var lines: CFArrayRef = CTFrameGetLines(frame)
var numberOfLines: NSInteger = CFArrayGetCount(lines)
var lineOrigins = [CGPoint](count: numberOfLines, repeatedValue: CGPointZero)
CTFrameGetLineOrigins(frame, CFRangeMake(0, numberOfLines), lineOrigins)
                      .
                      .
                      .
}

My Problem:

The error, Cannot invoke 'CTFrameGetLineOrigins' with an argument list of type '(CTFrame, CFRange, [(CGPoint)])', showed up.

  • After checked the official document, I found that CTFrameGetLineOrigins is declared as CTFrameGetLineOrigins(frame: CTFrame!, range: CFRange, origins: UnsafeMutablePointer<CGPoint>) in Swift.

  • And it also be declared as CTFrameGetLineOrigins(CTFrameRef frame, CFRange range, CGPoint origins[]) in Objective-C.

  • The difference between two function parameters is CGPoint. Come On!!! CGPoint Again?

My Question:

  • I guess the error was triggered by the parameter of CGPoint. I must make some mistakes when I translated [CGPoint] to UnsafeMutablePointer<CGPoint>. Can anyone tell what's difference between that two types?
  • Please tell how to solve this problem.

A big appreciation for your time and guide.

Ethan Joe

0

There are 0 answers