Last active
January 5, 2016 10:26
-
-
Save lukewakeford/11cccee9fdfe2cb6f09e to your computer and use it in GitHub Desktop.
Swift String Extensions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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