Fixed some swiftlint warnings

This commit is contained in:
Lucas Farah 2017-01-24 10:24:35 -02:00 committed by Arunav Sanyal
parent 5b6250722f
commit 1885a24296
18 changed files with 173 additions and 194 deletions

View File

@ -43,7 +43,7 @@ extension Array {
}
/// EZSE: Iterates on each element of the array with its index. (Index, Element)
public func forEachEnumerated(_ body: @escaping (_ offset: Int, _ element: Element) -> ()) {
public func forEachEnumerated(_ body: @escaping (_ offset: Int, _ element: Element) -> Void) {
self.enumerated().forEach(body)
}
@ -201,7 +201,7 @@ extension Array where Element: Equatable {
}
}
//MARK: - Deprecated 1.8
// MARK: - Deprecated 1.8
extension Array {
@ -241,13 +241,13 @@ extension Array where Element: Equatable {
}
//MARK: - Deprecated 1.7
// MARK: - Deprecated 1.7
extension Array {
/// EZSE: Iterates on each element of the array with its index. (Index, Element)
@available(*, deprecated: 1.7, renamed: "forEachEnumerated(_:)")
public func forEach(_ call: @escaping (Int, Element) -> ()) {
public func forEach(_ call: @escaping (Int, Element) -> Void) {
forEachEnumerated(call)
}
@ -287,13 +287,13 @@ extension Array where Element: Equatable {
}
//MARK: - Deprecated 1.6
// MARK: - Deprecated 1.6
extension Array {
/// EZSE: Iterates on each element of the array.
@available(*, deprecated: 1.6, renamed: "forEach(_:)")
public func each(_ call: (Element) -> ()) {
public func each(_ call: (Element) -> Void) {
forEach(call)
}
@ -306,7 +306,7 @@ extension Array {
/// EZSE: Iterates on each element of the array with its index. (Index, Element)
@available(*, deprecated: 1.6, renamed: "forEachEnumerated(_:)")
public func each(_ call: @escaping (Int, Element) -> ()) {
public func each(_ call: @escaping (Int, Element) -> Void) {
forEachEnumerated(call)
}

View File

@ -12,9 +12,9 @@ import UIKit
///Make sure you use `[weak self] (NSURLRequest) in` if you are using the keyword `self` inside the closure or there might be a memory leak
open class BlockWebView: UIWebView, UIWebViewDelegate {
open var didStartLoad: ((URLRequest) -> ())?
open var didFinishLoad: ((URLRequest) -> ())?
open var didFailLoad: ((URLRequest, Error) -> ())?
open var didStartLoad: ((URLRequest) -> Void)?
open var didFinishLoad: ((URLRequest) -> Void)?
open var didFailLoad: ((URLRequest, Error) -> Void)?
open var shouldStartLoadingRequest: ((URLRequest) -> (Bool))?

View File

@ -73,10 +73,10 @@ extension CGFloat {
let twoPi = CGFloat(.pi * 2.0)
var angle = (second - first).truncatingRemainder(dividingBy: twoPi)
if angle >= .pi {
angle = angle - twoPi
angle -= twoPi
}
if angle <= -.pi {
angle = angle + twoPi
angle += twoPi
}
return angle
}

View File

@ -51,7 +51,7 @@ extension CGRect {
}
/// EZSE : Surface Area represented by a CGRectangle
public var area : CGFloat {
public var area: CGFloat {
return self.h * self.w
}
}

View File

@ -18,18 +18,14 @@ extension Character {
/// EZSE: Convert the character to lowercase
public var lowercased: Character {
get {
let s = String(self).lowercased()
return s[s.startIndex]
}
}
/// EZSE: Convert the character to uppercase
public var uppercased: Character {
get {
let s = String(self).uppercased()
return s[s.startIndex]
}
}
/// EZSE : Checks if character is emoji

View File

@ -200,7 +200,7 @@ extension Date {
}
/// EZSE : Gets the nano second from the date
public var nanosecond : Int {
public var nanosecond: Int {
return Calendar.current.component(.nanosecond, from: self)
}
}

View File

@ -162,7 +162,7 @@ extension Dictionary where Value: Equatable {
}
/// EZSE: Combines the first dictionary with the second and returns single dictionary
public func += <KeyType, ValueType> (left: inout Dictionary<KeyType, ValueType>, right: Dictionary<KeyType, ValueType>) {
public func += <KeyType, ValueType> (left: inout [KeyType: ValueType], right: [KeyType: ValueType]) {
for (k, v) in right {
left.updateValue(v, forKey: k)
}

View File

@ -16,7 +16,7 @@ extension Double {
public var toInt: Int { return Int(self) }
}
//MARK: - Deprecated 1.8
// MARK: - Deprecated 1.8
extension Double {
@ -45,8 +45,8 @@ extension Double {
}
/// EZSE: Absolute value of Double.
public var abs : Double {
if (self > 0) {
public var abs: Double {
if self > 0 {
return self
} else {
return -self

View File

@ -181,7 +181,7 @@ public struct ez {
}
/// EZSE: Calls action when a screen shot is taken
public static func detectScreenShot(_ action: @escaping () -> ()) {
public static func detectScreenShot(_ action: @escaping () -> Void) {
let mainQueue = OperationQueue.main
NotificationCenter.default.addObserver(forName: NSNotification.Name.UIApplicationUserDidTakeScreenshot, object: nil, queue: mainQueue) { notification in
// executes after screenshot
@ -205,30 +205,30 @@ public struct ez {
// MARK: - Dispatch
/// EZSE: Runs the function after x seconds
public static func dispatchDelay(_ second: Double, closure:@escaping ()->()) {
public static func dispatchDelay(_ second: Double, closure:@escaping () -> Void) {
DispatchQueue.main.asyncAfter(
deadline: DispatchTime.now() + Double(Int64(second * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC), execute: closure)
}
/// EZSE: Runs function after x seconds
public static func runThisAfterDelay(seconds: Double, after: @escaping () -> ()) {
public static func runThisAfterDelay(seconds: Double, after: @escaping () -> Void) {
runThisAfterDelay(seconds: seconds, queue: DispatchQueue.main, after: after)
}
//TODO: Make this easier
/// EZSE: Runs function after x seconds with dispatch_queue, use this syntax: dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0)
public static func runThisAfterDelay(seconds: Double, queue: DispatchQueue, after: @escaping ()->()) {
public static func runThisAfterDelay(seconds: Double, queue: DispatchQueue, after: @escaping () -> Void) {
let time = DispatchTime.now() + Double(Int64(seconds * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC)
queue.asyncAfter(deadline: time, execute: after)
}
/// EZSE: Submits a block for asynchronous execution on the main queue
public static func runThisInMainThread(_ block: @escaping ()->()) {
public static func runThisInMainThread(_ block: @escaping () -> Void) {
DispatchQueue.main.async(execute: block)
}
/// EZSE: Runs in Default priority queue
public static func runThisInBackground(_ block: @escaping () -> ()) {
public static func runThisInBackground(_ block: @escaping () -> Void) {
DispatchQueue.global(qos: .default).async(execute: block)
}

View File

@ -59,7 +59,7 @@ extension UInt {
/// EZSE: Greatest common divisor of two integers using the Euclid's algorithm.
/// Time complexity of this in O(log(n))
public static func gcd(_ firstNum:UInt, _ secondNum:UInt) -> UInt {
public static func gcd(_ firstNum: UInt, _ secondNum: UInt) -> UInt {
let remainder = firstNum % secondNum
if remainder != 0 {
return gcd(secondNum, remainder)
@ -69,7 +69,7 @@ extension UInt {
}
/// EZSE: Least common multiple of two numbers. LCM = n * m / gcd(n, m)
public static func lcm(_ firstNum:UInt, _ secondNum:UInt) -> UInt {
public static func lcm(_ firstNum: UInt, _ secondNum: UInt) -> UInt {
return firstNum * secondNum / UInt.gcd(firstNum, secondNum)
}
}

View File

@ -11,7 +11,7 @@ import Foundation
public extension NSDictionary {
//MARK: - Deprecated 1.8
// MARK: - Deprecated 1.8
/// EZSE: Unserialize JSON string into NSDictionary
@available(*, deprecated: 1.8)

View File

@ -9,9 +9,9 @@
// swiftlint:disable trailing_whitespace
#if os(OSX)
import AppKit
import AppKit
#else
import UIKit
import UIKit
#endif
extension String {
@ -25,48 +25,48 @@ extension String {
}
return nil
}
/// EZSE: base64 encoded of string
var base64: String {
let plainData = (self as NSString).data(using: String.Encoding.utf8.rawValue)
let base64String = plainData!.base64EncodedString(options: NSData.Base64EncodingOptions(rawValue: 0))
return base64String
}
/// EZSE: Cut string from integerIndex to the end
public subscript(integerIndex: Int) -> Character {
let index = characters.index(startIndex, offsetBy: integerIndex)
return self[index]
}
/// EZSE: Cut string from range
public subscript(integerRange: Range<Int>) -> String {
let start = characters.index(startIndex, offsetBy: integerRange.lowerBound)
let end = characters.index(startIndex, offsetBy: integerRange.upperBound)
return self[start..<end]
}
/// EZSE: Cut string from closedrange
public subscript(integerClosedRange: ClosedRange<Int>) -> String {
return self[integerClosedRange.lowerBound..<(integerClosedRange.upperBound + 1)]
}
/// EZSE: Character count
public var length: Int {
return self.characters.count
}
/// EZSE: Counts number of instances of the input inside String
public func count(_ substring: String) -> Int {
return components(separatedBy: substring).count - 1
}
/// EZSE: Capitalizes first character of String
public mutating func capitalizeFirst() {
guard characters.count > 0 else { return }
self.replaceSubrange(startIndex...startIndex, with: String(self[startIndex]).capitalized)
}
/// EZSE: Capitalizes first character of String, returns a new string
public func capitalizedFirst() -> String {
guard characters.count > 0 else { return self }
@ -88,7 +88,7 @@ extension String {
guard characters.count > 0 && count > 0 else { return self }
var result = self
result.replaceSubrange(startIndex..<self.index(startIndex, offsetBy: min(count, length)),
with: String(self[startIndex..<self.index(startIndex, offsetBy: min(count, length))]).uppercased())
with: String(self[startIndex..<self.index(startIndex, offsetBy: min(count, length))]).uppercased())
return result
}
@ -96,7 +96,7 @@ extension String {
public mutating func uppercaseSuffix(_ count: Int) {
guard characters.count > 0 && count > 0 else { return }
self.replaceSubrange(self.index(endIndex, offsetBy: -min(count, length))..<endIndex,
with: String(self[self.index(endIndex, offsetBy: -min(count, length))..<endIndex]).uppercased())
with: String(self[self.index(endIndex, offsetBy: -min(count, length))..<endIndex]).uppercased())
}
/// EZSE: Uppercases last 'count' characters of String, returns a new string
@ -104,7 +104,7 @@ extension String {
guard characters.count > 0 && count > 0 else { return self }
var result = self
result.replaceSubrange(characters.index(endIndex, offsetBy: -min(count, length))..<endIndex,
with: String(self[characters.index(endIndex, offsetBy: -min(count, length))..<endIndex]).uppercased())
with: String(self[characters.index(endIndex, offsetBy: -min(count, length))..<endIndex]).uppercased())
return result
}
@ -122,7 +122,7 @@ extension String {
guard characters.count > 0 && (0..<length).contains(from) else { return self }
var result = self
result.replaceSubrange(characters.index(startIndex, offsetBy: from)..<characters.index(startIndex, offsetBy: to),
with: String(self[characters.index(startIndex, offsetBy: from)..<characters.index(startIndex, offsetBy: to)]).uppercased())
with: String(self[characters.index(startIndex, offsetBy: from)..<characters.index(startIndex, offsetBy: to)]).uppercased())
return result
}
@ -144,7 +144,7 @@ extension String {
public mutating func lowercasePrefix(_ count: Int) {
guard characters.count > 0 && count > 0 else { return }
self.replaceSubrange(startIndex..<self.index(startIndex, offsetBy: min(count, length)),
with: String(self[startIndex..<self.index(startIndex, offsetBy: min(count, length))]).lowercased())
with: String(self[startIndex..<self.index(startIndex, offsetBy: min(count, length))]).lowercased())
}
/// EZSE: Lowercases first 'count' characters of String, returns a new string
@ -152,7 +152,7 @@ extension String {
guard characters.count > 0 && count > 0 else { return self }
var result = self
result.replaceSubrange(startIndex..<characters.index(startIndex, offsetBy: min(count, length)),
with: String(self[startIndex..<characters.index(startIndex, offsetBy: min(count, length))]).lowercased())
with: String(self[startIndex..<characters.index(startIndex, offsetBy: min(count, length))]).lowercased())
return result
}
@ -160,7 +160,7 @@ extension String {
public mutating func lowercaseSuffix(_ count: Int) {
guard characters.count > 0 && count > 0 else { return }
self.replaceSubrange(self.index(endIndex, offsetBy: -min(count, length))..<endIndex,
with: String(self[self.index(endIndex, offsetBy: -min(count, length))..<endIndex]).lowercased())
with: String(self[self.index(endIndex, offsetBy: -min(count, length))..<endIndex]).lowercased())
}
/// EZSE: Lowercases last 'count' characters of String, returns a new string
@ -168,7 +168,7 @@ extension String {
guard characters.count > 0 && count > 0 else { return self }
var result = self
result.replaceSubrange(characters.index(endIndex, offsetBy: -min(count, length))..<endIndex,
with: String(self[characters.index(endIndex, offsetBy: -min(count, length))..<endIndex]).lowercased())
with: String(self[characters.index(endIndex, offsetBy: -min(count, length))..<endIndex]).lowercased())
return result
}
@ -177,7 +177,7 @@ extension String {
let from = max(range.lowerBound, 0), to = min(range.upperBound, length)
guard characters.count > 0 && (0..<length).contains(from) else { return }
self.replaceSubrange(self.index(startIndex, offsetBy: from)..<self.index(startIndex, offsetBy: to),
with: String(self[self.index(startIndex, offsetBy: from)..<self.index(startIndex, offsetBy: to)]).lowercased())
with: String(self[self.index(startIndex, offsetBy: from)..<self.index(startIndex, offsetBy: to)]).lowercased())
}
/// EZSE: Lowercases string in range 'range' (from range.startIndex to range.endIndex), returns new string
@ -186,7 +186,7 @@ extension String {
guard characters.count > 0 && (0..<length).contains(from) else { return self }
var result = self
result.replaceSubrange(characters.index(startIndex, offsetBy: from)..<characters.index(startIndex, offsetBy: to),
with: String(self[characters.index(startIndex, offsetBy: from)..<characters.index(startIndex, offsetBy: to)]).lowercased())
with: String(self[characters.index(startIndex, offsetBy: from)..<characters.index(startIndex, offsetBy: to)]).lowercased())
return result
}
@ -200,22 +200,20 @@ extension String {
/// EZSE: Checks if string is empty or consists only of whitespace and newline characters
public var isBlank: Bool {
get {
let trimmed = trimmingCharacters(in: .whitespacesAndNewlines)
return trimmed.isEmpty
}
let trimmed = trimmingCharacters(in: .whitespacesAndNewlines)
return trimmed.isEmpty
}
/// EZSE: Trims white space and new line characters
public mutating func trim() {
self = self.trimmed()
self = self.trimmed()
}
/// EZSE: Trims white space and new line characters, returns a new string
public func trimmed() -> String {
return self.trimmingCharacters(in: .whitespacesAndNewlines)
}
/// EZSE: Position of begining character of substing
public func positionOfSubstring(_ subString: String, caseInsensitive: Bool = false, fromEnd: Bool = false) -> Int {
if subString.isEmpty {
@ -230,34 +228,34 @@ extension String {
}
return -1
}
/// EZSE: split string using a spearator string, returns an array of string
public func split(_ separator: String) -> [String] {
return self.components(separatedBy: separator).filter {
!$0.trimmed().isEmpty
}
}
/// EZSE: split string with delimiters, returns an array of string
public func split(_ characters: CharacterSet) -> [String] {
return self.components(separatedBy: characters).filter {
!$0.trimmed().isEmpty
}
}
/// EZSE : Returns count of words in string
public var countofWords: Int {
let regex = try? NSRegularExpression(pattern: "\\w+", options: NSRegularExpression.Options())
return regex?.numberOfMatches(in: self, options: NSRegularExpression.MatchingOptions(), range: NSRange(location: 0, length: self.length)) ?? 0
}
/// EZSE : Returns count of paragraphs in string
public var countofParagraphs: Int {
let regex = try? NSRegularExpression(pattern: "\\n", options: NSRegularExpression.Options())
let str = self.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
return (regex?.numberOfMatches(in: str, options: NSRegularExpression.MatchingOptions(), range: NSRange(location:0, length: str.length)) ?? -1) + 1
}
internal func rangeFromNSRange(_ nsRange: NSRange) -> Range<String.Index>? {
let from16 = utf16.startIndex.advanced(by: nsRange.location)
let to16 = from16.advanced(by: nsRange.length)
@ -267,21 +265,21 @@ extension String {
}
return nil
}
/// EZSE: Find matches of regular expression in string
public func matchesForRegexInText(_ regex: String!) -> [String] {
let regex = try? NSRegularExpression(pattern: regex, options: [])
let results = regex?.matches(in: self, options: [], range: NSRange(location: 0, length: self.length)) ?? []
return results.map { self.substring(with: self.rangeFromNSRange($0.range)!) }
}
/// EZSE: Checks if String contains Email
public var isEmail: Bool {
let dataDetector = try? NSDataDetector(types: NSTextCheckingResult.CheckingType.link.rawValue)
let firstMatch = dataDetector?.firstMatch(in: self, options: NSRegularExpression.MatchingOptions.reportCompletion, range: NSRange(location: 0, length: length))
return (firstMatch?.range.location != NSNotFound && firstMatch?.url?.scheme == "mailto")
}
/// EZSE: Returns if String is a number
public func isNumber() -> Bool {
if let _ = NumberFormatter().number(from: self) {
@ -289,7 +287,7 @@ extension String {
}
return false
}
/// EZSE: Extracts URLS from String
public var extractURLs: [URL] {
var urls: [URL] = []
@ -299,9 +297,9 @@ extension String {
} catch _ as NSError {
detector = nil
}
let text = self
if let detector = detector {
detector.enumerateMatches(in: text, options: [], range: NSRange(location: 0, length: text.characters.count), using: {
(result: NSTextCheckingResult?, flags: NSRegularExpression.MatchingFlags, stop: UnsafeMutablePointer<ObjCBool>) -> Void in
@ -310,15 +308,15 @@ extension String {
}
})
}
return urls
}
/// EZSE: Checking if String contains input with comparing options
public func contains(_ find: String, compareOption: NSString.CompareOptions) -> Bool {
return self.range(of: find, options: compareOption) != nil
}
/// EZSE: Converts String to Int
public func toInt() -> Int? {
if let num = NumberFormatter().number(from: self) {
@ -327,7 +325,7 @@ extension String {
return nil
}
}
/// EZSE: Converts String to Double
public func toDouble() -> Double? {
if let num = NumberFormatter().number(from: self) {
@ -336,7 +334,7 @@ extension String {
return nil
}
}
/// EZSE: Converts String to Float
public func toFloat() -> Float? {
if let num = NumberFormatter().number(from: self) {
@ -345,7 +343,7 @@ extension String {
return nil
}
}
/// EZSE: Converts String to Bool
public func toBool() -> Bool? {
let trimmedString = trimmed().lowercased()
@ -354,7 +352,7 @@ extension String {
}
return nil
}
///EZSE: Returns the first index of the occurency of the character in String
public func getIndexOf(_ char: Character) -> Int? {
for (index, c) in characters.enumerated() {
@ -364,10 +362,10 @@ extension String {
}
return nil
}
/// EZSE: Converts String to NSString
public var toNSString: NSString { get { return self as NSString } }
public var toNSString: NSString { return self as NSString }
#if os(iOS)
///EZSE: Returns bold NSAttributedString
@ -377,7 +375,7 @@ extension String {
}
#endif
///EZSE: Returns underlined NSAttributedString
public func underline() -> NSAttributedString {
let underlineString = NSAttributedString(string: self, attributes: [NSUnderlineStyleAttributeName: NSUnderlineStyle.styleSingle.rawValue])
@ -385,7 +383,7 @@ extension String {
}
#if os(iOS)
///EZSE: Returns italic NSAttributedString
public func italic() -> NSAttributedString {
let italicString = NSMutableAttributedString(string: self, attributes: [NSFontAttributeName: UIFont.italicSystemFont(ofSize: UIFont.systemFontSize)])
@ -393,9 +391,9 @@ extension String {
}
#endif
#if os(iOS)
///EZSE: Returns hight of rendered string
func height(_ width: CGFloat, font: UIFont, lineBreakMode: NSLineBreakMode?) -> CGFloat {
var attrib: [String: AnyObject] = [NSFontAttributeName: font]
@ -409,13 +407,13 @@ extension String {
}
#endif
///EZSE: Returns NSAttributedString
public func color(_ color: UIColor) -> NSAttributedString {
let colorString = NSMutableAttributedString(string: self, attributes: [NSForegroundColorAttributeName: color])
return colorString
}
///EZSE: Returns NSAttributedString
public func colorSubString(_ subString: String, color: UIColor) -> NSMutableAttributedString {
var start = 0
@ -435,7 +433,7 @@ extension String {
}
return attrText
}
/// EZSE: Checks if String contains Emoji
public func includesEmoji() -> Bool {
for i in 0...length {
@ -448,9 +446,9 @@ extension String {
}
#if os(iOS)
/// EZSE: copy string to pasteboard
public func addToPasteboard() {
public func addToPasteboard() {
let pasteboard = UIPasteboard.general
pasteboard.string = self
}
@ -485,7 +483,7 @@ extension String {
nFormatter.maximumFractionDigits = precision
self = nFormatter.string(from: NSNumber(value: value))!
}
init(_ value: Double, precision: Int) {
let nFormatter = NumberFormatter()
nFormatter.numberStyle = .decimal

View File

@ -17,13 +17,13 @@ extension Timer {
}
/// EZSE: Run function after x seconds
public static func runThisAfterDelay(seconds: Double, after: @escaping () -> ()) {
public static func runThisAfterDelay(seconds: Double, after: @escaping () -> Void) {
runThisAfterDelay(seconds: seconds, queue: DispatchQueue.main, after: after)
}
//TODO: Make this easier
/// EZSwiftExtensions - dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0)
public static func runThisAfterDelay(seconds: Double, queue: DispatchQueue, after: @escaping ()->()) {
public static func runThisAfterDelay(seconds: Double, queue: DispatchQueue, after: @escaping () -> Void) {
let time = DispatchTime.now() + Double(Int64(seconds * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC)
queue.asyncAfter(deadline: time, execute: after)
}

View File

@ -12,7 +12,7 @@ import UIKit
private let DeviceList = [
/* iPod 5 */ "iPod5,1": "iPod Touch 5",
/* iPod 6 */ "iPod7,1": "iPod Touch 6",
/* iPhone 4 */ "iPhone3,1": "iPhone 4", "iPhone3,2": "iPhone 4", "iPhone3,3": "iPhone 4",
/* iPhone 4 */ "iPhone3,1": "iPhone 4", "iPhone3,2": "iPhone 4", "iPhone3,3": "iPhone 4",
/* iPhone 4S */ "iPhone4,1": "iPhone 4S",
/* iPhone 5 */ "iPhone5,1": "iPhone 5", "iPhone5,2": "iPhone 5",
/* iPhone 5C */ "iPhone5,3": "iPhone 5C", "iPhone5,4": "iPhone 5C",

View File

@ -91,7 +91,7 @@ extension UIFont {
return Font(.AvenirNext, type: .Regular, size: size)
}
//MARK: Deprecated
// MARK: Deprecated
/// EZSwiftExtensions
@available(*, deprecated: 1.8)

View File

@ -95,7 +95,7 @@ extension UIImageView {
}
}
//MARK: Deprecated 1.8
// MARK: Deprecated 1.8
/// EZSwiftExtensions
@available(*, deprecated: 1.8, renamed: "image(url:)")

View File

@ -6,30 +6,28 @@
// Copyright (c) 2015 Goktug Yilmaz. All rights reserved.
// swiftlint:disable trailing_whitespace
import UIKit
extension UIViewController {
// MARK: - Notifications
///EZSE: Adds an NotificationCenter with name and Selector
public func addNotificationObserver(_ name: String, selector: Selector) {
NotificationCenter.default.addObserver(self, selector: selector, name: NSNotification.Name(rawValue: name), object: nil)
}
///EZSE: Removes an NSNotificationCenter for name
public func removeNotificationObserver(_ name: String) {
NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: name), object: nil)
}
///EZSE: Removes NotificationCenter'd observer
public func removeNotificationObserver() {
NotificationCenter.default.removeObserver(self)
NotificationCenter.default.removeObserver(self)
}
#if os(iOS)
///EZSE: Adds a NotificationCenter Observer for keyboardWillShowNotification()
///
/// You also need to implement ```keyboardWillShowNotification(_ notification: Notification)```
@ -43,14 +41,14 @@ extension UIViewController {
public func addKeyboardDidShowNotification() {
self.addNotificationObserver(NSNotification.Name.UIKeyboardDidShow.rawValue, selector: #selector(UIViewController.keyboardDidShowNotification(_:)))
}
///EZSE: Adds a NotificationCenter Observer for keyboardWillHideNotification()
///
/// You also need to implement ```keyboardWillHideNotification(_ notification: Notification)```
public func addKeyboardWillHideNotification() {
self.addNotificationObserver(NSNotification.Name.UIKeyboardWillHide.rawValue, selector: #selector(UIViewController.keyboardWillHideNotification(_:)))
}
///EZSE: Adds a NotificationCenter Observer for keyboardDidHideNotification()
///
/// You also need to implement ```keyboardDidHideNotification(_ notification: Notification)```
@ -62,154 +60,145 @@ extension UIViewController {
public func removeKeyboardWillShowNotification() {
self.removeNotificationObserver(NSNotification.Name.UIKeyboardWillShow.rawValue)
}
///EZSE: Removes keyboardDidShowNotification()'s NotificationCenter Observer
public func removeKeyboardDidShowNotification() {
self.removeNotificationObserver(NSNotification.Name.UIKeyboardDidShow.rawValue)
}
///EZSE: Removes keyboardWillHideNotification()'s NotificationCenter Observer
public func removeKeyboardWillHideNotification() {
self.removeNotificationObserver(NSNotification.Name.UIKeyboardWillHide.rawValue)
}
///EZSE: Removes keyboardDidHideNotification()'s NotificationCenter Observer
public func removeKeyboardDidHideNotification() {
self.removeNotificationObserver(NSNotification.Name.UIKeyboardDidHide.rawValue)
}
public func keyboardDidShowNotification(_ notification: Notification) {
if let nInfo = (notification as NSNotification).userInfo, let value = nInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue {
let frame = value.cgRectValue
keyboardDidShowWithFrame(frame)
}
}
public func keyboardWillShowNotification(_ notification: Notification) {
if let nInfo = (notification as NSNotification).userInfo, let value = nInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue {
let frame = value.cgRectValue
keyboardWillShowWithFrame(frame)
}
}
public func keyboardWillHideNotification(_ notification: Notification) {
if let nInfo = (notification as NSNotification).userInfo, let value = nInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue {
let frame = value.cgRectValue
keyboardWillHideWithFrame(frame)
}
}
public func keyboardDidHideNotification(_ notification: Notification) {
if let nInfo = (notification as NSNotification).userInfo, let value = nInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue {
let frame = value.cgRectValue
keyboardDidHideWithFrame(frame)
}
}
public func keyboardWillShowWithFrame(_ frame: CGRect) {
}
public func keyboardDidShowWithFrame(_ frame: CGRect) {
}
public func keyboardWillHideWithFrame(_ frame: CGRect) {
}
public func keyboardDidHideWithFrame(_ frame: CGRect) {
}
//EZSE: Makes the UIViewController register tap events and hides keyboard when clicked somewhere in the ViewController.
public func hideKeyboardWhenTappedAround() {
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
tap.cancelsTouchesInView = false
view.addGestureRecognizer(tap)
}
public func hideKeyboardWhenTappedAroundAndCancelsTouchesInView() {
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
view.addGestureRecognizer(tap)
}
#endif
//EZSE: Dismisses keyboard
public func dismissKeyboard() {
view.endEditing(true)
}
// MARK: - VC Container
///EZSE: Returns maximum y of the ViewController
public var top: CGFloat {
get {
if let me = self as? UINavigationController, let visibleViewController = me.visibleViewController {
return visibleViewController.top
}
if let nav = self.navigationController {
if nav.isNavigationBarHidden {
return view.top
} else {
return nav.navigationBar.bottom
}
} else {
if let me = self as? UINavigationController, let visibleViewController = me.visibleViewController {
return visibleViewController.top
}
if let nav = self.navigationController {
if nav.isNavigationBarHidden {
return view.top
} else {
return nav.navigationBar.bottom
}
} else {
return view.top
}
}
///EZSE: Returns minimum y of the ViewController
public var bottom: CGFloat {
get {
if let me = self as? UINavigationController, let visibleViewController = me.visibleViewController {
return visibleViewController.bottom
}
if let tab = tabBarController {
if tab.tabBar.isHidden {
return view.bottom
} else {
return tab.tabBar.top
}
} else {
if let me = self as? UINavigationController, let visibleViewController = me.visibleViewController {
return visibleViewController.bottom
}
if let tab = tabBarController {
if tab.tabBar.isHidden {
return view.bottom
} else {
return tab.tabBar.top
}
} else {
return view.bottom
}
}
///EZSE: Returns Tab Bar's height
public var tabBarHeight: CGFloat {
get {
if let me = self as? UINavigationController, let visibleViewController = me.visibleViewController {
return visibleViewController.tabBarHeight
}
if let tab = self.tabBarController {
return tab.tabBar.frame.size.height
}
return 0
if let me = self as? UINavigationController, let visibleViewController = me.visibleViewController {
return visibleViewController.tabBarHeight
}
if let tab = self.tabBarController {
return tab.tabBar.frame.size.height
}
return 0
}
///EZSE: Returns Navigation Bar's height
public var navigationBarHeight: CGFloat {
get {
if let me = self as? UINavigationController, let visibleViewController = me.visibleViewController {
return visibleViewController.navigationBarHeight
}
if let nav = self.navigationController {
return nav.navigationBar.h
}
return 0
if let me = self as? UINavigationController, let visibleViewController = me.visibleViewController {
return visibleViewController.navigationBarHeight
}
if let nav = self.navigationController {
return nav.navigationBar.h
}
return 0
}
///EZSE: Returns Navigation Bar's color
public var navigationBarColor: UIColor? {
get {
@ -221,28 +210,24 @@ extension UIViewController {
navigationController?.navigationBar.barTintColor = value
}
}
///EZSE: Returns current Navigation Bar
public var navBar: UINavigationBar? {
get {
return navigationController?.navigationBar
}
return navigationController?.navigationBar
}
/// EZSwiftExtensions
public var applicationFrame: CGRect {
get {
return CGRect(x: view.x, y: top, width: view.w, height: bottom - top)
}
return CGRect(x: view.x, y: top, width: view.w, height: bottom - top)
}
// MARK: - VC Flow
///EZSE: Pushes a view controller onto the receivers stack and updates the display.
public func pushVC(_ vc: UIViewController) {
navigationController?.pushViewController(vc, animated: true)
}
///EZSE: Pops the top view controller from the navigation stack and updates the display.
public func popVC() {
_ = navigationController?.popViewController(animated: true)
@ -257,19 +242,19 @@ extension UIViewController {
public func presentVC(_ vc: UIViewController) {
present(vc, animated: true, completion: nil)
}
///EZSE: Dismisses the view controller that was presented modally by the view controller.
public func dismissVC(completion: (() -> Void)? ) {
dismiss(animated: true, completion: completion)
}
///EZSE: Adds the specified view controller as a child of the current view controller.
public func addAsChildViewController(_ vc: UIViewController, toView: UIView) {
self.addChildViewController(vc)
toView.addSubview(vc.view)
vc.didMove(toParentViewController: self)
}
///EZSE: Adds image named: as a UIImageView in the Background
public func setBackgroundImage(_ named: String) {
let image = UIImage(named: named)
@ -278,7 +263,7 @@ extension UIViewController {
view.addSubview(imageView)
view.sendSubview(toBack: imageView)
}
///EZSE: Adds UIImage as a UIImageView in the Background
@nonobjc func setBackgroundImage(_ image: UIImage) {
let imageView = UIImageView(frame: view.frame)

View File

@ -476,7 +476,7 @@ extension UIView {
}
/// EZSwiftExtensions - Make sure you use "[weak self] (gesture) in" if you are using the keyword self inside the closure or there might be a memory leak
public func addTapGesture(tapNumber: Int = 1, action: ((UITapGestureRecognizer) -> ())?) {
public func addTapGesture(tapNumber: Int = 1, action: ((UITapGestureRecognizer) -> Void)?) {
let tap = BlockTap(tapCount: tapNumber, fingerCount: 1, action: action)
addGestureRecognizer(tap)
isUserInteractionEnabled = true
@ -498,7 +498,7 @@ extension UIView {
}
/// EZSwiftExtensions - Make sure you use "[weak self] (gesture) in" if you are using the keyword self inside the closure or there might be a memory leak
public func addSwipeGesture(direction: UISwipeGestureRecognizerDirection, numberOfTouches: Int = 1, action: ((UISwipeGestureRecognizer) -> ())?) {
public func addSwipeGesture(direction: UISwipeGestureRecognizerDirection, numberOfTouches: Int = 1, action: ((UISwipeGestureRecognizer) -> Void)?) {
let swipe = BlockSwipe(direction: direction, fingerCount: numberOfTouches, action: action)
addGestureRecognizer(swipe)
isUserInteractionEnabled = true
@ -512,7 +512,7 @@ extension UIView {
}
/// EZSwiftExtensions - Make sure you use "[weak self] (gesture) in" if you are using the keyword self inside the closure or there might be a memory leak
public func addPanGesture(action: ((UIPanGestureRecognizer) -> ())?) {
public func addPanGesture(action: ((UIPanGestureRecognizer) -> Void)?) {
let pan = BlockPan(action: action)
addGestureRecognizer(pan)
isUserInteractionEnabled = true
@ -532,7 +532,7 @@ extension UIView {
#if os(iOS)
/// EZSwiftExtensions - Make sure you use "[weak self] (gesture) in" if you are using the keyword self inside the closure or there might be a memory leak
public func addPinchGesture(action: ((UIPinchGestureRecognizer) -> ())?) {
public func addPinchGesture(action: ((UIPinchGestureRecognizer) -> Void)?) {
let pinch = BlockPinch(action: action)
addGestureRecognizer(pinch)
isUserInteractionEnabled = true
@ -548,7 +548,7 @@ extension UIView {
}
/// EZSwiftExtensions - Make sure you use "[weak self] (gesture) in" if you are using the keyword self inside the closure or there might be a memory leak
public func addLongPressGesture(action: ((UILongPressGestureRecognizer) -> ())?) {
public func addLongPressGesture(action: ((UILongPressGestureRecognizer) -> Void)?) {
let longPress = BlockLongPress(action: action)
addGestureRecognizer(longPress)
isUserInteractionEnabled = true
@ -597,7 +597,7 @@ extension UIView {
}
}
//MARK: Fade Extensions
// MARK: Fade Extensions
private let UIViewDefaultFadeDuration: TimeInterval = 0.4