From f413603c0d25368bb672d056f8b04be91599bc59 Mon Sep 17 00:00:00 2001 From: Yoshitaka Seki Date: Thu, 22 Sep 2016 19:11:10 +0900 Subject: [PATCH] Convert source compatible to Swift3.0 --- ActionClosurable-Demo/AppDelegate.swift | 12 +++++----- ActionClosurable-Demo/ViewController.swift | 19 ++++++++-------- ActionClosurable.xcodeproj/project.pbxproj | 14 +++++++----- ActionClosurable/ActionClosurable.swift | 22 +++++++++--------- ActionClosurable/Extensions.swift | 26 +++++++++++----------- 5 files changed, 47 insertions(+), 46 deletions(-) diff --git a/ActionClosurable-Demo/AppDelegate.swift b/ActionClosurable-Demo/AppDelegate.swift index cbd4cd2..c1aefd9 100644 --- a/ActionClosurable-Demo/AppDelegate.swift +++ b/ActionClosurable-Demo/AppDelegate.swift @@ -14,30 +14,30 @@ class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? - func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { + func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. return true } - func applicationWillResignActive(application: UIApplication) { + func applicationWillResignActive(_ application: UIApplication) { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. } - func applicationDidEnterBackground(application: UIApplication) { + func applicationDidEnterBackground(_ application: UIApplication) { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. } - func applicationWillEnterForeground(application: UIApplication) { + func applicationWillEnterForeground(_ application: UIApplication) { // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. } - func applicationDidBecomeActive(application: UIApplication) { + func applicationDidBecomeActive(_ application: UIApplication) { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. } - func applicationWillTerminate(application: UIApplication) { + func applicationWillTerminate(_ application: UIApplication) { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } diff --git a/ActionClosurable-Demo/ViewController.swift b/ActionClosurable-Demo/ViewController.swift index 7f3d6bf..9c45dfd 100644 --- a/ActionClosurable-Demo/ViewController.swift +++ b/ActionClosurable-Demo/ViewController.swift @@ -12,9 +12,9 @@ import ActionClosurable extension UIImage { static func whiteImage() -> UIImage? { UIGraphicsBeginImageContextWithOptions(CGSize(width: 20, height: 20), false, 0) - let contextImg = UIGraphicsGetCurrentContext() - CGContextSetRGBFillColor(contextImg!,1,1,1,1) - CGContextFillRect(contextImg!, CGRect(x: 0, y: 0, width: 20, height: 20)) + let context = UIGraphicsGetCurrentContext() + context?.setFillColor(red: 1,green: 1,blue: 1,alpha: 1) + context?.fill(CGRect(x: 0, y: 0, width: 20, height: 20)) let image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() @@ -25,21 +25,20 @@ extension UIImage { class ViewController: UIViewController { var button: UIButton? = UIButton(frame: CGRect(x: 0,y: 70,width: 100,height: 100)) var gr: UITapGestureRecognizer? = UITapGestureRecognizer() - var timer: NSTimer? override func viewDidLoad() { super.viewDidLoad() navigationItem.rightBarButtonItems = [ - UIBarButtonItem(title: "title", style: .Plain, closure: { _ in + UIBarButtonItem(title: "title", style: .plain, closure: { _ in print("barButtonItem title") }), - UIBarButtonItem(image: UIImage.whiteImage(), style: .Plain, closure: { _ in + UIBarButtonItem(image: UIImage.whiteImage(), style: .plain, closure: { _ in print("barButtonItem image") }), ] - button?.setTitle("button", forState: .Normal) - button?.backgroundColor = UIColor.redColor() + button?.setTitle("button", for: UIControlState()) + button?.backgroundColor = UIColor.red self.view.addSubview(button!) button?.onTap { [weak self] in @@ -48,7 +47,7 @@ class ViewController: UIViewController { self!.button = nil } - button?.on(.TouchDown) { + button?.on(.touchDown) { print($0) } @@ -61,7 +60,7 @@ class ViewController: UIViewController { let label = UILabel(frame: CGRect(x: 0, y: 300, width: 200, height: 20)) label.text = "hogehoge" - label.userInteractionEnabled = true + label.isUserInteractionEnabled = true self.view.addSubview(label) label.addGestureRecognizer(UIPanGestureRecognizer { gr in print("UIPanGestureRecognizer fire") diff --git a/ActionClosurable.xcodeproj/project.pbxproj b/ActionClosurable.xcodeproj/project.pbxproj index 745a2dd..93fa94f 100644 --- a/ActionClosurable.xcodeproj/project.pbxproj +++ b/ActionClosurable.xcodeproj/project.pbxproj @@ -232,9 +232,11 @@ }; B6554A291CC24F3D0060DC4B = { CreatedOnToolsVersion = 7.3; + LastSwiftMigration = 0800; }; B6554A4A1CC25F320060DC4B = { CreatedOnToolsVersion = 7.3; + LastSwiftMigration = 0800; }; }; }; @@ -448,7 +450,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = takasek.ActionClosurableTests; PRODUCT_NAME = "$(TARGET_NAME)"; - SWIFT_VERSION = 2.3; + SWIFT_VERSION = 3.0; }; name = Debug; }; @@ -459,7 +461,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = takasek.ActionClosurableTests; PRODUCT_NAME = "$(TARGET_NAME)"; - SWIFT_VERSION = 2.3; + SWIFT_VERSION = 3.0; }; name = Release; }; @@ -478,7 +480,7 @@ PRODUCT_BUNDLE_IDENTIFIER = takasek.ActionClosurable; PRODUCT_NAME = "$(TARGET_NAME)"; SKIP_INSTALL = YES; - SWIFT_VERSION = 2.3; + SWIFT_VERSION = 3.0; VERSIONING_SYSTEM = "apple-generic"; VERSION_INFO_PREFIX = ""; }; @@ -499,7 +501,7 @@ PRODUCT_BUNDLE_IDENTIFIER = takasek.ActionClosurable; PRODUCT_NAME = "$(TARGET_NAME)"; SKIP_INSTALL = YES; - SWIFT_VERSION = 2.3; + SWIFT_VERSION = 3.0; VERSIONING_SYSTEM = "apple-generic"; VERSION_INFO_PREFIX = ""; }; @@ -514,7 +516,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = "takasek.ActionClosurable-Demo"; PRODUCT_NAME = "$(TARGET_NAME)"; - SWIFT_VERSION = 2.3; + SWIFT_VERSION = 3.0; }; name = Debug; }; @@ -527,7 +529,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = "takasek.ActionClosurable-Demo"; PRODUCT_NAME = "$(TARGET_NAME)"; - SWIFT_VERSION = 2.3; + SWIFT_VERSION = 3.0; }; name = Release; }; diff --git a/ActionClosurable/ActionClosurable.swift b/ActionClosurable/ActionClosurable.swift index c727ec4..07f7755 100644 --- a/ActionClosurable/ActionClosurable.swift +++ b/ActionClosurable/ActionClosurable.swift @@ -9,19 +9,19 @@ import Foundation public class Actor { - @objc func act(sender: AnyObject) { closure(sender as! T) } - private let closure: T -> Void - init(_ closure: T -> Void) { + @objc func act(_ sender: AnyObject) { closure(sender as! T) } + fileprivate let closure: (T) -> Void + init(acts closure: @escaping (T) -> Void) { self.closure = closure } } private class GreenRoom { - private var actors: [Any] = [] + fileprivate var actors: [Any] = [] } private var GreenRoomKey: UInt32 = 893 -func register(actor: Actor, to object: AnyObject) { +private func _register(_ actor: Actor, to object: AnyObject) { let room = objc_getAssociatedObject(object, &GreenRoomKey) as? GreenRoom ?? { let room = GreenRoom() objc_setAssociatedObject(object, &GreenRoomKey, room, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) @@ -32,15 +32,15 @@ func register(actor: Actor, to object: AnyObject) { public protocol ActionClosurable {} public extension ActionClosurable where Self: AnyObject { - public func registerClosure(closure: Self -> Void, @noescape configure: (Actor, Selector) -> Void) { - let actor = Actor(closure) + public func register(closure: @escaping (Self) -> Void, configure: (Actor, Selector) -> Void) { + let actor = Actor(acts: closure) configure(actor, #selector(Actor.act(_:))) - register(actor, to: self) + _register(actor, to: self) } - public static func registerClosure(closure: Self -> Void, @noescape configure: (Actor, Selector) -> Self) -> Self { - let actor = Actor(closure) + public static func register(closure: @escaping (Self) -> Void, configure: (Actor, Selector) -> Self) -> Self { + let actor = Actor(acts: closure) let instance = configure(actor, #selector(Actor.act(_:))) - register(actor, to: instance) + _register(actor, to: instance) return instance } } diff --git a/ActionClosurable/Extensions.swift b/ActionClosurable/Extensions.swift index ff55641..0e4b33b 100644 --- a/ActionClosurable/Extensions.swift +++ b/ActionClosurable/Extensions.swift @@ -9,49 +9,49 @@ import UIKit extension ActionClosurable where Self: UIControl { - public func on(controlEvents: UIControlEvents, closure: Self -> Void) { - registerClosure(closure) { - self.addTarget($0, action: $1, forControlEvents: controlEvents) + public func on(_ controlEvents: UIControlEvents, closure: @escaping (Self) -> Void) { + register(closure: closure) { + self.addTarget($0, action: $1, for: controlEvents) } } } extension ActionClosurable where Self: UIButton { - public func onTap(closure: Self -> Void) { - registerClosure(closure) { - self.addTarget($0, action: $1, forControlEvents: .TouchUpInside) + public func onTap(_ closure: @escaping (Self) -> Void) { + register(closure: closure) { + self.addTarget($0, action: $1, for: .touchUpInside) } } } extension ActionClosurable where Self: UIGestureRecognizer { - public func onGesture(closure: Self -> Void) { - registerClosure(closure) { + public func onGesture(_ closure: @escaping (Self) -> Void) { + register(closure: closure) { self.addTarget($0, action: $1) } } - public init(closure: Self -> Void) { + public init(closure: @escaping (Self) -> Void) { self.init() onGesture(closure) } } extension ActionClosurable where Self: UIBarButtonItem { - public init(title: String, style: UIBarButtonItemStyle, closure: Self -> Void) { + public init(title: String, style: UIBarButtonItemStyle, closure: @escaping (Self) -> Void) { self.init() self.title = title self.style = style self.onTap(closure) } - public init(image: UIImage?, style: UIBarButtonItemStyle, closure: Self -> Void) { + public init(image: UIImage?, style: UIBarButtonItemStyle, closure: @escaping (Self) -> Void) { self.init() self.image = image self.style = style self.onTap(closure) } - public func onTap(closure: Self -> Void) { - registerClosure(closure) { + public func onTap(_ closure: @escaping (Self) -> Void) { + register(closure: closure) { self.target = $0 self.action = $1 }