I am creating username from first and last name. It was working fine with swift 2.2 but after migrating to swift 3 now the string do get concate but when it does the first name is have optional with it. Check below images
Asked
Active
Viewed 92 times
-1

Parth Adroja
- 13,198
- 5
- 37
- 71
-
user firstName! – Wolverine Oct 19 '16 at 09:57
-
`let name = "\(firstName) \(lastName)"!` – Scriptable Oct 19 '16 at 10:05
-
@Parth, Could you please paste the code so we can play with it in ground? :) – Mohammad Zaid Pathan Oct 19 '16 at 11:20
-
@Scriptable : Playground is not accepting your syntax – BaSha Oct 19 '16 at 12:07
1 Answers
2
use like this
let name = "\(firstName!) \(lastName)"
I have tried with basic example too. If string contain optional("") then you can resolve it by this by force the compiler to implicitly force unwrap.
you can check in .playground
let firstname : String!
let lastname : String!
firstname = "Hello" ==> "Hello"
lastname = "World" ==> "World"
let fullname = "\(firstname) \(lastname)" ==> "Optional("Hello") Optional("World")"
let fullname = "\(firstname!) \(lastname!)" ==> "Hello World"
Give it a try using above solution, Hope it Helps!

Wolverine
- 4,264
- 1
- 27
- 49
-
2
-
Yes that's correct it gets fixed by it but even if i don't add the type remains String!. – Parth Adroja Oct 19 '16 at 10:01
-
@BaSha If firstname is already implicit, then it will show the correct value right ? yet if it is not, then you have to force the compiler to implicitly force unwrap it for you. – Wolverine Oct 19 '16 at 10:11
-