Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8256955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:02:37+00:00 2026-06-08T02:02:37+00:00

I have a custom object in which I am trying to write a convenience

  • 0

I have a custom object in which I am trying to write a convenience class method that returns an array of CGPoint structures. The reason for this is there are several methods such as CGPathAddLines which take that as a parameter, and a class method would save me from converting my custom objects to an array of CGPoint structures every time. Unfortunately, I am not sure of the best way to achieve this, especially in regards to memory management.

Here is my implementation thus far:

+ (CGPoint*)CGPointsFromData:(NSArray*)data
{
    CGPoint *points = (CGPoint*)malloc(data.count * sizeof(CGPoint));
    DataPoint *dataPoint;
    int i = 0;
    for (id object in data) {
        if ([object isKindOfClass:[DataPoint class]]) {
            dataPoint = (DataPoint*)object;
            points[i] = dataPoint.point;
            i++;
        }
    }
    return points;
}

Edit: In case it isn’t clear, the DataPoint class has a property CGPoint point.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T02:02:38+00:00Added an answer on June 8, 2026 at 2:02 am

    If you want this to be a class method, place the word ‘alloc’ or something like it in the method name and place the obligation of free-ing the memory on the caller of your method, then simply return the pointer you allocated (no need to involve variables from any other scope inside this method). Then whomever calls this method can store the pointer in an instance variable and free it whenever appropriate.

    + (CGPoint*)allocCGPointsArrayWithData:(NSArray*)data
    {
        CGPoint *thesePoints = (CGPoint*)malloc(data.count * sizeof(CGFloat));
        DataPoint *dataPoint;
        int i = 0;
        for (id object in data) {
            if ([object isKindOfClass:[DataPoint class]]) {
                dataPoint = (DataPoint*)object;
                thesePoints[i] = dataPoint.point;
                i++;
            }
        }
        return thesePoints;
    }    
    

    and then to consume this (from the same class in this case)

    // @interface
    @interface FooViewController:UIViewController {
        CGPoint *_myPointsCArray;
    }
    
    // @implementation    
    -(void) viewWillAppear {
        _myPointsCArray = [[self class] allocCGPointsArrayWithData:self.dataArray];
        [super viewWillAppear];
    }
    -(void) viewWillDisappear {
        free(_myPointsCArray);
        [super viewWillDisappear];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a custom object that I was adding to an array via a
I have a List of custom object, which consist of a custom list. class
Say I have a list of member, each of which is a custom object:
I have an NSOperationQueue to which i have added my custom class's objects(inherits from
I have a custom object ExportType : public class ExportType{ protected String name; protected
I have a custom principal object that I want to be able to serialize
I have a class called XYZ inheriting from NSObject and an array which contains
I have couple of objects(1 custom object called appointment and event object) which i
I'm trying to create a controller for a custom complex object but have some
I have a custom DownloadFiles class which extends AsyncTask . The app launches itself

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.