country picker swift 4 [duplicate] - ios

This question already has answers here:
Getting country name from country code
(8 answers)
Closed 4 years ago.
I would like implement a native iOS country picker in swift 4.
Currently I have a table which contain the countries code:
var countriesCodes = NSLocale.isoCountryCodes as [String]
I implement the delegate, and I have a problem in the data source.
How to convert each country code in country name?

You can convert each country code key into its localised display name using the following function provided in Foundation (documentation here):
Locale.current.localizedString(forRegionCode:)
// "NZ" -> "New Zealand"
You can convert your country code array to display names using flatMap:
let countryNames = countriesCodes.flatMap(
Locale.current.localizedString(forRegionCode:))
Although you may wish to make a tuple pair of the ISO country code and its localised display name together, which would be much more useful:
let countryCodesAndNames = countriesCodes.flatMap { code in
Locale.current.localizedString(forRegionCode: code).map { (code, $0) }
}
// countryCodesAndNames is type [(String, String)]
// which is (ISO code, displayName)
// eg.
print(countryCodesAndNames[0])
// prints ("AC", "Ascension Island")

Related

Phone number format from country code on iOS

I need to display phone number format as placeholder in UITextField. How can I do that?
For country selection I'm using below mentioned library and it provides me country flag and country code against user selected country.
https://github.com/NikKovIos/NKVPhonePicker
After selecting a country I need to display phone number format for that selected country and on submission of that phone number I have to validate the phone number.
I also find that third party (PhoneNumberKit) which is inspired by google's libphonenumber but it is for validating, it do not provide expected phone number format against country code. Below is the link.
https://github.com/marmelroy/PhoneNumberKit
Update 1:
Tried this and getting Generic parser error
let phoneNumberKit = PhoneNumberKit()
do {
let phoneNumber = try phoneNumberKit.parse("+921230123456")
}
catch {
print("Generic parser error")
}
Update 2:
Updated code, still getting exception
let phoneNumberKit = PhoneNumberKit()
do {
let phoneNumber = try phoneNumberKit.parse("1230123456", withRegion: "FR", ignoreType: false)
let formatedNumber = phoneNumberKit.format(phoneNumber, toType: .international)
print(formatedNumber)
}
catch {
print("Generic parser error")
}
I don't know whether this is a valid solution or not, you could try this
Say let your placeholder be 012345679 what I believe you could do is
Create a variable to store this placeholder
Parse this placeholder against the country that the user selects.
Set the parsed one as the placeholder in the textfield.
For those who wanna do the same thing, I used two different 3rd parties to achieve the functionality.
NKVPhoneNumber
SHSPhoneComponent
NKVPhoneNumber is used to select country code, i've modified it a bit a introduced phone_format in the Meta Data. Once selected a country from the list it return a Country object which includes Code, Extension, Flag and format_placeholder
SHSPhoneComponent then use that format_placeholder for validation of the format.
import SHSPhoneComponent
import NKVPhonePicker
#IBOutlet weak var phoneTF: SHSPhoneTextField!
#IBOutlet weak var phoneFlag: NKVPhonePickerTextField!
#IBOutlet weak var lblCountryCode: UILabel!
//MARK: - NKV callback delegates
func countriesViewController(_ sender: CountriesViewController, didSelectCountry country: Country) {
//
phoneTF.formatter.setDefaultOutputPattern(country.formatPattern)
phoneTF.text = ""
phoneTF.placeholder = country.formatPatternPlaceHolder
countryCode = "+\(country.phoneExtension)"
lblCountryCode.text = countryCode
}
Note: I've converted NVKPhoneNumber to Swift 4.0,

Cannot assign value of type 'String' to type 'UILabel!' [duplicate]

This question already has answers here:
Cannot assign a value of type "String" to type "UILabel" in swift
(2 answers)
Closed 5 years ago.
I'm getting this error in XCode with Swift. I've tried a lot of other solutions I've seen on this site, but none seem to work.
#IBAction func generateClick(_ sender: Any) {
let int1 = Int(number1.text!)!
let int2 = Int(number2.text!)!
let random = arc4random_uniform(UInt32((int2 - int1) + int1))
numberText = "\(random)"
The goal is the pick a random number when they click the generate button. (Between the 2 numbers they specify, being number1 and number2)
I suppose numberText is you UILabel reference.
You want assign to it's text Attribute like this:
numberText.text = "\(random)"

ios - filter two arrays with objects Swift [duplicate]

This question already has answers here:
How to remove common items from two struct arrays in Swift
(3 answers)
Closed 6 years ago.
I have got two arrays with objects.
var filteredData:[MainData] = [MainData]()
var removeData:[MainData] = [MainData]()
struct MainData {
var open:NSTimeInterval
var works = [Visit]()
}
I want remove data from filteredData using function filter with parameter filteredData.open == removeData.open
I can't filter two arrays with objects.
You can try like this, first get an Array of open from removeData array and check that it is contains object from the filteredData Array opens.
let opens = removeData.map { $0.open }
filteredData = filteredData.filter { !opens.contains($0.open) }

NSMutableArray to Array of String Conversion Swift iOS [duplicate]

This question already has answers here:
How can I cast an NSMutableArray to a Swift array of a specific type?
(7 answers)
Closed 7 years ago.
I need to populate data in a table view. How can I convert [NSMutableArray?] to [(String)] in Swift iOS
println(self.subcategory[index]!) i.e., an [NSMutableArray?], where
index is Int shows:
(
Operations,
Marketing,
"Public Relations",
"Human Resources",
Advertising,
Finance,
Hotels,
Restaurants,
Other
)
Try with this.
let myArray : NSMutableArray = ["Operations"]
myArray.addObject("Marketing")
myArray.addObject("Public Relations")
myArray.addObject("Human Resources")
myArray.addObject("Advertising")
myArray.addObject("Finance")
myArray.addObject("Hotels")
myArray.addObject("Restaurants")
myArray.addObject("Other")
NSLog("\(myArray)")
var string = myArray.componentsJoinedByString(",")
NSLog("\(string)")

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