This question already has answers here:
Generate UUID from name space?
(2 answers)
Closed 5 days ago.
I am having string that i want to convert UUID
my string is - > "43pszizx-0000-0000-0000-000000000000"
i want to convert this into UUID
i have tried this code
let contactUUID = "43pszizx-0000-0000-0000-000000000000"
if let uuid = UUID(uuidString: contactUUID) {
moviesVC.conversationID = uuid
}
but it always returns nil
is there any solution to convert this string to UUID, this string coming from API as a contactId
thanks in advance
As per the doc
Returns nil if the string isn’t a valid UUID representation.
let contactUUID = "43pszizx-0000-0000-0000-000000000000"
//The string representation of a UUID, such as E621E1F8-C36C-495A-93FC-0C247A3E6E5F.
But the contactUUID doesn't seems to be matching with UUID representation so its always returning nil
Related
This question already has answers here:
How to get device make and model on iOS?
(23 answers)
Closed 3 years ago.
I've been searching a while about this and I've found out that this can't be get from UIDevice.current.model, because it returns just iPhone. there were several answers pointing to this code:
if let simulatorModelIdentifier = ProcessInfo().environment["SIMULATOR_MODEL_IDENTIFIER"] {
return simulatorModelIdentifier
}
var sysinfo = utsname()
uname(&sysinfo) // ignore return value
let deviceModel = String(bytes: Data(bytes: &sysinfo.machine, count: Int(_SYS_NAMELEN)), encoding: .ascii)?.trimmingCharacters(in: .controlCharacters)
return deviceModel ?? ""
by the way I'm not sure that this uses public or private api and it seems like private api for me.
Question
is this code using any private api?
Your code does not use any private API.
uname function returns a POSIX structure containing name and information about current kernel. The machine variable contains an hardware identifier. You'll need to convert this identifier to a more friendly name, but that's all.
You can use man uname in your terminal for more information about the function.
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.
I want to parse and download the current EUR - USD exchange rate. I have descided to get the value from the European Central Bank Feed.
I'm using the CheatyXML XMLParser extension.
How can I get the USD value?
With the following code, I get the value: "European Central Bank". My String is an optional on porpuse. Because my app crashed like 1 trillion times during finding the correct code to get the currency rate...
let feedUrl = NSURL(string: "http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml")
let parser: XMLParser! = XMLParser(contentsOfURL: feedUrl!)
let exchangeString: String? = parser["gesmes:Sender"]["gesmes:name"].string // Returns an optional String
print(exchangeString)
How do I get the value of <Cube currency="USD" ?
let blogName: String? = parser["Cube"]["Cube"].string // Returns an optional String
Is not working.
Help is very appreciated.
You need to go one level deeper (there's three "Cube" fields), then get the attributes and finally subscript with the right key, for example:
parser["Cube"]["Cube"]["Cube"].attributes["currency"] as? String // "USD"
parser["Cube"]["Cube"]["Cube"].attributes["rate"] as? String // "1.1287"
I am trying to compare string values. String values are stored in Dictionary.
When I unwrap data like this:
let type:String = basicBlk["type"] as! String
and compare
print (type.lowercaseString == "minion")
result is true
While if I cast like this:
let type:String = String(basicBlk["type"])
and compare
print (type.lowercaseString == "minion")
result is false
I would like to understand what exactly is the difference.
This
let type:String = String(basicBlk["type"])
Converts the optional value into a string that looks like:
Optional("Minion")
And when you convert that string to lower case, it looks like:
optional("minion")
The issue is that you're creating a string representation of an optional value, and it's including the string "Optional(" and string ")" in the resulting value.
Just print type for both of your two examples and you'll see what I mean.
This question already has answers here:
Converting String to Int with Swift
(31 answers)
Closed 8 years ago.
I'm working in a small project and I faced this problem : text field is String by default so I need to take the input and convert it to integer then calculate I was searching for a way to solve the converting problem and I didn't find any working solution so can any one help me please ?
try like this
In Swift 2.0, toInt(),replaced with initializers. (In this case, Int(someString).)
let myString : String = "50"
let x : Int? = Int(myString)
if (x != null) {
// converted String to Int
}
myString.toInt() - convert the string value into int .