0

I am trying to create an array of CGPoint from an NSArray whose each entry is NSPoint

 NSArray *points = [_terrain getPathPoints];
 CCLOG(@"Terrain Points=%@",points);

Output in log:

Terrain Points=(
    "NSPoint: {-3.4324746, 70.220947}",
    "NSPoint: {-3.9852712, 70.136444}",
    "NSPoint: {-4.3119373, 70.058411}",
    "NSPoint: {-4.4321384, 69.986282}",
    "NSPoint: {-4.3655362, 69.919479}",
    "NSPoint: {-4.131794, 69.857513}",
    "NSPoint: {-3.7505748, 69.799774}"
)

getPathPoints is a method from a third party tool that I am using. This method returns an array of NSPoint. How can I create an array of CGPoint from the above data?

2 Answers 2

2

What you're seeing are most likely NSValue objects created from CGPoint structs (you cannot put a CGPoint or NSPoint directly into an NSArray). The fact that they log as NSPoint may be a bit confusing (a vestige from the Mac, NSPoint is essentially the same as CGPoint), but you can easily convert such a "boxed" value back to a CGPoint with the CGPointValue method, e.g.

NSArray *points = [_terrain getPathPoints];
NSValue *lastPointValue = [points lastObject];
CGPoint lastPoint = [lastPointValue CGPointValue];
//...
Sign up to request clarification or add additional context in comments.

Comments

1

Did you read the documentation?

Graphics Type Conversions

When going back and forth between Cocoa and Quartz code, some conversion of data types may be necessary. Table 9-1 shows the Cocoa equivalents of some basic Quartz types.

Table 9-1  Simple data-type conversions
    Cocoa type       Quartz type
      NSRect           CGRect
      NSPoint          CGPoint
      NSSize           CGSize

Although in each case the structure layout is the same, you cannot pass the Quartz data type directly to a method expecting the Cocoa type. To convert, you must cast from one type to another, as shown in the following example:

NSRect cocoaRect = *(NSRect*)&myCGRect;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.