• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: dynamic NSPointArray allocation
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: dynamic NSPointArray allocation


  • Subject: Re: dynamic NSPointArray allocation
  • From: "Henry McGilton (Boulevardier)" <email@hidden>
  • Date: Sat, 28 Nov 2009 15:12:22 -0800

On Nov 28, 2009, at 2:50 PM, Quincey Morris wrote:

> On Nov 27, 2009, at 14:58, Shane wrote:
>
>> I think I'm understanding this in part ...
>>
>> // *.h
>> 	NSMutableData *pointData;
>> 	NSPointArray *points;
>>
>> // *.m
>> 	pointData = [[NSMutableData alloc] init];
>>
>> 	...
>>
>> 	NSPoint point = NSMakePoint([iters floatValue], [mse floatValue]);
>> 	// not sure how to get an NSPoint into NSMutableData?
>>
>> 	// here I resize NSMutableData, but this will always only be
>> 	// increasing the length by 1.
>> 	[pointData increaseLenghtBy:sizeof(NSPoint)];
>>
>> 	// and here I assign the data to NSPointArray
>> 	points = (NSPoint *) [pointData bytes];
>>
>> And from here I can pass this NSPointArray around till I get to the
>> point where I will use this data for my NSBezierPath. Will
>> NSBezierPath (which takes an NSPointArray) be able to properly read
>> from 'points' by doing the above?
>
> In part, but not quite. Although Graham's advice was good, and might be a better approach, for completeness here's how this approach works.
>
> You only need one instance variable:
>
> 	NSMutableData *pointData;
>
> which you can initialize as above.
>
> To add a point, I'd do something like this:
>
> 	NSUInteger pointCount = [pointData length] / sizeof (NSPoint);
> 	[pointData setLength: (pointCount  + 1) * sizeof (NSPoint)];
> 	NSPointArray points = [pointData mutableBytes];
> 	points [pointCount] = somePoint;
>
> To add the points to a Bezier path:
>
> 	[somePath appendBezierPathWithPoints: [pointData mutableBytes] count: [pointData length] / sizeof (NSPoint)];
>
> Note:
>
> 1. 'NSPointArray*' isn't the correct type for an array of points. Plain 'NSPointArray' is what you want.
>
> 2. You will get a compiler warning if you try to assign [pointData bytes] to the NSPointArray variable, because the [point bytes] is of type 'const void*'. You need [pointData mutableBytes] which is just of type 'void*'.
>
> 3. There's no need to cast [point mutableBytes] to NSPointArray because there's no warning assigning from 'void*' to a compatible pointer type. Casting it is not wrong, but any time you write an explicit cast, you're overriding much of the compiler's type checking, so it's easy to introduce bugs that way. (Not likely in this case, but as a general consideration.) It's a personal thing, though -- I guess some people like to have the explicit cast because it documents what the code is supposed to be doing.

Another possible approach is simply use a NSMutableArray full of NSValue objects that contain
the NSPoint structures . . .

    Cheers,
        . . . . . . . .    Henry


_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: dynamic NSPointArray allocation
      • From: Andrew Farmer <email@hidden>
References: 
 >dynamic NSPointArray allocation (From: Shane <email@hidden>)
 >Re: dynamic NSPointArray allocation (From: Quincey Morris <email@hidden>)
 >Re: dynamic NSPointArray allocation (From: Shane <email@hidden>)
 >Re: dynamic NSPointArray allocation (From: Quincey Morris <email@hidden>)

  • Prev by Date: Re: dynamic NSPointArray allocation
  • Next by Date: Re: Is there a key path for this?
  • Previous by thread: Re: dynamic NSPointArray allocation
  • Next by thread: Re: dynamic NSPointArray allocation
  • Index(es):
    • Date
    • Thread