6

When I need to add a integer to an array I use

[myArray addObject:[NSNumber numberWithInt:myInt]];

but what about CGPaths? How do I add them to the array?

I need something like

"[NSValue valueWithCGPath:myPath]"... ???!!!

any clues? thanks

Duck
  • 34,902
  • 47
  • 248
  • 470
  • 2
    You might want to have a look at this question: http://stackoverflow.com/questions/4240481/how-to-store-cggradientref-in-nsmutablearray – albertamg Jun 19 '11 at 17:35
  • this is brilliant, thanks. It will serve for other stuff I am doing, but for this case it is crashing, because after adding the object to the array I archive it. As CGPaths are CFType, the archiving crashes... but anyway, as I said, it will serve for other purposes in the same app. Thanks!!!!! +1 – Duck Jun 19 '11 at 17:46

1 Answers1

11

The easiest way if you can target iOS 3.2 or higher is to wrap the CGPathRef in a UIBezierPath:

[myArray addObject:[UIBezierPath bezierPathWithCGPath:myPath]];

If you need to support earlier iOS versions, you can use a CFArray instead of NSArray because CGPath derives from CFType.

Daniel Dickison
  • 21,832
  • 13
  • 69
  • 89
  • Thanks. I have tried that but my problem is this: after adding all paths to the array, using the code you posted, I archive it using NSKeyedArchiver. When I try to unarchive that, it crashes. Is there a way to convert the path to another thing (instead of a UIBezierPath) and then store it on the array? I am just building this array for archival purposes. – Duck Jun 19 '11 at 17:36
  • Are you sure the archive actually produces anything? You should probably post a separate question, how to archive and restore a UIBezierPath. You may have to store the points and path metadata manually. – Kendall Helmstetter Gelner Jun 19 '11 at 18:10
  • Kendall - **[here it is](http://stackoverflow.com/questions/6400284/iphone-storing-and-retrieving-a-uibezierpath-with-nskeyedarchiver-crashing)** without any working solution. Yes, the archive is storing the data and the bezier data is loading successfully to NSData variable when unarchiving, but when I try to decode the data and produce a UIBezierPath, it crashes. – Duck Jun 19 '11 at 18:33