Parse and Validate a Name in Swift
If you allow users to submit contact information via online form submissions, there is a margin of error that comes along with the convenience. Invalid name values can throw a huge wrench in your customer transactions, causing order mix-ups, botched communications, and more. In an attempt to avoid these situations, I will be demonstrating how you can use an API in Swift to instantly parse and validate a full name string.
To call the validation function, you simply need to input your request string and API key into the following code:
import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endifvar semaphore = DispatchSemaphore (value: 0)let parameters = "{\n \"FullNameString\": \"<string>\"\n}"
let postData = parameters.data(using: .utf8)var request = URLRequest(url: URL(string: "https://api.cloudmersive.com/validate/name/full-name")!,timeoutInterval: Double.infinity)
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("YOUR-API-KEY-HERE", forHTTPHeaderField: "Apikey")request.httpMethod = "POST"
request.httpBody = postDatalet task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data else {
print(String(describing: error))
semaphore.signal()
return
}
print(String(data: data, encoding: .utf8)!)
semaphore.signal()
}task.resume()
semaphore.wait()
This quick operation will parse the name, return its components, and inform you if it’s valid or invalid. To retrieve your API key, head to the Cloudmersive website to register for a free account; this provides access to 800 monthly calls across any of our APIs.