annoying parameter error for polylineWithCoordinates - ios

I am trying to initialize a polyline from a 2 element CLLocationCoordinate2D array called coordarray and the number 2, using this code:
self.line = MKPolyline.polylineWithCoordinates(coordarray,2)
however I am getting an error saying
Cannot invoke 'polylineWithCoordinates' with an argument list of type
([CLLocationCoordinate2D],Int)
I have checked the docs, and it seems that I have passed the correct parameter list, what am I missing?

That error message is very misleading - checking the documentation for MKPolyline there isn't actually a static method called polylineWithCoordinates for Swift, you may be looking at the Objective-C version of the documentation*. Perhaps you meant to use:
convenience init!(coordinates coords: UnsafeMutablePointer<CLLocationCoordinate2D>,
count count: Int)
In which case I believe your code needs to be:
// Note - coordarray is declared as var
var coordarray = // ...
self.line = MKPolyline(coordinates: &coordarray, count: 2)
Here's a link detailing more about adding an MKPolyline overlay: http://mobiletoolworks.com/adding-a-mkpolyline-overlay-using-swift-to-an-ios-mapkit-map/
*To change the documentation from displaying Objective-C to Swift, there's a control in the top-right corner of the page:

Related

How to fix “Cannot invoke initializer for type 'MKMapRect' with an argument list of type '(MKMapRect)''” error in Swift 5.0

I am using Apple’s Footprint: Indoor Positioning with Core Location sample code to build a mobile application. This code is in Swift 3.0, and I am converting it to Swift 5.0 syntax.
When I run this Swift 3.0 code,
var corners = [MKMapPoint(x: MKMapRectGetMaxX(MKMapRect.world), y: MKMapRectGetMaxY(MKMapRect.world))]
return HideBackgroundOverlay(points: &corners, count: corners.count)
I get this error.
“'MKMapRectGetMaxX' has been replaced by property 'MKMapRect.maxX'”
When I changed the syntax to
var corners = [MKMapPoint(x: MKMapRect(MKMapRect.world).MaxX, y: MKMapRectGetMaxY(MKMapRect.world)),
I get this error message
“Cannot invoke initializer for type 'MKMapRect' with an argument list of type '(MKMapRect)'”
How can I get this line of code to run?
MKMapRect.world is already an instance of MKMapRect; There is no need to wrap it in the MKMapRect initialiser.
You simply want
var corners = [MKMapPoint(x: MKMapRect.world.MaxX, y: MKMapRect.world.maxY)]

Contextual Type AnyObject Cannot Be Used With Array Literal for Coordinates

I have look into several other different questions posted with the same problem but non of them help answer my question because I am using this for coordinates. I recently upgrade to Swift3 and I got this error "Contextual Type AnyObject Cannot Be Used With Array Literal"
So this is how the code looks like where I declare my variable for my coordinates
var coordinates: [AnyObject]!
This is the next code where the error occurs, I will replace the numbers with x. I am using the longitude and latitude
coordinates = [[xx.xxxxxx, -xxx.xxxxxx],[xx.xxxxxx, -xxx.xxxxxx],[xx.xxxxxx, -xxx.xxxxxx]]
And the way I call it is by using for loop which I dont think causes any problem but I'll just post it just in case
for i in 0...2
{
let coordinate = coordinates[i]}
Problem solved by implementing
var coordinates: [[Double]]!
Just change :-
import MapKit
class yourController : ...{
var coordinates: [AnyObject]!
To
var coordinates = [CLLocationCoordinate2D]()
To access a particular lat or long at a particular index just:-
coordinates[your_index].latitude // Latitude
coordinates[your_index].longitude //Longitude
CLLocationCoordinate2D is a struct , much more easily scalable and accessible than array.

Convert UnsafeMutablePointer<CLLocationCoordinate2D> to [CLLocationCoordinate2D] in Swift

I write an app in Swift and is bridging some Objective-C code. One of these classes has a method that looks like this: + (CLLocationCoordinate2D *)polylineWithEncodedString:(NSString *)encodedString;.
In Swift, it said this method returns a UnsafeMutablePointer<CLLocationCoordinate2D>. What I want is a Swift array of CLLocationCoordinate2D.
What obviously doesn't work, but I tried, is this:
let coordinates: [CLLocationCoordinate2D] = TheClass.polylineWithEncodedString(encodedString)
which will give me the following error:
'UnsafeMutablePointer<CLLocationCoordinate2D>' is not convertible to '[CLLocationCoordinate2D]'
Is it somehow possible to convert this UnsafeMutablePointer<CLLocationCoordinate2D> to a [CLLocationCoordinate2D]? Or should I take a different approach?
You can just use the memory property of the UnsafeMutablePointer, which is the data behind the pointer. But keep in mind that UnsafeMutablePointer < CLLocationCoordinate2D > will return one CLLocationCoordinate2D, not an array, just as declared in the obj c function.
var coordinate: CLLocationCoordinate2D = TheClass.polylineWithEncodedString(encodedString).memory

NSGliff for Swift Dictionary Key - Using uintptr_t?

I need to use an NSGlyph as the key in a Swift dictionary
var glyph:NSGlyph //set to a glyph
var glyphDict:[NSGlyph:CGPath] //Will contain a cache of glyphs previously converted to paths
var path = glyphDict[glyph]
but I get:
error: '[NSGlyph : CGPath]?' does not have a member named 'subscript'
So I guess Apple hasn't defined a subscript for NSGlyph?
I've found this code from Apple's VectorTextLayer Sample Code that successfully uses a CGGlyph as a key in a CFDictionary. How can I adapt this to work in a Swift dictionary?
CGPathRef path = (CGPathRef)CFDictionaryGetValue(glyphDict, (const void *)(uintptr_t)glyph);
I understand that code is wrapping the CGGlyph into a uintptr_t. How could I do this in Swift?
I think you've copied different code in your question. That error happens when the variable you're using as dictionary is an optional, so declared as:
var glyphDict:[NSGlyph:CGPath]?
To solve the issue, you can read from the dictionary using optional chaining:
var path = glyphDict?[glyph]
Note that path is an optional itself.

Odd xcode error when calling defined func in Swift

I have the following function defined in a .swift file in a new project I'm creating:
func createCircle(xPos: Int, yPos: Int) {
// do code here
}
For some reason when I try to call it using the following code xcode displays an error stating "Missing argument label 'yPos:' in call".
createCircle(100, 100)
The odd thing is it treats yPos different than xPos - if I include xPos: in the calling function it highlights the line and says "Cannot convert the expression's type '$Tf' to type 'IntegerLiteralConvertable'". So the following is what I end up with in order to call the aforementioned function:
createCircle(100, yPos: 100)
Am I missing something obvious or is this an xcode beta bug?
According to the Swift documentation
Section - Methods - Local and External Parameter Names for Methods
"Specifically, Swift gives the first parameter name in a method a
local parameter name by default, and gives the second and subsequent
parameter names both local and external parameter names by default."
Judging from your error, it seems like you're using a method and not a function. You don't need to provide the name for the first parameter but you must for subsequent parameters but this behaviour can be changed with the _ (underscore character).
class MyCircle {
var xPos: Double = 0.0;
var yPos: Double = 0.0;
func createCircle(x: Double, _ y: Double) {
// insert create circle logic here.
}
}
var cir = MyCircle();
cir.createCircle(100, 100);
By using the underscore for subsequent parameters you don't have to provide the label for them when calling the method.

Resources