Getting number from String Swift 2 Issue - ios

I was able to get the phone number from the textfield with the method below in Swift 1.2 but I'm trying the same method in Swift 2 but I'm getting an error from .join "join is unavailable". Please how can I write the same method in Swift 2?
let userNumber = NSNumberFormatter().numberFromString("".join(phoneNumberTextField.text!.componentsSeparatedByCharactersInSet(NSCharacterSet.decimalDigitCharacterSet().invertedSet)))
This answer didn't help me that I can't get join at all.
Cannot invoke `join` with an argument list of type (String, [String]) in Swift 2.0

Try this:
let numbers = phoneNumberTextField.text!.componentsSeparatedByCharactersInSet(NSCharacterSet.decimalDigitCharacterSet().invertedSet)
let userNumber = numbers.joinWithSeparator(" ") // Using space as separator

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])

UnsafePointer no longer works in swift 3

After I convert from swift 2 to swift 3, there is an error pop up for the below metioned line
let value = UnsafePointer<UInt32>(array1).pointee
'init' is unavailable: use 'withMemoryRebound(to:capacity:_)' to temporarily view memory as another layout-compatible type.
in swift2 it is like
let value = UnsafePointer<UInt32>(array1).memory
Can someone explain please?
Sorry I'm quite new to swift3
After i have make the changes to
let abc = UnsafePointer<UInt32>(array1).withMemoryRebound(to: <#T##T.Type#>, capacity: <#T##Int#>, <#T##body: (UnsafeMutablePointer<T>) throws -> Result##(UnsafeMutablePointer<T>) throws -> Result#>)
but still what value should go in to the variable? Sorry, i have search around but too bad i can't find a solution
You can try this:
let rawPointer = UnsafeRawPointer(array1)
let pointer = rawPointer.assumingMemoryBound(to: UInt32.self)
let value = pointer.pointee
Raw pointer is a pointer for accessing untype data.
assumingMemoryBound(to:) can convert from an UnsafeRawPointer to UnsafePointer<T>.
Reference :Swift 3.0 Unsafe World
If array is an Array, your best bet is to use withUnsafeBufferPointer:
array.withUnsafeBufferPointer { buffer in
// do something with 'buffer'
// (if you need an UnsafePointer rather than an UnsafeBufferPointer,
// you can access that via the buffer's .baseAddress property)
}
Make sure you don't let the buffer pointer escape from the closure, because it will not be valid outside it.

Creating closed Range in Swift 3 not working

Can anyone tell me why the code below works in Swift 2, but somehow breaks in Swift 3?
let range: Range = 0...2
However it can simply be fixed by doing this
let range: Range = 0..<3
Anyone knows what is the reason behind this?
Operators ... and ..< used to produce the same type, Range, in Swift 2.x. Now they produce different types (migration guide):
Range
CountableRange
ClosedRange
CountableClosedRange
Changing the type in the first assignment to ClosedRange should fix the problem. Better yet, let Swift infer the type for you:
let range = 0...2

'Binding' is not convertible to UILabel using SQLite in Swift

My first time around here with a Swift related question with the SQLite.Swift library.
I have a for loop for a db.prepare statement, but I am stuck when trying to assign an array value to a UILabel.
// Prepare query to retrieve the message
var _phraseMessage:String = ""
let _stmt = _db.prepare("SELECT id, message FROM messages WHERE language = 'EN' AND category = 1 and username = 'user' LIMIT 1")
for row in _stmt {
println("id: \(row[0]), message: \(row[1])")
self._phraseMessageLabel = row[1] --> i get an error here **"'Binding' is not convertible to UILabel"**
}
How can assign the value in row[1] to my UILabel? Or even better to a String variable if possible.
Thanks in advance!
Disclaimer: This is my first attempt to Swift + Third party library
You're trying to set the UILabel property itself to your value - you want to set the text property on the label to your value:
self._phraseMessageLabel.text = row[1] as! String
If you want to assign the value to a variable:
var message = row[1] as! String
One thing to note is that with both approaches, your label text or variable will only end up being set to the value for the last row returned, the one that is processed last by the for loop.
Beyond Undo's fix above, I wanted to offer a couple other solutions.
Try using Database.scalar or Statement.scalar if you're only using a single value, as in your example above.
// make sure to remove "id" from your SELECT statement
self._phraseMessageLabel.text = _stmt.scalar() as! String
Note: Make sure to remove id from your SELECT statement; scalar returns the first column of the first row only.
Use the Statement.row cursor.
_stmt.step()
self._phraseMessageLabel.text = _stmt.row[1]

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