Find out if a Swift string contains emoji [duplicate] - ios

This question already has answers here:
Find out if Character in String is emoji?
(17 answers)
Closed 5 years ago.
I am working in an application in which i need to look up emoji characters from the string means a string contains one or more emojis ? As i am new in swift so i am not aware about it.
exa. "Newbie 😀" will returns yes and
"Newbie" will returns no

Yes please use below method to identify emojies
public var checkcontainEmoji: Bool
{
for ucode in unicodeScalars
{
switch ucode.value
{
case 0x3030, 0x00AE, 0x00A9,
0x1D000...0x1F77F,
0x2100...0x27BF,
0xFE00...0xFE0F,
0x1F900...0x1F9FF:
return true
default:
continue
}
}
return false
}
Output : - "HI 😀".checkcontainEmoji --> TRUE

Related

Swift split String into array [duplicate]

This question already has answers here:
Swift Put String to Array from one to all letters
(5 answers)
Closed 2 years ago.
I have a string with no spaces I would like to turn the string into an array.
Let's say the string is "guru1", I would like my array to be ["g", "gu", "gur", "guru", "guru1"]
Thanks in advance.
You can use reduce :
"guru1".reduce([]) { $0 + [($0.last ?? "") + "\($1)"] }

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

What is use of underScore in Double value in swift? [duplicate]

This question already has answers here:
What is type of 123_456_789?
(2 answers)
Closed 5 years ago.
I just recently look into Apple documentation I found code like below
extension Double {
var km: Double { return self * 1_000.0 }
var m: Double { return self }
var cm: Double { return self / 100.0 }
var mm: Double { return self / 1_000.0 }
var ft: Double { return self / 3.28084 }
}
What is the use of "_" in double value? it's working without error why?
Just a way to improve readability. According to Swift documentation:
Numeric literals can contain extra formatting to make them easier to
read. Both integers and floats can be padded with extra zeros and can
contain underscores to help with readability. Neither type of
formatting affects the underlying value of the literal.

How to generate random code mixed of numbers and letters - Swift [duplicate]

This question already has answers here:
Generate random alphanumeric string in Swift
(24 answers)
Closed 6 years ago.
I'm trying to generate random code mixed of numbers (0-9) and letters(A-z) so I can send it to user email to use it when login .
Is there a library or any way to do this ?
This is give random value...
func returnUUIDWithTimeStamp() -> String {
return UUID().uuidString + NSString(format: "%f", Date().timeIntervalSince1970 * 1000).substring(with: NSRange(location: 8, length: 5))
}
Result will contain '-'
C95B920A-3866-4A50-A783-D719FB0B607512830

How can I convert a String to an Int in swift? [duplicate]

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 .

Resources