Skip to content

Commit

Permalink
Merge pull request #24 from tattn/swift5
Browse files Browse the repository at this point in the history
Swift 5 support
  • Loading branch information
tattn authored Apr 6, 2019
2 parents b745a07 + fe91b6c commit 40ef0d9
Show file tree
Hide file tree
Showing 18 changed files with 79 additions and 70 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ SwiftExtensions

[![Build Status](https://app.bitrise.io/app/e58694b1fa46a551/status.svg?token=E2FYmP02umcT9pjF4NKDSw)](https://app.bitrise.io/app/e58694b1fa46a551)
[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)
[![Swift Version](https://img.shields.io/badge/Swift-4-F16D39.svg)](https://developer.apple.com/swift)
[![Swift Version](https://img.shields.io/badge/Swift-5-F16D39.svg)](https://developer.apple.com/swift)

My favorite Swift extensions.

Expand Down
12 changes: 6 additions & 6 deletions Sources/Array+.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,26 @@ import Foundation

public extension Array where Element: Equatable {
@discardableResult
public mutating func remove(_ element: Element) -> Index? {
guard let index = index(of: element) else { return nil }
mutating func remove(_ element: Element) -> Index? {
guard let index = firstIndex(of: element) else { return nil }
remove(at: index)
return index
}

@discardableResult
public mutating func remove(_ elements: [Element]) -> [Index] {
mutating func remove(_ elements: [Element]) -> [Index] {
return elements.compactMap { remove($0) }
}
}

public extension Array where Element: Hashable {
public mutating func unify() {
mutating func unify() {
self = unified()
}
}

public extension Collection where Element: Hashable {
public func unified() -> [Element] {
func unified() -> [Element] {
return reduce(into: []) {
if !$0.contains($1) {
$0.append($1)
Expand All @@ -39,7 +39,7 @@ public extension Collection where Element: Hashable {
}

public extension Collection {
public subscript(safe index: Index) -> Element? {
subscript(safe index: Index) -> Element? {
return startIndex <= index && index < endIndex ? self[index] : nil
}
}
4 changes: 2 additions & 2 deletions Sources/Comparable+.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

import Foundation

extension Comparable {
public func clamped(min: Self, max: Self) -> Self {
public extension Comparable {
func clamped(min: Self, max: Self) -> Self {
if self < min { return min }
if self > max { return max }
return self
Expand Down
6 changes: 3 additions & 3 deletions Sources/Error+.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import Foundation

extension Error {
public var code: Int { return (self as NSError).code }
public var domain: String { return (self as NSError).domain }
public extension Error {
var code: Int { return (self as NSError).code }
var domain: String { return (self as NSError).domain }
}
6 changes: 3 additions & 3 deletions Sources/NSObject+.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ public protocol ClassNameProtocol {
}

public extension ClassNameProtocol {
public static var className: String {
static var className: String {
return String(describing: self)
}

public var className: String {
var className: String {
return type(of: self).className
}
}

extension NSObject: ClassNameProtocol {}

public extension NSObjectProtocol {
public var describedProperty: String {
var describedProperty: String {
let mirror = Mirror(reflecting: self)
return mirror.children.map { element -> String in
let key = element.label ?? "Unknown"
Expand Down
16 changes: 8 additions & 8 deletions Sources/NibInstantiatable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ public protocol NibInstantiatable {
}

public extension NibInstantiatable where Self: NSObject {
public static var nibName: String { return className }
public static var nibBundle: Bundle { return Bundle(for: self) }
public static var nibOwner: Any? { return self }
public static var nibOptions: [UINib.OptionsKey: Any]? { return nil }
public static var instantiateIndex: Int { return 0 }
static var nibName: String { return className }
static var nibBundle: Bundle { return Bundle(for: self) }
static var nibOwner: Any? { return self }
static var nibOptions: [UINib.OptionsKey: Any]? { return nil }
static var instantiateIndex: Int { return 0 }
}

public extension NibInstantiatable where Self: UIView {
public static func instantiate() -> Self {
static func instantiate() -> Self {
let nib = UINib(nibName: nibName, bundle: nibBundle)
return nib.instantiate(withOwner: nibOwner, options: nibOptions)[instantiateIndex] as! Self
}
Expand All @@ -36,9 +36,9 @@ public protocol EmbeddedNibInstantiatable {
}

public extension EmbeddedNibInstantiatable where Self: UIView, Embedded: UIView {
public var embedded: Embedded { return subviews[0] as! Embedded }
var embedded: Embedded { return subviews[0] as! Embedded }

public func configureEmbededView() {
func configureEmbededView() {
let view = Embedded.instantiate()
insertSubview(view, at: 0)
view.fillSuperview()
Expand Down
4 changes: 2 additions & 2 deletions Sources/ScopeMethod.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public protocol Appliable {}

public extension Appliable {
@discardableResult
public func apply(closure: (Self) -> Void) -> Self {
func apply(closure: (Self) -> Void) -> Self {
closure(self)
return self
}
Expand All @@ -22,7 +22,7 @@ public protocol Runnable {}

public extension Runnable {
@discardableResult
public func run<T>(closure: (Self) -> T) -> T {
func run<T>(closure: (Self) -> T) -> T {
return closure(self)
}
}
Expand Down
8 changes: 4 additions & 4 deletions Sources/StoryboardInstantiatable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,25 @@ public protocol StoryboardInstantiatable {
}

public extension StoryboardInstantiatable where Self: NSObject {
public static var storyboardName: String {
static var storyboardName: String {
return className
}

public static var storyboardBundle: Bundle {
static var storyboardBundle: Bundle {
return Bundle(for: self)
}

private static var storyboard: UIStoryboard {
return UIStoryboard(name: storyboardName, bundle: storyboardBundle)
}

public static var instantiateType: StoryboardInstantiateType {
static var instantiateType: StoryboardInstantiateType {
return .identifier(className)
}
}

public extension StoryboardInstantiatable where Self: UIViewController {
public static func instantiate() -> Self {
static func instantiate() -> Self {
switch instantiateType {
case .initial:
return storyboard.instantiateInitialViewController() as! Self
Expand Down
10 changes: 5 additions & 5 deletions Sources/String+.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@
import Foundation

public extension String {
public var localized: String {
var localized: String {
return NSLocalizedString(self, comment: self)
}

public func localized(withTableName tableName: String? = nil, bundle: Bundle = Bundle.main, value: String = "") -> String {
func localized(withTableName tableName: String? = nil, bundle: Bundle = Bundle.main, value: String = "") -> String {
return NSLocalizedString(self, tableName: tableName, bundle: bundle, value: value, comment: self)
}
}

public extension String {
public var url: URL? {
var url: URL? {
return URL(string: self)
}
}
Expand Down Expand Up @@ -54,11 +54,11 @@ public extension String {
}

public extension String {
public var halfWidth: String {
var halfWidth: String {
return transformFullWidthToHalfWidth(reverse: false)
}

public var fullWidth: String {
var fullWidth: String {
return transformFullWidthToHalfWidth(reverse: true)
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/TargetedExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ public protocol TargetedExtensionCompatible {
}

public extension TargetedExtensionCompatible where Self == Compatible {
public static var ex: TargetedExtension<Self>.Type {
static var ex: TargetedExtension<Self>.Type {
return TargetedExtension<Self>.self
}

public var ex: TargetedExtension<Self> {
var ex: TargetedExtension<Self> {
return TargetedExtension(self)
}
}
4 changes: 2 additions & 2 deletions Sources/UIApplication+.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import UIKit

public extension UIApplication {
public var topViewController: UIViewController? {
var topViewController: UIViewController? {
guard var topViewController = UIApplication.shared.keyWindow?.rootViewController else { return nil }

while let presentedViewController = topViewController.presentedViewController {
Expand All @@ -19,7 +19,7 @@ public extension UIApplication {
}


public var topNavigationController: UINavigationController? {
var topNavigationController: UINavigationController? {
return topViewController as? UINavigationController
}
}
12 changes: 6 additions & 6 deletions Sources/UICollectionView+.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,36 @@
import UIKit

public extension UICollectionView {
public func register(cellType: UICollectionViewCell.Type, bundle: Bundle? = nil) {
func register(cellType: UICollectionViewCell.Type, bundle: Bundle? = nil) {
let className = cellType.className
let nib = UINib(nibName: className, bundle: bundle)
register(nib, forCellWithReuseIdentifier: className)
}

public func register(cellTypes: [UICollectionViewCell.Type], bundle: Bundle? = nil) {
func register(cellTypes: [UICollectionViewCell.Type], bundle: Bundle? = nil) {
cellTypes.forEach { register(cellType: $0, bundle: bundle) }
}

public func register(reusableViewType: UICollectionReusableView.Type,
func register(reusableViewType: UICollectionReusableView.Type,
ofKind kind: String = UICollectionView.elementKindSectionHeader,
bundle: Bundle? = nil) {
let className = reusableViewType.className
let nib = UINib(nibName: className, bundle: bundle)
register(nib, forSupplementaryViewOfKind: kind, withReuseIdentifier: className)
}

public func register(reusableViewTypes: [UICollectionReusableView.Type],
func register(reusableViewTypes: [UICollectionReusableView.Type],
ofKind kind: String = UICollectionView.elementKindSectionHeader,
bundle: Bundle? = nil) {
reusableViewTypes.forEach { register(reusableViewType: $0, ofKind: kind, bundle: bundle) }
}

public func dequeueReusableCell<T: UICollectionViewCell>(with type: T.Type,
func dequeueReusableCell<T: UICollectionViewCell>(with type: T.Type,
for indexPath: IndexPath) -> T {
return dequeueReusableCell(withReuseIdentifier: type.className, for: indexPath) as! T
}

public func dequeueReusableView<T: UICollectionReusableView>(with type: T.Type,
func dequeueReusableView<T: UICollectionReusableView>(with type: T.Type,
for indexPath: IndexPath,
ofKind kind: String = UICollectionView.elementKindSectionHeader) -> T {
return dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: type.className, for: indexPath) as! T
Expand Down
8 changes: 4 additions & 4 deletions Sources/UIColor+.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
import UIKit

public extension UIColor {
public convenience init(hex: Int, alpha: Double = 1.0) {
convenience init(hex: Int, alpha: Double = 1.0) {
let r = CGFloat((hex & 0xFF0000) >> 16) / 255.0
let g = CGFloat((hex & 0x00FF00) >> 8) / 255.0
let b = CGFloat(hex & 0x0000FF) / 255.0
self.init(red: r, green: g, blue: b, alpha: CGFloat(alpha))
}

public convenience init?(rgbHexString: String, alpha: Double = 1.0) {
convenience init?(rgbHexString: String, alpha: Double = 1.0) {
let scanner = Scanner(string: rgbHexString.replacingOccurrences(of: "#", with: ""))
var rgbHex: UInt32 = 0
guard scanner.scanHexInt32(&rgbHex) else {
Expand All @@ -33,7 +33,7 @@ public extension UIColor {
}

public extension UIColor {
public struct Components {
struct Components {
var _base: UIColor
public var rgba: (CGFloat, CGFloat, CGFloat, CGFloat) {
var r: CGFloat = 0; var g: CGFloat = 0; var b: CGFloat = 0; var a: CGFloat = 0
Expand All @@ -46,7 +46,7 @@ public extension UIColor {
return (h, s, v)
}
}
public var components: UIColor.Components {
var components: UIColor.Components {
return Components(_base: self)
}
}
16 changes: 8 additions & 8 deletions Sources/UIImage+.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import UIKit

public extension UIImage {
public convenience init(color: UIColor, size: CGSize) {
convenience init(color: UIColor, size: CGSize) {
UIGraphicsBeginImageContext(size)
guard let context: CGContext = UIGraphicsGetCurrentContext() else {
self.init()
Expand All @@ -32,7 +32,7 @@ public extension UIImage {
}
}

public func image(withTint color: UIColor) -> UIImage {
func image(withTint color: UIColor) -> UIImage {
let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
UIGraphicsBeginImageContextWithOptions(rect.size, false, 0)

Expand All @@ -53,7 +53,7 @@ public extension UIImage {
return image
}

public func cropping(to rect: CGRect) -> UIImage? {
func cropping(to rect: CGRect) -> UIImage? {
let originalRect = CGRect(
x: rect.origin.x * scale,
y: rect.origin.y * scale,
Expand All @@ -67,7 +67,7 @@ public extension UIImage {
return UIImage(cgImage: imageRef, scale: scale, orientation: imageOrientation)
}

public func resize(to newSize: CGSize) -> UIImage? {
func resize(to newSize: CGSize) -> UIImage? {
UIGraphicsBeginImageContext(CGSize(
width: newSize.width * scale,
height: newSize.height * scale
Expand All @@ -82,7 +82,7 @@ public extension UIImage {
return UIImage(cgImage: cgImage, scale: scale, orientation: imageOrientation)
}

public func resize(to newSize: CGSize, scalingMode: ScalingMode) -> UIImage? {
func resize(to newSize: CGSize, scalingMode: ScalingMode) -> UIImage? {
let aspectRatio = scalingMode.aspectRatio(between: newSize, and: size)

let scaledImageRect = CGRect(x: (newSize.width - size.width * aspectRatio) / 2.0,
Expand All @@ -96,15 +96,15 @@ public extension UIImage {
return UIGraphicsGetImageFromCurrentImageContext()
}

public func toJPEG(quarity: CGFloat = 1.0) -> Data? {
func toJPEG(quarity: CGFloat = 1.0) -> Data? {
return self.jpegData(compressionQuality: quarity)
}

public func toPNG(quarity: CGFloat = 1.0) -> Data? {
func toPNG(quarity: CGFloat = 1.0) -> Data? {
return self.pngData()
}

public func rounded() -> UIImage? {
func rounded() -> UIImage? {
let imageView = UIImageView(image: self)
imageView.layer.cornerRadius = min(size.height/2, size.width/2)
imageView.layer.masksToBounds = true
Expand Down
Loading

0 comments on commit 40ef0d9

Please sign in to comment.