No more force-unwrapping 🤕
This commit is contained in:
parent
59c537d9eb
commit
050106afd8
|
@ -50,8 +50,8 @@ public class BlockButton: UIButton {
|
|||
defaultInit()
|
||||
}
|
||||
|
||||
public required init(coder aDecoder: NSCoder) {
|
||||
super.init(coder: aDecoder)!
|
||||
public required init?(coder aDecoder: NSCoder) {
|
||||
super.init(coder: aDecoder)
|
||||
}
|
||||
|
||||
private func defaultInit() {
|
||||
|
@ -82,12 +82,15 @@ public class BlockButton: UIButton {
|
|||
highlightLayer.frame = layer.bounds
|
||||
highlightLayer.backgroundColor = UIColor.blackColor().CGColor
|
||||
highlightLayer.opacity = 0.5
|
||||
var maskImage: UIImage? = nil
|
||||
UIGraphicsBeginImageContextWithOptions(layer.bounds.size, false, 0)
|
||||
layer.renderInContext(UIGraphicsGetCurrentContext()!)
|
||||
let maskImage = UIGraphicsGetImageFromCurrentImageContext()
|
||||
if let context = UIGraphicsGetCurrentContext() {
|
||||
layer.renderInContext(context)
|
||||
maskImage = UIGraphicsGetImageFromCurrentImageContext()
|
||||
}
|
||||
UIGraphicsEndImageContext()
|
||||
let maskLayer = CALayer()
|
||||
maskLayer.contents = maskImage.CGImage
|
||||
maskLayer.contents = maskImage?.CGImage
|
||||
maskLayer.frame = highlightLayer.frame
|
||||
highlightLayer.mask = maskLayer
|
||||
layer.addSublayer(highlightLayer)
|
||||
|
|
|
@ -20,8 +20,8 @@ public class BlockWebView: UIWebView, UIWebViewDelegate {
|
|||
delegate = self
|
||||
}
|
||||
|
||||
public required init(coder aDecoder: NSCoder) {
|
||||
super.init(coder: aDecoder)!
|
||||
public required init?(coder aDecoder: NSCoder) {
|
||||
super.init(coder: aDecoder)
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -57,12 +57,12 @@ public struct ez {
|
|||
|
||||
/// EZSwiftExtensions
|
||||
public static var horizontalSizeClass: UIUserInterfaceSizeClass {
|
||||
return self.topMostVC?.traitCollection.horizontalSizeClass ?? UIUserInterfaceSizeClass(rawValue: 0)!
|
||||
return self.topMostVC?.traitCollection.horizontalSizeClass ?? UIUserInterfaceSizeClass.Unspecified
|
||||
}
|
||||
|
||||
/// EZSwiftExtensions
|
||||
public static var verticalSizeClass: UIUserInterfaceSizeClass {
|
||||
return self.topMostVC?.traitCollection.verticalSizeClass ?? UIUserInterfaceSizeClass(rawValue: 0)!
|
||||
return self.topMostVC?.traitCollection.verticalSizeClass ?? UIUserInterfaceSizeClass.Unspecified
|
||||
}
|
||||
|
||||
/// EZSE: Returns screen width
|
||||
|
@ -190,8 +190,13 @@ public struct ez {
|
|||
|
||||
/// EZSE:
|
||||
private static func requestURL(url: String, success: (NSData?) -> Void, error: ((NSError) -> Void)? = nil) {
|
||||
guard let requestURL = NSURL(string: url) else {
|
||||
assertionFailure("EZSwiftExtensions Error: Invalid URL")
|
||||
return
|
||||
}
|
||||
|
||||
NSURLSession.sharedSession().dataTaskWithRequest(
|
||||
NSURLRequest(URL: NSURL (string: url)!),
|
||||
NSURLRequest(URL: requestURL),
|
||||
completionHandler: { data, response, err in
|
||||
if let e = err {
|
||||
error?(e)
|
||||
|
|
|
@ -83,10 +83,15 @@ extension String {
|
|||
|
||||
let text = self
|
||||
|
||||
detector!.enumerateMatchesInString(text, options: [], range: NSMakeRange(0, text.characters.count), usingBlock: {
|
||||
(result: NSTextCheckingResult?, flags: NSMatchingFlags, stop: UnsafeMutablePointer<ObjCBool>) -> Void in
|
||||
urls.append(result!.URL!)
|
||||
})
|
||||
if let detector = detector {
|
||||
detector.enumerateMatchesInString(text, options: [], range: NSMakeRange(0, text.characters.count), usingBlock: {
|
||||
(result: NSTextCheckingResult?, flags: NSMatchingFlags, stop: UnsafeMutablePointer<ObjCBool>) -> Void in
|
||||
if let result = result,
|
||||
let url = result.URL {
|
||||
urls.append(url)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
return urls
|
||||
}
|
||||
|
|
|
@ -38,8 +38,8 @@ private let DeviceList = [
|
|||
|
||||
extension UIDevice {
|
||||
/// EZSwiftExtensions
|
||||
public class func idForVendor() -> String {
|
||||
return UIDevice.currentDevice().identifierForVendor!.UUIDString
|
||||
public class func idForVendor() -> String? {
|
||||
return UIDevice.currentDevice().identifierForVendor?.UUIDString
|
||||
}
|
||||
|
||||
/// EZSwiftExtensions - Operating system name
|
||||
|
|
|
@ -10,21 +10,19 @@ import UIKit
|
|||
|
||||
extension UIImage {
|
||||
/// EZSE: Returns compressed image to rate from 0 to 1
|
||||
public func compressImage(rate rate: CGFloat) -> NSData {
|
||||
let compressedImage = UIImageJPEGRepresentation(self, rate)
|
||||
return compressedImage!
|
||||
public func compressImage(rate rate: CGFloat) -> NSData? {
|
||||
return UIImageJPEGRepresentation(self, rate)
|
||||
}
|
||||
|
||||
/// EZSE: Returns Image size in Bytes
|
||||
public func getSizeAsBytes() -> Int {
|
||||
let imageData = NSData(data: UIImageJPEGRepresentation(self, 1)!)
|
||||
return imageData.length
|
||||
return UIImageJPEGRepresentation(self, 1)?.length ?? 0
|
||||
}
|
||||
|
||||
/// EZSE: Returns Image size in Kylobites
|
||||
public func getSizeAsKilobytes() -> Int {
|
||||
let imageData = NSData(data: UIImageJPEGRepresentation(self, 1)!)
|
||||
return imageData.length / 1024
|
||||
let sizeAsBytes = getSizeAsBytes()
|
||||
return sizeAsBytes != 0 ? sizeAsBytes / 1024 : 0
|
||||
}
|
||||
|
||||
/// EZSE: scales image
|
||||
|
|
|
@ -22,7 +22,7 @@ extension UIImageView {
|
|||
if image != nil {
|
||||
scaleImageFrameToWidth(width: scaleToWidth)
|
||||
} else {
|
||||
assertionFailure("EZSwiftExtensions Error: The imageName is invalid!!!")
|
||||
assertionFailure("EZSwiftExtensions Error: The imageName is invalid.")
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -41,25 +41,25 @@ extension UIImageView {
|
|||
|
||||
/// EZSwiftExtensions, scales this ImageView size to fit the given width
|
||||
public func scaleImageFrameToWidth(width width: CGFloat) {
|
||||
guard image != nil else {
|
||||
guard let image = image else {
|
||||
print("EZSwiftExtensions Error: The image is not set yet!")
|
||||
return
|
||||
}
|
||||
let widthRatio = image!.size.width / width
|
||||
let newWidth = image!.size.width / widthRatio
|
||||
let newHeigth = image!.size.height / widthRatio
|
||||
let widthRatio = image.size.width / width
|
||||
let newWidth = image.size.width / widthRatio
|
||||
let newHeigth = image.size.height / widthRatio
|
||||
frame = CGRect(x: frame.origin.x, y: frame.origin.y, width: newWidth, height: newHeigth)
|
||||
}
|
||||
|
||||
/// EZSwiftExtensions, scales this ImageView size to fit the given height
|
||||
public func scaleImageFrameToHeight(height height: CGFloat) {
|
||||
guard image != nil else {
|
||||
guard let image = image else {
|
||||
print("EZSwiftExtensions Error: The image is not set yet!")
|
||||
return
|
||||
}
|
||||
let heightRatio = image!.size.height / height
|
||||
let newHeight = image!.size.height / heightRatio
|
||||
let newWidth = image!.size.width / heightRatio
|
||||
let heightRatio = image.size.height / height
|
||||
let newHeight = image.size.height / heightRatio
|
||||
let newWidth = image.size.width / heightRatio
|
||||
frame = CGRect(x: frame.origin.x, y: frame.origin.y, width: newWidth, height: newHeight)
|
||||
}
|
||||
|
||||
|
|
|
@ -122,8 +122,9 @@ extension UIViewController {
|
|||
/// EZSwiftExtensions
|
||||
public var top: CGFloat {
|
||||
get {
|
||||
if let me = self as? UINavigationController {
|
||||
return me.visibleViewController!.top
|
||||
if let me = self as? UINavigationController,
|
||||
let visibleViewController = me.visibleViewController {
|
||||
return visibleViewController.top
|
||||
}
|
||||
if let nav = self.navigationController {
|
||||
if nav.navigationBarHidden {
|
||||
|
@ -140,8 +141,9 @@ extension UIViewController {
|
|||
/// EZSwiftExtensions
|
||||
public var bottom: CGFloat {
|
||||
get {
|
||||
if let me = self as? UINavigationController {
|
||||
return me.visibleViewController!.bottom
|
||||
if let me = self as? UINavigationController,
|
||||
let visibleViewController = me.visibleViewController {
|
||||
return visibleViewController.bottom
|
||||
}
|
||||
if let tab = tabBarController {
|
||||
if tab.tabBar.hidden {
|
||||
|
@ -158,8 +160,9 @@ extension UIViewController {
|
|||
/// EZSwiftExtensions
|
||||
public var tabBarHeight: CGFloat {
|
||||
get {
|
||||
if let me = self as? UINavigationController {
|
||||
return me.visibleViewController!.tabBarHeight
|
||||
if let me = self as? UINavigationController,
|
||||
let visibleViewController = me.visibleViewController {
|
||||
return visibleViewController.tabBarHeight
|
||||
}
|
||||
if let tab = self.tabBarController {
|
||||
return tab.tabBar.frame.size.height
|
||||
|
@ -171,8 +174,9 @@ extension UIViewController {
|
|||
/// EZSwiftExtensions
|
||||
public var navigationBarHeight: CGFloat {
|
||||
get {
|
||||
if let me = self as? UINavigationController {
|
||||
return me.visibleViewController!.navigationBarHeight
|
||||
if let me = self as? UINavigationController,
|
||||
let visibleViewController = me.visibleViewController {
|
||||
return visibleViewController.navigationBarHeight
|
||||
}
|
||||
if let nav = self.navigationController {
|
||||
return nav.navigationBar.h
|
||||
|
@ -184,8 +188,9 @@ extension UIViewController {
|
|||
/// EZSwiftExtensions
|
||||
public var navigationBarColor: UIColor? {
|
||||
get {
|
||||
if let me = self as? UINavigationController {
|
||||
return me.visibleViewController!.navigationBarColor
|
||||
if let me = self as? UINavigationController,
|
||||
let visibleViewController = me.visibleViewController {
|
||||
return visibleViewController.navigationBarColor
|
||||
}
|
||||
return navigationController?.navigationBar.tintColor
|
||||
} set(value) {
|
||||
|
|
|
@ -231,20 +231,22 @@ extension UIView {
|
|||
|
||||
/// EZSE: Centers view in superview horizontally
|
||||
public func centerXInSuperView() {
|
||||
if self.superview != nil {
|
||||
self.x = self.superview!.w/2 - self.w/2
|
||||
} else {
|
||||
assertionFailure("Your view doesn't have a superview")
|
||||
guard let parentView = superview else {
|
||||
assertionFailure("EZSwiftExtensions Error: The view \(self) doesn't have a superview")
|
||||
return
|
||||
}
|
||||
|
||||
self.x = parentView.w/2 - self.w/2
|
||||
}
|
||||
|
||||
/// EZSE: Centers view in superview vertically
|
||||
public func centerYInSuperView() {
|
||||
if self.superview != nil {
|
||||
self.y = self.superview!.h/2 - self.h/2
|
||||
} else {
|
||||
assertionFailure("Your view doesn't have a superview")
|
||||
guard let parentView = superview else {
|
||||
assertionFailure("EZSwiftExtensions Error: The view \(self) doesn't have a superview")
|
||||
return
|
||||
}
|
||||
|
||||
self.y = parentView.h/2 - self.h/2
|
||||
}
|
||||
|
||||
/// EZSE: Centers view in superview horizontally & vertically
|
||||
|
@ -381,7 +383,6 @@ extension UIView {
|
|||
shapeLayer.lineWidth = width
|
||||
self.layer.addSublayer(shapeLayer)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private let UIViewAnimationDuration: NSTimeInterval = 1
|
||||
|
@ -540,7 +541,6 @@ extension UIView {
|
|||
public func roundView() {
|
||||
self.layer.cornerRadius = min(self.frame.size.height, self.frame.size.width) / 2
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
extension UIView {
|
||||
|
@ -560,15 +560,14 @@ extension UIView {
|
|||
|
||||
self.layer.addAnimation(anim, forKey: nil)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
extension UIView {
|
||||
///EZSE: Loops until it finds the top root view. //TODO: Add to readme
|
||||
func rootView() -> UIView {
|
||||
guard superview != nil else {
|
||||
guard let parentView = superview else {
|
||||
return self
|
||||
}
|
||||
return superview!.rootView()
|
||||
return parentView.rootView()
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue