I am creating an app that tracks a drive, so as you drive it records location and at the end, it shows a map of where you have been. I am using swiftUI's MapKit to display map data. In the process of displaying the information, I ran into a problem.
I plan to use MapKit polyline (MKPolyline) to show the location points but to initialize an MKPolyline, you must first have an unsafe pointer of either MKMapPoints or CLLocationCoordinate2Ds.
init(points: UnsafePointer<MKMapPoint>, count: Int)
init(coordinates: UnsafePointer<CLLocationCoordinate2D>, count: Int)
I have an array of coordinates but need an unsafe pointer instead. How do I go about converting them?
Thanks to Leo and Rob. They told me how. I just put the array right into the coordinates and swift handled the rest for me.
let Line = MKPolyline(coordinates: Locations, count: Locations.count)
Check out this thread for a more in depth answer
Related
so I have a final project for a class where I need to make a video game in LabView. The issue I'm having at the moment is that I can't figure out the 'right' way to put 'yourShip.png' into the 2d array of 2d pictures at [0,0]. Every tutorial I can find basically has exactly what I have down below in the screenshot, and it makes sense to me. However, running the program quickly shows that it does nothing.
To describe the code, I have a path constant that leads to the picture, which feeds to a draw flattened pixelmap function. Up to this point I know the code works, since creating a test indicator reveals as such. However, next I try to use the replace array subset function to replace the (default blank) 2d picture at [0,0] with yourShip.png. 'screen' is a 5x5 2d array of 2d pictures. The local variable of the same name being outputted to is indeed the very same array.
My main guess with why my code doesn't work is because of the way I'm taking screen as the input variable and then outputting to it via a local variable. However, if this is wrong, I'm confused with how I should do it. All I want to do is 'spawn' the image at the correct index.
The replace array subset works quite literally, i.e. it can only replace existing elements.
If there is no element at the specified index because the array is smaller, the function will do just nothing.
I guess your array is empty, so, initialize your screen array first to a size of at least 1x1.
I want to resample an array of elements using the command idresamp(). The input arguments for idresamp function is an array x. So I should get the output as an array. However, I am getting a structure iddata. I don't know how to access the elements /result of the resampling. Can somebody please show how to access the resampled values? Thank you.
x=rand(4000,1); %create some arbitrary data
x_resamp =idresamp(x,2); %resampling factor is 2
Here x_resamp is of iddata type. So, I am unable to access the result. On clicking the variable x_resamp this is what I got
How does one access the resampled values (output). Where is the array? The next step is to calculate the power after resampling and hence I need to use the resampled values.
I am using Matlab R2018a.
If you just want to resample by a factor 2, and have access to the Signal Processing Toolbox, use resample:
y = resample(x,2,1);
If you are insistent on using idresamp, you need to know that it returns an object of type iddata, which comes with a long documentation on usage. I think this complicates things more than you are looking for. It seems you should be able to do:
x_resamp = idresamp(x,2);
y = x_resamp.OutputData;
(but I can't test this because I don't have access to this toolbox.)
After watching build better apps with value type . In the photoshop example they made, they said that
the only thing that gets copied in the two instances of that diagram are the tiles that contain the person's shirt. So even though I have two distinct documents, the old state and the new state, the only new data that I have had to consume as a result of that is the tiles contained in this person's shirt.
So I begin to wonder how would these two array in memory looks like. So I do a little experiment.
struct Test {
var i: Int
var j: Int
}
var valueArray = [Test(i: 1, j: 9), Test(i: 2, j: 7)]
var valueArray2 = valueArray
When I print valueArray and valueArray2's address, they are not the same.
"Maybe they implement this by store pointer in array?"
But when I print memory content using lldb , they are actually just 4 Int (1,9,2,7).
So I am confused, I haven't even change the array yet. And they seems to make a copy of entire array? So where did I misunderstand?
The function I used to print struct's address is by using the method provided by #nschum in this question.
func address(o: UnsafePointer<Void>) {
let addr = unsafeBitCast(o, Int.self)
print(NSString(format: "%p", addr))
}
This is not a duplicate question of this question. I am asking about language feather and the other one is about programming skill.
Okay, I did many experiment and finally figured out.
We can's use & to get array address because once we do that , Swift will copy the array to better interact with C, use & get object's address that adjacent to array and do the math instead. Or use lldb instruction frame variable -L
The whole Array is copied once any of it's value element changed.
Actual value element of Array is allocated at heap.
Swift also did a lot of optimization for Array whose element is class.
Swift is awesome.
I actually write my first blog for this.
After your comments and getting a better understanding if this, I loaded it up in a playground and it seems to be working as expected
original answer for reference
The thing to remember with this is that structs are basically chunks of data in memory. When you create the valueArray, a chunk of memory is being set to value it's assigned
When you create valueArray2, you're creating a new instance of the struct, which means it will have a brand new chunk of memory, and you're then setting the value of that new chunk of memory to the same value of the chunk of memory from the valueArray. This results in a copy of the data in two different memory locations.
This is in contrast to an object, in which case valueArray would be a pointer to a chunk of memory, and when you create valueArray2 it would be creating a new pointer to the same chunk of memory.
I have an array of street addresses, I want my app to sort that array starting with the closest address to my location (GPS).
Now that Apple has released iOS Maps in iOS 6, what is the easier way to do that?
Thanks,
Matt.
Approach I'd take would be use CLGeocoder to obtain CLLocation instances for each address (there are several async methods provided by the class to do this). Save the values obtained if needed for future sorts to save having to re-obtain them. Get your location as a CLLocation instance. Use -[ CLLocation distanceFromLocation: ] to obtain distance between your device location and each address location. Sort on these distance values.
Hope this helps!
Mark
I have a application that will capture the screen and I want to write the captured information to an array, this takes AGES as the array ends up being +2million values. I am iterating and adding the values to the array, is there any way quicker (eg binary operations)? Should it be this slow? Why is it?
Assuming your GetPixel'ing the screen pixel by pixel, its the GetPixel call that's slow (it interrogates the display driver) not the (pre-dimensioned) array assignment.
You can instead use the getdibits() api which will copy the DC's colour info into a buffer in a single call.
Here is a C++ example, but the methodology & call sequence is the same as for VB.
Figured out why it was so slow, it was because I was using ReDim on every iteration of the loop - thanks for the help anyways
Martin