Convert Unicode (e.g. 1f564) to Emoji in Swift [duplicate] - ios

This question already has answers here:
Print unicode character from variable (swift)
(7 answers)
Closed 7 years ago.
How do you convert unicode emoji stored as a string (e.g. 1f564) to an emoji that could be placed on a UILabel?
let myString = "1f564"
I've seen the use of the escape character but I can't insert variables to replace the characters.
let flag = "\u{1f1e9}\u{1f1ea}"
Thanks

This works. I don't know if there is anything more direct:
let myStr = "1f564"
let str = String(UnicodeScalar(Int(myStr, radix: 16)!)!)

Related

Swift 4 - 'substring(to:)' is deprecated - Alternative To My Code [duplicate]

This question already has answers here:
'substring(to:)' is deprecated: Please use String slicing subscript with a 'partial range upto' operator
(2 answers)
Closed 3 years ago.
My code was as below
var theReview = addReview.text
let len2 = addReview.text.utf16.count
if len2 > 3000 {
theReview = theReview?.substring(to: (theReview?.index((theReview?.startIndex)!, offsetBy: 3000))!)
}
My aim was to get the first 3000 characters of the text if it is longer than 3000 characters.
However, I get the warning below:
'substring(to:)' is deprecated: Please use String slicing subscript
with a 'partial range upto' operator
What can be an alternative to my code. I am not a very professional coder. So any help would be great.
Simply call prefix(_:) on theReview with the required length.
func prefix(_ maxLength: Int) -> Substring
Returns a subsequence, up to the specified maximum length, containing
the initial elements of the collection.
If the maximum length exceeds the number of elements in the
collection, the result contains all the elements in the collection
var theReview = addReview.text
theReview = String(theReview.prefix(3000))
Note: There is no need to check if the theReview's length exceeds 3000. It will be handled by prefix(_:) itself.
might this could help you
var theReview = addReview.text
theReview = String(theReview.prefix(3000))
Use String init with slice.
let index = theReview.index(theReview.startIndex, offsetBy: 3000)
theReview = String(theReview[theReview.startIndex..<index])
or prefix as mentioned in previous answers

How can I check if a string already contains a character in Swift [duplicate]

This question already has answers here:
How do I check if a string contains another string in Swift?
(28 answers)
Closed 4 years ago.
I would like to check in my app, if there is already a - character in the textField.
How can I do this?
You can use contains() method
let myString = "test"
if myString.contains("e"){
let index = myString.index(of: "e")
myString.remove(at: index!)
}
p.s you can get myString from textField.text
You can simply:
var yourTextFieldString: String = textField.text
yourTextFieldString.contains("-")
It returns a Bool.

Showing Emoji with string in textfield in swift

I am new to swift development. I need to show the emoji inside the text field and labels.
I also need to send them to server because Application is multi-platform.
On Xcode version 7.2.1+, you can use the below shortcut to show the symbols panels and insert the emoji:
Shortcut: (press the below three keys together)
Ctrl + Command + Space
You can declare a string variable with text and an emoji inside using its unicode number (1F603 is unicode number for an open faced smiley), like so:
let str : String = "Smiley \u{1F603}"
Then with your UITextField/UILabel, set the .text attribute to be the string.
yourTextField.text = str
//or for a UILabel.
yourLabel.text = str
textField.text=#"😀";
for above Emoji --- put the focus at place of emoji in UITextField and go to
Edit > Emoji & Symbols in Xcode 7.3
and select what ever emoji you want.
OR
Set Unicode values directly in code:
var string: String = "I want to visit ā¤ŽāĨā¤‚ā¤Ŧā¤ˆ. 😊"
var character: Character = "🌍"
Use hexadecimal to set values
var string: String = "\u{61}\u{5927}\u{1F34E}\u{3C0}"
var character: Character = "\u{65}\u{301}"

How can I convert String to Double without losing precision in swift [duplicate]

This question already has answers here:
Swift - How to convert String to Double
(30 answers)
Closed 7 years ago.
How do I convert a string to double or float without losing precision
ex.
let stringNumber = "12.00"
What you want to use is NSString(format:) for the visual format of your value.
Swiftstub sample
To always show 2 digits visually use this:
NSString(format: "%.2f", value)

How to split a string (such as "xPM - yPM") into two strings, Swift [duplicate]

This question already has answers here:
Split a String into an array in Swift?
(40 answers)
Closed 7 years ago.
If I have a string such as xAM - yPM is it possible to split the string into 2 strings using the - as the point where the string is split?
You can use the componentsSeparatedByString function:
var splittedArray = yourString.componentsSeparatedByString(" - ")
println(splittedArray[0]) // "xPM"
println(splittedArray[1]) // "yPM"

Resources