Skip to content

Instantly share code, notes, and snippets.

@lukewakeford
Last active January 5, 2016 10:26
Show Gist options
  • Save lukewakeford/11cccee9fdfe2cb6f09e to your computer and use it in GitHub Desktop.
Save lukewakeford/11cccee9fdfe2cb6f09e to your computer and use it in GitHub Desktop.
Swift String Extensions
extension String {
func isEmail() -> Bool {
let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,6}"
let emailTest = NSPredicate(format:"SELF MATCHES %@", emailRegEx)
return emailTest.evaluateWithObject(self)
}
func convertStringToDictionary() -> [String:AnyObject]? {
if let data = self.dataUsingEncoding(NSUTF8StringEncoding) {
do {
let json = try NSJSONSerialization.JSONObjectWithData(data, options: .MutableContainers) as? [String:AnyObject]
return json
} catch {
print("Something went wrong")
}
}
return nil
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment