Using lowercaseString in Swift with the "İ" character - ios

In my app, I use lowercaseString. When I change "İ" to lowercase, the output is incorrect. It puts more points on the smaller letters:
let isim = "titTTSSİŞÇÖÜİİİle".lowercaseString
The output is:
titttssi̇şçöüi̇i̇i̇le
The correct output for İ should be i

It looks like this is Turkish text. You can use lowercaseStringWithLocale to handle this correctly:
4> let isim = "titTTSSİŞÇÖÜİİİle".lowercaseStringWithLocale(NSLocale(localeIdentifier: "tr"))
isim: String = "titttssişçöüiiile"
You may wish to use NSLocale.currentLocale instead, to accommodate users in different locales based on their preferences.

You may have to change that character separately. Perhaps something like this:
var isim: NSString = "titTTSSİŞÇÖÜİİİle".lowercaseString
isim = isim.stringByReplacingOccurrencesOfString("İ", withString: "i")
Hope this helps.

Related

substring of the first 4 characters from a textField in Swift 4

I'm trying to create a substring of the first 4 characters entered in a textField in Swift 4 on my iOS app.
Since the change to Swift 4 I'm struggling with basic String parsing.
So based on Apple documentation I'm assuming I need to use the substring.index function and I understand the second parameter (offsetBy) is the number of characters to create a substring with. I'm just unsure how I tell Swift to start at the beginning of the string.
This is the code so far:
let postcode = textFieldPostcode.text
let newPostcode = postcode?.index(STARTATTHEBEGININGOFTHESTRING, offsetBy: 4)
I hope my explanation makes sense, happy to answer any questions on this.
Thanks,
In Swift 4 you can use
let string = "Hello World"
let first4 = string.prefix(4) // Hell
The type of the result is a new type Substring which behaves very similar to String. However if first4 is supposed to leave the current scope – for example as a return value of a function – it's recommended to create a String explicitly:
let first4 = String(string.prefix(4)) // Hell
See also SE 0163 String Revision 1
In Swift 4:
let postcode = textFieldPostcode.text!
let index = postcode.index(postcode.startIndex, offsetBy: 4)
let newPostCode = String(postcode[..<index])

Get string of all possible characters given iOS keyboard language

I'm trying to verify that every character in a text box is limited to the language's keyboard options. This means that in English, you would only be able to type characters which are accessible through the iOS keyboard. If there is a string containing all characters, or a code solution - either will work.
Thanks for the help.
Try something like this. Let me know if it helped :) Good luck!
let letters = CharacterSet.letters
let text = textField.text
let range = text.rangeOfCharacters(from: letters)
if range != nil {
// Yay it's correct
}else{
// Oh no
}
The CharacterSet documentation has multiple options for the characters you want to restrict the field to. You can even make your own character sets! Check out the link: https://developer.apple.com/documentation/foundation/characterset
You could check it like that (is Swift):
let charactersSet = CharacterSet(charactersIn: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
if yourString.rangeOfCharacter(from: charactersSet.inverted) != nil {
print("string contains special characters")
}
You can try some thing like:
NSString *inputString = #"abc0129yourcontent";
BOOL success = [inputString isMatchedByRegex:#"^[0-9a-zA-Z]+$"];
You can convert in Swift if required.

Replace Characters in a Range in Swift

I have a chunk of code that looks like this:
HiClass *myNewClass;
Now, what I'm doing is writing a method to roll through and delete the Hi, as well as everything after it, including the *, so that only myNewClass; is left. Now, I take out the "Hi" like so:
textToConvert = inputField.stringValue.stringByReplacingOccurrencesOfString("Hi", withString: "",
options: NSStringCompareOptions.LiteralSearch, range: nil)
But within this same method, if possible, I would like to somehow delete everything leaving only myNewClass;, as stated above.
My first though on how to approach this is to use a range. Though still being new to Swift and trying to avoid Objective-C, I'm unsure of how to remove all of the characters between the Hi and the *, leaving onlymyClass.
you can use this method
var testStr = "HiClass *myNewClass"
let array = testStr.componentsSeparatedByString("*")
testStr = String(array.last)
You can use indexOf with Swift 2.0 to find the index, and just take a substring to the end of it like this:
var str = "HiClass *myNewClass"
if let idx = str.characters.indexOf("*") {
var s = str.substringFromIndex(advance(idx, 1))
}

String characters (marks) are not counted correctlly

Characters as (é) or in arabic (دٌ) are counted as one in a string, how do I make it recognise the mark as a character?
It should be like (د) is a character and (ٌ) is another character.
I don't want to use NSString because I'm using (startIndex) which is not supported in NSString as far as I know.
Thank you
I’m by no means sufficiently knowledgeable in this area to be confident there aren’t some gotchas from this approach, but this appears to do what you’re looking for:
let s = "éدٌ"
let separated = map(s.unicodeScalars) { Character($0) }
println(" , ".join(separated.map(toString)))
// prints "e , ́ , د , ٌ"
Note, if you create a new string from a sequence of those separated characters, it will recompose them:
println(String(separated)) // prints
// prints "éدٌ"

How to list (almost) all emojis in Swift for iOS 8 without using any form of lookup tables?

I'm playing around with emojis in Swift using Xcode playground for some simple iOS8 apps. For this, I want to create something similar to a unicode/emoji map/description.
In order to do this, I need to have a loop that would allow me to print out a list of emojis. I was thinking of something along these lines
for i in 0x1F601 - 0x1F64F {
var hex = String(format:"%2X", i)
println("\u{\(hex)}") //Is there another way to create UTF8 string corresponding to emoji
}
But the println() throws an error
Expected '}'in \u{...} escape sequence.
Is there a simple way to do this which I am missing?
I understand that not all entries will correspond to an emoji. Also, I'm able create a lookup table with reference from http://apps.timwhitlock.info/emoji/tables/unicode, but I would like a lazy/easy method of achieving the same.
You can loop over those hex values with a Range: 0x1F601...0x1F64F and then create the Strings using a UnicodeScalar:
for i in 0x1F601...0x1F64F {
guard let scalar = UnicodeScalar(i) else { continue }
let c = String(scalar)
print(c)
}
Outputs:
😁😂😃😄😅😆😇😈😉😊😋😌😍😎😏😐😑😒😓😔😕😖😗😘😙😚😛😜😝😞😟😠😡😢😣😤😥😦😧😨😩😪😫😬😭😮😯😰😱😲😳😴😵😶😷😸😹😺😻😼😽😾😿🙀🙁🙂🙃🙄🙅🙆🙇🙈🙉🙊🙋🙌🙍🙎🙏
If you want all the emoji, just add another loop over an array of ranges:
// NOTE: These ranges are still just a subset of all the emoji characters;
// they seem to be all over the place...
let emojiRanges = [
0x1F601...0x1F64F,
0x2702...0x27B0,
0x1F680...0x1F6C0,
0x1F170...0x1F251
]
for range in emojiRanges {
for i in range {
guard let scalar = UnicodeScalar(i) else { continue }
let c = String(scalar)
print(c)
}
}
For those asking, the full list of available emojis can be found here: https://www.unicode.org/emoji/charts/full-emoji-list.html
A parsable list of unicode sequences for all emojis can be found in the emoji-sequences.txt file under the directory for the version you're interested in here: http://unicode.org/Public/emoji/
As of 9/15/2021 the latest version of the emoji standard available on Apple devices is 13.1.

Resources