# CGPathApply

**URL:** <https://forums.swift.org/t/cgpathapply/2594>\
**Category:** Using Swift\
**Created:** [May 13, 2016, 4:04am UTC](https://forums.swift.org/t/cgpathapply/2594 "2016-05-13T04:04:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jon\_Hull](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jon_hull/32/678_2.png) [@Jon\_Hull](https://forums.swift.org/u/Jon_Hull)\
**Post date:** [May 13, 2016, 4:04am UTC](https://forums.swift.org/t/cgpathapply/2594/1 "2016-05-13T04:04:28Z")

</div>

I am having the darnedest time trying to figure out how to use CGPathApply with a swift function in Swift 2.2+. I tried it before in Swift 1.2 and it wasn’t possible without bridging to ObjC, but I am fairly sure that changed with Swift 2. Every search I do just comes up with people saying it isn’t possible in Swift 1.

Does anyone know how to pull this off? (I am trying to read in a CGPath and create my own enum-based path struct for easy editing)

Thanks,  
Jon

---

<div class="post-metadata">

**Author:** ![zwaldowski](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/zwaldowski/32/21801_2.png) [@zwaldowski](https://forums.swift.org/u/zwaldowski)\
**Post date:** [May 13, 2016, 5:24am UTC](https://forums.swift.org/t/cgpathapply/2594/2 "2016-05-13T05:24:36Z")

</div>

This seems to do it:

> <https://gist.github.com/zwaldowski/7e6eacc9dea0682690a820afa62e54ff>

Problems are just like any context-based C API, you need to squeeze all  
the important stuff into a word-size thing.

Cheers!  
Zachary Waldowski  
zach@waldowski.me

> **···**
>
> On Thu, May 12, 2016, at 09:04 PM, Jonathan Hull via swift-users wrote:
> 
> > I am having the darnedest time trying to figure out how to use  
> > CGPathApply with a swift function in Swift 2.2+. I tried it before in  
> > Swift 1.2 and it wasn’t possible without bridging to ObjC, but I am  
> > fairly sure that changed with Swift 2. Every search I do just comes up  
> > with people saying it isn’t possible in Swift 1.
> > 
> > Does anyone know how to pull this off? (I am trying to read in a CGPath  
> > and create my own enum-based path struct for easy editing)
> > 
> > Thanks,  
> > Jon  
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-users mailing list  
> > swift-users@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-users](https://lists.swift.org/mailman/listinfo/swift-users)

---

<div class="post-metadata">

**Author:** ![eskimo](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/eskimo/32/13558_2.png) [@eskimo](https://forums.swift.org/u/eskimo)\
**Post date:** [May 13, 2016, 7:57am UTC](https://forums.swift.org/t/cgpathapply/2594/3 "2016-05-13T07:57:24Z")

</div>

… and the best way to do that is to use a closure.

typealias CGPathApplierBlock = @convention(block) (CGPathElement) -\> Void

func CGPathApplyBlock(path: CGPath, @noescape block: CGPathApplierBlock) {  
&nbsp;&nbsp;&nbsp;CGPathApply(path, unsafeBitCast(block, UnsafeMutablePointer\<Void\>.self), { info, element in  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;let block2 = unsafeBitCast(info, CGPathApplierBlock.self)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;block2(element.memory)  
&nbsp;&nbsp;&nbsp;})  
}

Share and Enjoy

> **···**
>
> On 13 May 2016, at 06:24, Zach Waldowski via swift-users \<swift-users@swift.org\> wrote:
> 
> > Problems are just like any context-based C API, you need to squeeze all
> 
> --  
> Quinn "The Eskimo!" \<[http://www.apple.com/developer/&gt](http://www.apple.com/developer/&gt);  
> Apple Developer Relations, Developer Technical Support, Core OS/Hardware

---

<div class="post-metadata">

**Author:** ![zwaldowski](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/zwaldowski/32/21801_2.png) [@zwaldowski](https://forums.swift.org/u/zwaldowski)\
**Post date:** [May 13, 2016, 3:40pm UTC](https://forums.swift.org/t/cgpathapply/2594/4 "2016-05-13T15:40:52Z")

</div>

100% right. That solution is by far better if you wanted to work with  
the CGPathElement, since that's block-representable. My solution was  
assuming (perhaps wrongly) to want to represent it as a Swift type.  
You're the best, as always! :)

Cheers!  
Zach

> **···**
>
> On Fri, May 13, 2016, at 12:57 AM, Quinn The Eskimo! via swift-users wrote:
> 
> > On 13 May 2016, at 06:24, Zach Waldowski via swift-users \> \<swift-users@swift.org\> wrote:
> > 
> > \> Problems are just like any context-based C API, you need to squeeze all
> > 
> > … and the best way to do that is to use a closure.
> > 
> > typealias CGPathApplierBlock = @convention(block) (CGPathElement) -\> Void
> > 
> > func CGPathApplyBlock(path: CGPath, @noescape block: CGPathApplierBlock)  
> > {  
> > &nbsp;&nbsp;&nbsp;CGPathApply(path, unsafeBitCast(block,  
> > &nbsp;&nbsp;&nbsp;UnsafeMutablePointer\<Void\>.self), { info, element in  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;let block2 = unsafeBitCast(info, CGPathApplierBlock.self)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;block2(element.memory)  
> > &nbsp;&nbsp;&nbsp;})  
> > }
> > 
> > Share and Enjoy  
> > --  
> > Quinn "The Eskimo!" \<[http://www.apple.com/developer/&gt](http://www.apple.com/developer/&gt);  
> > Apple Developer Relations, Developer Technical Support, Core OS/Hardware
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-users mailing list  
> > swift-users@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-users](https://lists.swift.org/mailman/listinfo/swift-users)
