I simply want to get the first name and first initial of the last name from a name string ie if name is "John Paul Apple" I want the abbvName to be JohnA.
Is there a more elegant way to do this than the following code ... I tried with reduce but couldn't get it to work
// get first name & last name initial
let name = "John Paul Apple"
let nameComponents = name.components(separatedBy: " ")
guard let firstname = nameComponents.first, !firstname.isEmpty else { return }
guard let lastname = nameComponents.last, !lastname.isEmpty else { return }
let startIx = lastname.startIndex
let lastinit = String(lastname.first!)
let abbvName = firstname + lastinit
I tried reduce ie but can only get it giving me initials as per this post
let initials = "John Fitzgerald Kennedy".components(separatedBy: " ").reduce("") { ($0 == "" ? "" : "\($0.first!)") + "\($1.first!)" }