Skip to content

Instantly share code, notes, and snippets.

@cfr
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cfr/cbde9da91297912821dd to your computer and use it in GitHub Desktop.
Save cfr/cbde9da91297912821dd to your computer and use it in GitHub Desktop.
Swift NSNotification crash
//
// 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
}
}
@ephemer
Copy link

ephemer commented Apr 30, 2015

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment