Skip to content

Instantly share code, notes, and snippets.

@maxcampolo
Created August 10, 2016 14:58
Show Gist options
  • Save maxcampolo/be7db6b96e628e47083c070dd91044dd to your computer and use it in GitHub Desktop.
Save maxcampolo/be7db6b96e628e47083c070dd91044dd to your computer and use it in GitHub Desktop.
Global constants with swift extensions and Implicit Member Expression.
extension Double {
public static let kRectX = 30.0
public static let kRectY = 30.0
public static let kRectWidth = 30.0
public static let kRectHeight = 30.0
}
public func makeRect() -> CGRect {
return CGRect(x: .kRectX, y: .kRectY, width: .kRectWidth, height: .kRectHeight)
}
extension String {
// Notifications
public static let myCustomNotification = "myCustomNotification"
// URL paths
public static let myURLPath = "https://www.google.com/"
public static func myOtherURLPath() -> String { return "https://www.google.com" }
}
extension NSURL {
public static let myURL = NSURL(string: .myURLPath)
public static let myOtherURL = NSURL(string: .myOtherURLPath())
public static func myThirdURL() -> NSURL { return NSURL(string: .myURLPath)! }
}
public func postNotification () {
NSNotificationCenter.defaultCenter().postNotificationName(.myCustomNotification, object: nil)
}
public func makeRequest() {
// Can't omit type with just string
let request = NSMutableURLRequest(URL: NSURL.myURL!)
let otherReqeust = NSMutableURLRequest(URL: NSURL.myOtherURL!)
// Can omit with func!
let thirdRequest = NSMutableURLRequest(URL: .myThirdURL())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment