String concate issues in swift 3 [duplicate] - ios

This question already has an answer here:
Swift 3 incorrect string interpolation with implicitly unwrapped Optionals
(1 answer)
Closed 6 years ago.
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

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!

Related

Capitalize each first letter of the words in a string [duplicate]

This question already has answers here:
How to capitalize each word in a string using Swift iOS
(8 answers)
Closed 5 years ago.
How can i capitalized each first letter of the result of this
self.namE.text = currentUser.displayName
self.handle.text = snapshotValue?["handle"] as? String
instead of "Bruce willis" i would like to have "Bruce Willis", i created this extension to capitalized the first letter
extension String {
func capitalizingFirstLetter() -> String {
return prefix(1).uppercased() + dropFirst()
}
}
but it obviously capitalize only the first word in a string, so how i have to modify this extension to get the right result? ( i looked in the doc but i didn't understand very well )
String in swift4 has already a capitalized computed property on itself, so without implementing anything yourself, you can get the desired result using this:
self.namE.text = currentUser.displayName.capitalized
E.g.:
self.namE.text = "bruce willis".capitalized

Swift 2 query printing with Optional wording [duplicate]

This question already has answers here:
swift How to remove optional String Character
(14 answers)
Closed 6 years ago.
let username = self.user?.getProperty("username") as? String
self.navigationItem.title = "#\(username)"
What I want to happen there is for it to print on the screen that users username with an # in front of it like #user2
What it is printing instead is #Optional("user2")
How do I make this stop that? Ha
String Interpolation prints also literal Optional(...) if the value is an optional.
To avoid that use either optional binding
if let username = self.user?.getProperty("username") as? String {
self.navigationItem.title = "#\(username)"
}
Or the ternary conditional operator
let username = self.user?.getProperty("username") as? String
self.navigationItem.title = username != nil ? "#\(username!)" : ""
In the first example the title won't be updated if username is nil, in the second it's updated with an empty string.

Swift 1.2 Results to '?' must be followed by a call, member lookup, or subscript [duplicate]

This question already has answers here:
swift ? must be followed by a call, member lookup, or subscript
(3 answers)
Closed 7 years ago.
Updating to Swift 1.2 / Xcode 6.3 caused the following errors:
Could someone with understanding of changes that took place between 1.1 and 1.2 help out what's going on here? All help appreciated! Thanks for reading this far!
In Swift 1.2, it is illegal to follow a variable with ? to unwrap it. The ? is used in an optional chain and must then be followed by a method call, a member lookup (i.e. a property), or a subscript as the error message said.
In the comments you added:
If I remove the "?" the code complies ok but what happens when a node
doesn't have a name?
It is perfectly valid to compare a String? to a literal String value without unwrapping the variable first. If the optional is nil, then nil is not equal to any literal String so the if will simply fail.
Try the following in a Swift Playground:
var str: String? = nil
if str == "hello" {
println("it is hello")
} else {
println("not hello") // prints "not hello"
}
// Here we reassign str, but it is still a String?
str = "hello"
if str == "hello" {
println("it is hello") // prints "it is hello"
} else {
println("not hello")
}
So it is completely safe just just compare paintedNode.name to "paintedArea" and if the node doesn't have a name then paintedNode.name will be nil and the if will fail just as if it had a different name.

concatenate enum of strings [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have 2 enums below.
CONFIG_URLS.BASE_URL
CONFIG_URLS.URL1
Each of these enums points to a string.
I want to create a string variable by concatenating BASE_URL and URL1.
Should be swift code.
Can anybody help? Many thanks
I presume that your enum has a raw value of type string. In that case, I would recommend creating a static function that given a enum case returns an absolute URL obtained by appending the value of a case representing a path to the value of the base url:
enum CONFIG_URLS : String {
case BASE_URL = "http://www.myurl.com"
case URL1 = "/some/path"
static func getUrl(url: CONFIG_URLS) -> String {
switch url {
case .BASE_URL:
return BASE_URL.rawValue
default:
return "\(CONFIG_URLS.BASE_URL.rawValue)\(url.rawValue)"
}
}
}
println(CONFIG_URLS.getUrl(.BASE_URL))
println(CONFIG_URLS.getUrl(.URL1))
Alternatively, the static method can be converted to a property, used in a similar way:
enum CONFIG_URLS : String {
case BASE_URL = "http://www.myurl.com"
case URL1 = "/some/path"
var url: String {
switch self {
case .BASE_URL:
return BASE_URL.rawValue
default:
return "\(CONFIG_URLS.BASE_URL.rawValue)\(self.rawValue)"
}
}
}
println(CONFIG_URLS.BASE_URL.url)
println(CONFIG_URLS.URL1.url)
Swift provides the following options.
Option 1
You can concatenate two constant strings as below:
let str1 = "hi" // constant
let str2 = " how are u" // constant
var str3 = string1 + string2 // here str3 is variable which will hold
// the concatenated value i.e. str3 = "hi how are u"
Option 2
You can also append a String value to an existing String variable with the addition assignment operator (+=):
let str1 = "hi"
var str2 = "how are u"
str2 += str1
// str2 now equals "hi how are u"
THe solution was to use rawValue and use + for concatenation (in Swift) , which I wasnt aware about regarding enums.
Thanks everyone.

Swift : How to put string in \( )? [duplicate]

This question already has answers here:
include dictionary or array value in swift string with backslash notation [duplicate]
(2 answers)
Closed 8 years ago.
I create a dictionary, and I want to use println() to print dictionary content,
but it doesn't work, and I don't know how to solve it..
Please help... Thanks
var dictionary = ["name":"herny","age":20]
println("name = \(dictionary["name"])"); <--- this doesn't work, compile error because dictionary["name"] in \()
You can't put things with quotes in a \( ), you'll need to assign it to a variable:
var dictionary = ["name": "henry", "age": "20"];
let name = dictionary["name"];
println("name = \(name)");

Resources