Created
November 8, 2016 02:55
-
-
Save s2339956/da0e59160ab6b8006302502404410f60 to your computer and use it in GitHub Desktop.
Swiftの便利なextension
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
// | |
// Util.swift | |
// | |
// | |
// Created by himara2 on 2015/11/01. | |
// | |
// | |
import Foundation | |
extension NSObject { | |
class var className: String { | |
get { | |
return NSStringFromClass(self).componentsSeparatedByString(".").last! | |
} | |
} | |
var className: String { | |
get { | |
return self.dynamicType.className | |
} | |
} | |
} | |
extension UITableView { | |
func registerCell<T: UITableViewCell>(type: T.Type) { | |
let className = type.className | |
let nib = UINib(nibName: className, bundle: nil) | |
registerNib(nib, forCellReuseIdentifier: className) | |
} | |
func dequeueCell<T: UITableViewCell>(type: T.Type, indexPath: NSIndexPath) -> T { | |
return self.dequeueReusableCellWithIdentifier(type.className, forIndexPath: indexPath) as! T | |
} | |
} | |
extension UICollectionView { | |
func registerCell<T: UICollectionViewCell>(type: T.Type) { | |
let className = type.className | |
let nib = UINib(nibName: type.className, bundle: nil) | |
registerNib(nib, forCellWithReuseIdentifier: className) | |
} | |
func registerReusableView<T: UICollectionReusableView>(type: T.Type, kind: String) { | |
let className = type.className | |
let nib = UINib(nibName: className, bundle: nil) | |
registerNib(nib, forSupplementaryViewOfKind: kind, withReuseIdentifier: className) | |
} | |
func dequeueCell<T: UICollectionViewCell>(type: T.Type, forIndexPath indexPath: NSIndexPath) -> T { | |
return dequeueReusableCellWithReuseIdentifier(type.className, forIndexPath: indexPath) as! T | |
} | |
func dequeueReusableView<T: UICollectionReusableView>(kind: String, type: T.Type, indexPath: NSIndexPath) -> T { | |
return dequeueReusableSupplementaryViewOfKind(kind, | |
withReuseIdentifier: type.className, | |
forIndexPath: indexPath) as! T | |
} | |
} | |
extension UIColor { | |
class func color(hex: Int, alpha: Double = 1.0) -> UIColor { | |
let red = Double((hex & 0xFF0000) >> 16) / 255.0 | |
let green = Double((hex & 0xFF00) >> 8) / 255.0 | |
let blue = Double((hex & 0xFF)) / 255.0 | |
return UIColor(red: CGFloat(red), green: CGFloat(green), blue: CGFloat(blue), alpha: CGFloat(alpha)) | |
} | |
class func defaultColor(alpha: Double = 1.0) -> UIColor { | |
return UIColor.color(0x00ACED, alpha: alpha) | |
} | |
} | |
extension UIApplication { | |
func topViewController() -> UIViewController? { | |
if var topViewController = UIApplication.sharedApplication().keyWindow?.rootViewController { | |
while (topViewController.presentedViewController != nil) { | |
topViewController = topViewController.presentedViewController! | |
} | |
return topViewController | |
} else { | |
return nil | |
} | |
} | |
} | |
private let isFirstSessionDoneKey = "IS_FIRST_SESSION_DONE" | |
extension NSUserDefaults { | |
var isFirstSessionDone: Bool? { | |
get { | |
return self.objectForKey(isFirstSessionDoneKey) as? Bool | |
} | |
set { | |
self.setObject(true, forKey: isFirstSessionDoneKey) | |
self.synchronize() | |
} | |
} | |
} | |
func instantiate<T: UIViewController where T: NSObject>(_: T.Type) -> T { | |
let storyboard = UIStoryboard(name: T.className, bundle: nil) | |
return storyboard.instantiateInitialViewController() as! T | |
} | |
func instantiate<T: UIViewController where T: NSObject>(_: T.Type, storyboard: String) -> T { | |
let storyboard = UIStoryboard(name: storyboard, bundle: nil) | |
return storyboard.instantiateViewControllerWithIdentifier(T.className) as! T | |
} | |
func instantiateFromNib<T: UIView, U: AnyObject where T: NSObject>(_: T.Type, owner: U) -> T { | |
return UINib(nibName: T.className, bundle: nil).instantiateWithOwner(owner, options: nil)[0] as! T | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment