Skip to content

Instantly share code, notes, and snippets.

@maxcampolo
maxcampolo / AnimatedStatusBar.swift
Created January 11, 2016 19:36
Animate the application status bar
func animateStatusBarHidden(hidden: Bool) {
let key = NSString(data: NSData(bytes: [0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x42, 0x61, 0x72] as [CUnsignedChar], length: 9), encoding: NSASCIIStringEncoding)
let object = UIApplication.sharedApplication()
var statusBar: UIView?
if let mKey = key as? String {
if object.respondsToSelector(NSSelectorFromString(mKey)) {
statusBar = object.valueForKey(mKey) as? UIView
}
}
@maxcampolo
maxcampolo / CALayer+BorderColor.swift
Created January 19, 2016 22:35
CALayer extension that adds borderUIColor as an attribute that can be set by key path in interface builder.
extension CALayer {
func borderUIColor() -> UIColor? {
return borderColor != nil ? UIColor(CGColor: borderColor!) : nil
}
func setBorderUIColor(color: UIColor) {
borderColor = color.CGColor
}
}
@maxcampolo
maxcampolo / UIColor+Hex.swift
Created February 18, 2016 20:54
UIColor extension for returning UIColor from hex code
// Initialize a color from hex string with format #RRGGBB
extension UIColor {
convenience init(hex: String, alpha: CGFloat = 1) {
assert(hex[hex.startIndex] == "#", "Expected hex string of format #RRGGBB")
let scanner = NSScanner(string: hex)
scanner.scanLocation = 1 // skip #
var rgb: UInt32 = 0
scanner.scanHexInt(&rgb)
@maxcampolo
maxcampolo / EquatableProtocol.swift
Created March 15, 2016 19:56
Implementation of the Equatable protocol in Swift on a custom object.
public class MyCustomObject: Equatable {}
// MARK: Equatable
/**
Evaluate equality as an identity check on an ObjectIdentifier constructed with an instance of our type. This is a public function in the global scope.
*/
public func ==(lhs: MyCustomObject, rhs: MyCustomObject) -> Bool {
return ObjectIdentifier(lhs) == ObjectIdentifier(rhs)
}
@maxcampolo
maxcampolo / ContainedClosure.swift
Created April 1, 2016 19:26
Call a function from within another function as a closure.
public someFunction() {
({ self.someStringVariable = "Some value" })()
}
@maxcampolo
maxcampolo / PerformSelector.swift
Created April 28, 2016 16:01
Performing selector after delay and cancelling previous perform requests in Swift.
// Perform selector
self.performSelector(#selector(MyViewController.myFunction(_:)), withObject: myObject, afterDelay: 1.0)
// Cancel perform requests
self.classForCoder.cancelPreviousPerformRequestsWithTarget(self, selector: #selector(MyViewController.myFunction(_:)), object: myObject)
// Note: -
// myObject can be nil
public override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
// Before rotation (willTransitionToInterfaceOrientation)
// ... Do stuff that should happen before rotation ... //
coordinator.animateAlongsideTransition({ (context) in
// During rotation
// ... Do stuff that should happen during rotation ... //
},
completion: { (context) in
// After rotation (didTransitionToInterfaceOrientation)
@maxcampolo
maxcampolo / BatchImageDownload.swift
Created June 21, 2016 14:53
Batch image download with AFNetworking using dispatch group
/**
Batch request an array of images for background download task. This method fires completion block when ALL images are downloaded and returns an array of UIImage objects.
- Parameter imageURLs: Array of strings representing location of images as URL's
- Parameter completion: Block that provides a dictionary of downloaded images as argument. The dictionary contains images at the index they were passed in the original array and NSNull objects for images that failed to download.
- Parameter imageScale: Scale to interpret image data for UIImage. A scale of 1.0 will keep full size of image. Default (imageScale = nil) is scale factor of UIScreen resolution.
*/
@objc
static func batchImageDownloadWithImageUrlArray(imageURLs: [String], imageScale: NSNumber?, completion: ((images: [Int : AnyObject]) -> Void)?) {
var imageDict = [Int : AnyObject]() // This should be UIImage? but Objective-C can't have nil in dictionaries
let manager = AFHTTPSessionManager()
@maxcampolo
maxcampolo / SwiftNotes.swift
Last active July 22, 2016 14:36
Just some notes about the intricacies of the swift language.
// 1.) Swift selectors must have @objc if they are not exposed to objective-c (i.e. they are private to a swift class)
// 2.) Make a failable initializer by adding an optional (?) after initializer (init?())
// 3.) To cast in a for in loop use "case". Use "where" to unwrap optionals or check a condition.
for case let button as AClass in view.subviews {
}
for view in childViews where view is UIButton {
}
for case let view as MyCustomView in self.view.subviews {
view.removeFromSuperview
}
@maxcampolo
maxcampolo / AutoIncrement.script
Last active August 8, 2016 17:30
Auto-increment target build number in Xcode.
#!/bin/bash
# Auto Increment Version Script
buildPlist=$INFOPLIST_FILE
echo $buildPlist
CFSVString=$(/usr/libexec/PlistBuddy -c "Print CFBundleShortVersionString" "$buildPlist")
CFBundleVersion=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "$buildPlist")
BUILD_NR=${CFBundleVersion##*.}
BUILD_NR=$(($BUILD_NR + 1))
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $BUILD_NR" "$buildPlist"