Create NSArray from function return

Discussion in 'iOS Programming' started by VentralChaos, Feb 2, 2009.

  1. VentralChaos macrumors newbie

    Jan 15, 2009
    I'm sure this is a pretty simple answer, although I can't seem to figure out the syntax. I need an NSArray to contain the contents of a function return. (From Stanford CS193P)

    + (NSArray *)pointsForPolygonInRect:(CGRect)rect numberOfSides:(int)numberOfSides
    NSArray *myPoints = [[NSArray alloc] [pointsForPolygonInRect:bounds numberOfSides:5]];

    That obviously does not compile but hopefully illustrates my request
  2. ghayenga macrumors regular

    Jun 18, 2008
    You mean:
    NSArray *myPoints = [[NSArray alloc] initWithArray:[pointsForPolygonInRect:bounds numberOfSides:5]];
  3. VentralChaos thread starter macrumors newbie

    Jan 15, 2009
    yes, ghayenga, I can tell you understand what I mean, but your suggestion does not compile "error: 'pointsForPolygonInRect' undeclared (first use in this function)"
  4. detz macrumors 65816

    Jun 29, 2007
    Where does pointsForPolygonInRect come from? What object has that method? If that function is in a UIView object of TestView then this works fine....

    NSArray *myArray = [TestView pointsForPolygonInRect:CGRectMake(0, 0, 100, 100) numberOfSides:4];

    pointsForPolygonInRect, the way you have it defined above is a class function so you have to call it on a class not an object.
  5. VentralChaos thread starter macrumors newbie

    Jan 15, 2009
    This are the instructions I'm using:

    it's part of the PolygonView class that I created, but maybe I put it in the wrong place?

    I've attached my project so maybe you can get a clearer picture. As always, all help is appreciated, thanks

    EDIT: sooo...I figured it out I guess. I needed to make it a part of the PolygonShape class and create some new connections and other stuff. Anyways, now it is working, however I have one tiny question. In this case, why did I need to change the method to a '-' instead of a '+' like the code was given to me? Originally it wouldn't work but as soon as I made it a '-' it worked...

    Attached Files:

  6. dejo Moderator


    Staff Member

    Sep 2, 2004
    The Centennial State
    + = class method
    - = instance method

Share This Page