Styling Google Maps in swift 2 - swift

I'm facing a little problem I hope there is a simple solution.
Indeed I would like to custom my google map but I only found how to achieve this in Swift 3 (like the following instruction) but I'm currently developping in Swift 2.
do {
// Set the map style by passing the URL of the local file.
if let styleURL = Bundle.main.url(forResource: "style", withExtension: "json") {
mapView.mapStyle = try GMSMapStyle(contentsOfFileURL: styleURL)
} else {
NSLog("Unable to find style.json")
}
} catch {
NSLog("One or more of the map styles failed to load. \(error)")
}
Does somebody has an idea about how solve my problem in Swift 2 ?
Thank you for your answers !

Related

Extract PDF Highlights with PDFKit

I'm trying to extract all Highlights using PDFkit in a macOS app. Here's the code I'm using:
guard let path = item.filePath else { return }
let document = PDFDocument(url: URL(fileURLWithPath: path))
guard let numberOfPage = document?.pageCount else { return }
for i in 0...numberOfPage - 1 {
let pages = document?.page(at: i)
guard let annotations = pages?.annotations else { continue }
for annotation in annotations {
if annotation.type == "Highlight" {
print(annotation.contents)
self.annotations.append(annotation)
}
}
}
The problem is that print(annotation.contents) always return "Optional("")". I tried several pdf, and the result is always the same. The thing is that if I do print(annotation.color), it return the correct color of the given highlights.
Is there something wrong with my code that I didn't figure out? Or is this a normal behavior of PDFKit?
Use PDFAnnotationSubtype.highlight.rawValue to get the key for the highlights. If you print that value, you will see that it's /Highlight. Even though we know the key now, you should still use the enum value, in case anything ever changes in PDFKit.
So in your case...
if annotation.type == PDFAnnotationSubtype.highlight.rawValue {
If that confuses you, familiarize yourself with Enums and Raw Values.

Swift CSVImporter framework with remote URLs

Initial use and testing of the framework. The examples provided and most of the searches from across the internet are using "local" or downloaded CSV files to the device, with (path:).
I would like to pass various remote URLs but there are not many examples, using (url: URL).
So far, I am simply in viewDidLoad() following the same code as provided with the sample playground file, and trying to output to the console.
I have tried to run this in a simulator for the iPhone 8 device. Running Xcode 10.1.
From the documentation, there is an ".onFail" handler, which gets invoked on the sourceURL's I have provided, but I do not know what error objects exist to do any further troubleshooting.
let sourceURL = URL(string: "https://files.datapress.com/leeds/dataset/leeds-city-council-dataset-register/Dataset%20register.csv")
guard let sourceURL2 = URL(string: "https://minio.l3.ckan.io/ckan/ni/resources/2477b63a-b1c4-45cc-a5ee-8e33e5b20b5b/supplies-and-services-contracts---2014.2015-yr.csv?AWSAccessKeyId=aspjTDZu90BQVi&Expires=1546982840&Signature=dLDVWMu%2Fp4RiePIRhntCX6WFMpw%3D") else {
fatalError("URL string error")
}
let importer = CSVImporter<[String]>(url: sourceURL)
importer?.startImportingRecords { $0 }.onFail {
print("fail")
}.onFinish({ importedRecords in
print(importedRecords.count)
})

Swift Vapor: How to read a JSON file?

I'm using Vapor, the server-side Swift 4 framework for creating web servers. I have a migration that I'd like to apply that reads in from a JSON file; however, most if not all Swift tutorials I see on this indicate using Bundle, which to my knowledge, isn't available when developing server-side swift applications, which means attempts like this don't work:
if let path = Bundle.main.url(forResource: "myFile", withExtension: "json") {
...
This begs the question, given a relative file path, how can I read in a file using server-side Swift?
Silly me. Vapor exposes a DirectoryConfig object from which you can retrieve the working directory of the application. To parse a file, it thus becomes:
let directory = DirectoryConfig.detect()
let configDir = "Sources/App/Configuration"
do {
let data = try Data(contentsOf: URL(fileURLWithPath: directory.workDir)
.appendingPathComponent(configDir, isDirectory: true)
.appendingPathComponent("myFile.json", isDirectory: false))
// continue processing
} catch {
print(error)
}

how to open an app from Mac from Xcode project (Cocoa App)

Today I have created an Cocoa Application for the very first time . I want to create a simple App which will open an APP from my Mac , if the file not found it will show a Link in a Label to download the App. Here is my code below which I am struggling with .
if let fileCheck = NSURL.fileURL(withPath: "/Applications/Mango.app") {
if NSWorkspace.shared().open(fileCheck as URL) {
print("url successfully opened")
}
} else {
self.downloadLink.insertText("Invalid Path")
}
NSURL.fileURL(withPath: "/Applications/Mango.app") giving me Conditional Binding Must be Optional , I don't know how to fix that. And I am struggling with how to show a link on my Label either. Any kind hearted Dev please help.
got the solution by someone , I don't know why he deleted that !
if FileManager().fileExists(atPath: "/Applications/Mango.app") {
NSWorkspace.shared().open(NSURL(fileURLWithPath: "/Applications/Mango.app") as URL)
} else {
downloadLink.isHidden = false
}

Do - Catch error handling Swift 2.0

I have read through numerous posts regarding conversion between older versions of swift and swift 2.0 on the issue of Do-catch error handling. However each and every one of them seem different to my personal issue.
Besides solving my personal issue I'm fairly curious as to what the general idea is behind this concept, because I simply can not figure out how this works on a low level scale just by reading all these topics.
I'll post my personal issue below, but I'd also very much appreciate some sort of general explanation about how this do-catch method works.
if(urlResponse.statusCode == 200) {
self.tweets = NSJSONSerialization.JSONObjectWithData(responseData,
options: NSJSONReadingOptions.MutableContainers,
error: &jsonParseError) as? NSMutableArray
}
the error shows at the line:
error: &jsonParseError) as? NSMutableArray
Change your code to
if(urlResponse.statusCode == 200) {
do {
self.tweets = try NSJSONSerialization.JSONObjectWithData(responseData, options: NSJSONReadingOptions.MutableContainers) as? NSMutableArray
} catch let jsonParseError {
print("Parse error = \(jsonParseError)")
}
}
You can find more about error handling here.