Creating plain C array of structs

234 views
Skip to first unread message

Richard Venneman

unread,
Oct 29, 2012, 8:26:25 AM10/29/12
to rubym...@googlegroups.com
Hi,

I'm trying to create a MKPolyline with the MKPolyline#polylineWithCoordinates method. This method takes an array with coords as a parameter. I created an array like this:

coordinates = annotations.collect { |a| a.coordinate }

When I try to use this array with the polylineWithCoordinates method, the app crashes with the following error message:

2012-10-29 13:19:31.986 Cityguide[26645:c07] *** Terminating app due to uncaught exception 'TypeError', reason: 'path_map_view_controller.rb:38:in `drawPath': expected instance of Pointer, got `[#<CLLocationCoordinate2D latitude=53.1852416992188 longitude=5.54879379272461>, #<CLLocationCoordinate2D latitude=53.1885986328125 longitude=5.555419921875>]' (Array) (TypeError)

From this answer on SO, that apparently happens because of this reason: The polylineWithCoordinates method requires a plain C array of structs of typeCLLocationCoordinate2D.

Usually these plain C arrays apparently get created with malloc(). But that doesn't work in RubyMotion. So can anyone help me making polylineWithCoordinates work with the correct array type?

Thanks :)

Colin Thomas-Arnold

unread,
Oct 29, 2012, 9:56:03 AM10/29/12
to rubym...@googlegroups.com
You'll need to use the `Pointer` class (http://www.rubymotion.com/developer-center/guides/runtime/#_pointers).

One example in particular from that page: rect_ptr = Pointer.new(CGRect.type)

An optional second argument is the 'length'.  So in your case, I'm pretty sure this is what you need:

good_real_estate = [location, location, location]
ptr = Pointer.new(CLLocation2D.type, good_real_estate.length)
ptr[0] = good_real_estate[0]
ptr[1] = good_real_estate[1]
ptr[2] = good_real_estate[2]


And there is a sugarcube helper for this!

good_real_estate = [location, location, location]
ptr = good_real_estate.to_pointer(CLLocation2D.type)

--
 
 
 

Richard Venneman

unread,
Oct 29, 2012, 10:22:33 AM10/29/12
to rubym...@googlegroups.com
Thanks a lot Colin!
Creating a pointer with the CLLocationCoordinate2D type works perfectly.
Reply all
Reply to author
Forward
0 new messages