Credits:
http://stackoverflow.com/questions/24092884/get-nth-character-of-a-string-in-swift-programming-language

This may be handy for a number of situations - but this code is specifically created for Australian phone numbers. It does the task of adding spaces in between the numbers as people type without interfering with basic functions such as moving the cursor and changing the text.

First create a nifty extension for the String type. The subscript gets the character at an index and the function gets a String.Index type from an int!

extension String {  
subscript (i: Int) -> String {

    if countElements(self) > i {

        return String(Array(self)[i])
    }

    return ""
}

func indexAt(theInt:Int)->String.Index {

    return advance(self.startIndex, theInt)
    }
}

And this is the function to call on the text in your phone number entry field:

func returnMaskedPhoneField(thePhoneText:String)->String{

    var returnString = thePhoneText

    //Trims non-numerical characters
    returnString = returnString.stringByTrimmingCharactersInSet(NSCharacterSet.decimalDigitCharacterSet().invertedSet)
    //Removes all spaces
    returnString = returnString.stringByReplacingOccurrencesOfString(" ", withString: "")

    //Checks if we need to format a mobile number
    if thePhoneText[1] == "4"{

        if countElements(returnString) > 7 {

            returnString = returnString.stringByReplacingCharactersInRange(Range<String.Index>(start: returnString.indexAt(7), end: returnString.indexAt(7)), withString: " ")


        }

        if countElements(returnString) > 4 {

            returnString = returnString.stringByReplacingCharactersInRange(Range<String.Index>(start: returnString.indexAt(4), end: returnString.indexAt(4)), withString: " ")
        }

    }else {

        if countElements(returnString) > 6 {

            returnString = returnString.stringByReplacingCharactersInRange(Range<String.Index>(start: returnString.indexAt(6), end: returnString.indexAt(6)), withString: " ")


        }

        if countElements(returnString) > 2 {

            returnString = returnString.stringByReplacingCharactersInRange(Range<String.Index>(start: returnString.indexAt(2), end: returnString.indexAt(2)), withString: " ")
        }

    }

    return returnString
}

Then then here is where you would implement the function, put this in your viewDidLoad method:

aTextField.delegate = self  
aTextField.addTarget(self, action: "validateTextFields:", forControlEvents: UIControlEvents.EditingChanged)  

And this one somewhere in the class that's your textfield delegate:

func validateTextFields(sender:AnyObject){

    if let textField = sender as? UITextField {

        if textField == myTelephoneField {

            if let currentCurserPosition = myTelephoneField?.selectedTextRange {

                var isEndOfString = false

                let currentCurserPositionInteger = textField.offsetFromPosition(textField.beginningOfDocument, toPosition: currentCurserPosition.start)

                if currentCurserPositionInteger == countElements(textField.text){

                    isEndOfString = true
                }

                myTelephoneField?.text = returnMaskedPhoneField(textField.text)

                if isEndOfString == false {
                    myTelephoneField?.selectedTextRange = currentCurserPosition
                }

            }else {

                myTelephoneField?.text = returnMaskedPhoneField(textField.text)
            }
        }
    }
}