Вот мой код Swift 2, слегка локализованный с точки зрения Великобритании.
Будет форматироваться:
+ 11234567890 как +1 (123) 456 7890
+ 33123456789 как +33 1 23 45 67 89
+ 441234123456 как +44 1234 123456 (это было дополнительно локализовано как 01234 123456), потому что мне не нужно видеть код страны для номеров в Великобритании.
Звоните следующим образом:
initInternationalPhoneFormats() //this just needs to be done once
var formattedNo = formatInternationalPhoneNo("+11234567890")
Если у вас есть другие коды стран, форматы или улучшения кода, пожалуйста, сообщите мне.
Наслаждайтесь.
import Cocoa
extension String
{
//extension from http://stackoverflow.com/questions/24092884/get-nth-character-of-a-string-in-swift-programming-language
subscript (i: Int) -> Character
{
return self[self.startIndex.advancedBy(i)]
}
}
var phoneNoFormat = [String : String]()
var localCountryCode: String? = "+44"
func initInternationalPhoneFormats()
{
if phoneNoFormat.count == 0
{
phoneNoFormat["0"] = "+44 #### ######" //local no (UK)
phoneNoFormat["02"] = "+44 ## #### #####" //local no (UK) London
phoneNoFormat["+1"] = "+# (###) ###-####" //US and Canada
phoneNoFormat["+234"] = "+## # ### ####" //Nigeria
phoneNoFormat["+2348"] = "+## ### ### ####" //Nigeria Mobile
phoneNoFormat["+31"] = "+## ### ## ## ##" //Netherlands
phoneNoFormat["+316"] = "+## # ## ## ## ##" //Netherlands Mobile
phoneNoFormat["+33"] = "+## # ## ## ## ##" //France
phoneNoFormat["+39"] = "+## ## ########" //Italy
phoneNoFormat["+392"] = "+## #### #####" //Italy
phoneNoFormat["+393"] = "+## ### #######" //Italy
phoneNoFormat["+44"] = "+## #### ######" //United Kingdom
phoneNoFormat["+442"] = "+## ## #### #####" //United Kingdom London
phoneNoFormat["+51"] = "+## # ### ####" //Peru
phoneNoFormat["+519"] = "+## ### ### ###" //Peru Mobile
phoneNoFormat["+54"] = "+## ### ### ####" //Argentina
phoneNoFormat["+541"] = "+## ## #### ####" //Argentina
phoneNoFormat["+549"] = "+## # ### ### ####" //Argentina
phoneNoFormat["+55"] = "+## (##) ####-####" //Brazil
phoneNoFormat["+551"] = "+## (##) ####-###" //Brazil Mobile?
phoneNoFormat["+60"] = "+## # #### ####" //Malaysia
phoneNoFormat["+6012"] = "+## ## ### ####" //Malaysia Mobile
phoneNoFormat["+607"] = "+## # ### ####" //Malaysia?
phoneNoFormat["+61"] = "+## # #### ####" //Australia
phoneNoFormat["+614"] = "+## ### ### ###" //Australia Mobile
phoneNoFormat["+62"] = "+## ## #######" //Indonesia
phoneNoFormat["+628"] = "+## ### ######" //Indonesia Mobile
phoneNoFormat["+65"] = "+## #### ####" //Singapore
phoneNoFormat["+90"] = "+## (###) ### ## ##" //Turkey
}
}
func getDiallingCode(phoneNo: String) -> String
{
var countryCode = phoneNo
while countryCode.characters.count > 0 && phoneNoFormat[countryCode] == nil
{
countryCode = String(countryCode.characters.dropLast())
}
if countryCode == "0"
{
return localCountryCode!
}
return countryCode
}
func formatInternationalPhoneNo(fullPhoneNo: String, localisePhoneNo: Bool = true) -> String
{
if fullPhoneNo == ""
{
return ""
}
initInternationalPhoneFormats()
let diallingCode = getDiallingCode(fullPhoneNo)
let localPhoneNo = fullPhoneNo.stringByReplacingOccurrencesOfString(diallingCode, withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
var filteredPhoneNo = (localPhoneNo.characters.filter{["0","1","2","3","4","5","6","7","8","9"].contains($0)})
if filteredPhoneNo[0] == "0"
{
filteredPhoneNo.removeFirst()
}
let phoneNo:String = diallingCode + String(filteredPhoneNo)
if let format = phoneNoFormat[diallingCode]
{
let formatLength = format.characters.count
var formattedPhoneNo = [Character]()
var formatPos = 0
for char in phoneNo.characters
{
while formatPos < formatLength && format[formatPos] != "#" && format[formatPos] != "+"
{
formattedPhoneNo.append(format[formatPos])
formatPos++
}
if formatPos < formatLength
{
formattedPhoneNo.append(char)
formatPos++
}
else
{
break
}
}
if localisePhoneNo,
let localCode = localCountryCode
{
return String(formattedPhoneNo).stringByReplacingOccurrencesOfString(localCode + " ", withString: "0", options: NSStringCompareOptions.LiteralSearch, range: nil) //US users need to remove the extra 0
}
return String(formattedPhoneNo)
}
return String(filteredPhoneNo)
}