[Swift] If you want to use a URL that includes Japanese, use addingPercentEncoding.

I stumbled upon an API call without knowing it.

json-> swift type generation used quicktype

(Click here for the story of decoding failure when trying to get data from NewsAPI [Swift] A story that failed in decoding json acquired by News API and crushed it for half a day)

I had to use addingPercentEncoding when I wanted to fetch data from a URL containing Japanese in the API. image.png

So in the case of URL including Japanese, it should have been written as follows

let NewsApiURL="https://newsapi.org/v2/everything?q=\(keyword)&num=3&sortBy=latest&apiKey=\(myApiKey)"
let encodedURL = NewsApiURL.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)

guard let url = NSURL(string: encodedURL!) else {
    print("Invalid URL.")
    return
}

I gave it like this and it worked.

Recommended Posts

[Swift] If you want to use a URL that includes Japanese, use addingPercentEncoding.
If you want to use Mockito with Kotlin, use mockito-kotlin
If you want to make a Java application a Docker image, it is convenient to use jib.
[Swift] When you want to know if the number of characters in a String matches a certain number ...
If you want to mock a method in RSpec, you should use the allow method for mock and the singleton method.
If you want to use Oracle JDK 11 from September (add Amazon Corretto)
If you want to make a zip file with Ruby, it's rubyzip.
What to do when you want to delete a migration file that is "NO FILE"
How to create a route directly from the URL you want to specify + α
If you want to modify database columns etc.
How to make @Transactional work that does not work if you use it incorrectly
Volume that wants to use a lot of logical operators in if statements
When is it said that you can use try with a Swift error?
When you want to use the method outside
[Swift] Use nonzeroBitCount when you want popcnt in Swift
[Spring Boot] If you use Spring Boot, it was convenient to use a lot of util.
[Swift5] extension that allows UIImage to be specified by a String type URL
I want to use PowerMock in a class that combines parameterized tests and ordinary tests
7 things I want you to keep so that it doesn't become a fucking code
If you want to separate Spring Boot + Thymeleaf processing
I want to use a little icon in Rails
What to do if you accidentally create a model
If you want to recreate the instance in cloud9
A memo to check when you try to use Lombok
[Swift5] What to do if you want to commit files to github but there are too many
What to do if you get a java.io.IOException in GlassFish
If you want to study programming at university, go to Australia
Java study site summary that you want to read carefully
[# 3 Java] Read this if you want to study Java! ~ Carefully selected ~
I want to use FireBase to display a timeline like Twitter
Delegate is convenient to use when you want to reuse parts
What to do if you can't use the rails command
[Enum_help] Use enum_help to create a select box displayed in Japanese!
A collection of patterns that you want to be aware of so as not to complicate the code
Object-oriented design that can be used when you want to return a response in form format
[Swift] How to use UserDefaults
How to use Swift UIScrollView
I want to create a chat screen for the Swift chat app!
What to do if you get a gcc error in Docker
[PostgreSQL] If you want to delete the Rails app, delete the database first!
If you want to change the Java development environment from Eclipse
What to do if you get a DISPLAY error in gym.render ()
Practice to use when you want to execute different processing groups serially
When you receive a call, send an SMS to that number
A story that may make you happy if you create a Sorry Page image to put kubernetes into production