Last active
August 29, 2015 14:15
-
-
Save cfr/cbde9da91297912821dd to your computer and use it in GitHub Desktop.
Swift NSNotification crash
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
// | |
// AppDelegate.swift | |
// emailRegexTest | |
// | |
// Created by <#Stan Serebryakov#> on 20/02/15. | |
// | |
import UIKit | |
@UIApplicationMain | |
@objc class AppDelegate: UIResponder, UIApplicationDelegate { | |
var window: UIWindow? | |
func validateEmail(text: String) -> Bool { | |
let emailRegEx = "^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}$" // http://www.regular-expressions.info/email.html | |
let emailRange = text.uppercaseString.rangeOfString(emailRegEx, options: .RegularExpressionSearch) | |
return emailRange != nil | |
} | |
@objc func textDidChange(notification: NSNotification) -> Void { | |
if let tf = notification.object as? UITextField { | |
print(validateEmail(tf.text)) | |
} | |
return | |
} | |
func application(app: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { | |
let f = UIScreen.mainScreen().bounds | |
window = UIWindow(frame: f) | |
var vc = UIViewController() | |
vc.view = UIView(frame: f) | |
vc.view.backgroundColor = .greenColor() | |
var tf = UITextField(frame: CGRect(x: 10, y: 10, width: 100, height: 50)) | |
tf.backgroundColor = .redColor() | |
vc.view.addSubview(tf) | |
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("textDidChange:"), | |
name: UITextFieldTextDidChangeNotification, object: tf) | |
window?.rootViewController = vc | |
window?.makeKeyAndVisible() | |
return true | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm not sure how I came across this now, but have you tried just removing the "-> Void" and the return statement from the notification handler?