Skip to content

Instantly share code, notes, and snippets.

Last active July 10, 2018 19:24
Show Gist options
  • Save natecook1000/151d8de423eb77fc87bf to your computer and use it in GitHub Desktop.
Save natecook1000/151d8de423eb77fc87bf to your computer and use it in GitHub Desktop.
NSHipster New Year's 2016

Greetings and salutations, NSHipsters!

As the year winds down, it's a tradition here at NSHipster to ask you, dear readers, to offer up your favorite tricks and tips from the past year as gifts to your fellow hipsters. With iOS 9, El Capitan, brand new watch- and tvOS's, and the open-sourcing of some minor Apple-related tech, there's bound to be lots to share.

Submit your favorite piece of Swift or @objc trivia, helpful hints, unexpected discoveries, useful workarounds, useless fascinations, or anything else you found cool this year. Just comment below!

If you need inspiration, try the list from last year, or from the year before, or from the year before that.

새해 복 많이 받으세요! 🎆

Copy link

With Extensions in iOS, it is critical that frameworks that can be linked to both Extensions and Apps be cognizant of their uses so they don't call any APIs that might not be available to an Extension (like UIApplication). Here's a function to help determine if you are running in an extension at runtime:

FOUNDATION_EXTERN BOOL NOBAmIRunningAsAnExtension() __attribute__((const));

BOOL NOBAmIRunningAsAnExtension()
    // Per Apple, an extension will have a top level NSExtension dictionary in the info.plist

    static BOOL sIsExtension;
    static dispatch_once_t sOnceToken;
    dispatch_once(& sOnceToken, ^{
        NSDictionary *extensionDictionary = [[NSBundle mainBundle] infoDictionary][@"NSExtension"];
        sIsExtension = [extensionDictionary isKindOfClass:[NSDictionary class]];
    return sIsExtension;

That frees you to do things like this:

- (void)startBackgroundTask
    if (!NOBAmIRunningAsAnExtension()) {
        Class UIApplicationClass = NSClassFromString(@"UIApplication");
        id sharedApplication = [UIApplicationClass sharedApplication];
        self.backgroundTaskIdentifier = [sharedApplication beginBackgroundTaskWithExpirationHandler:^{
            if (self.backgroundTaskIdentifier != UIBackgroundTaskInvalid) {
                [sharedApplication endBackgroundTask:self.backgroundTaskIdentifier];
                self.backgroundTaskIdentifier = UIBackgroundTaskInvalid;

- (void)endBackgroundTask
    if (self.backgroundTaskIdentifier != UIBackgroundTaskInvalid) {
        Class UIApplicationClass = NSClassFromString(@"UIApplication");
        id sharedApplication = [UIApplicationClass sharedApplication];
        [sharedApplication endBackgroundTask:self.backgroundTaskIdentifier];
        self.backgroundTaskIdentifier = UIBackgroundTaskInvalid;

Copy link

This macro for checking block nullability before executing them:

#define BLOCK_EXEC(block, ...) if (block) { block(__VA_ARGS__); };

Moves from this:

if (completionBlock)
    completionBlock(arg1, arg2);


BLOCK_EXEC(completionBlock, arg1, arg2);


Copy link

rayfix commented Dec 24, 2015

Using autolayout to toggle a label in a table view from one line to many:

    label.numberOfLines = label.numberOfLines == 0 ? 1 : 0

Copy link

In the console support po frame printing
译: 在控制台里支持 po frame 打印

open terminal :

  1. touch ~/.lldbinit
  2. echo display @import UIKit >> ~/.lldbinit
  3. echo target stop-hook add -o "target stop-hook disable" >> ~/.lldbinit

Copy link

vlat456 commented Dec 24, 2015

I did found it somewhere on stackowerflow.
For these, who like me, are trying to avoid the mess with -DDEBUG in Swift, but have to know, which version of executable is running, Debug or Release.

In code:

debugPrint("It's Debug build")
} else {
debugPrint("It's Release build")


include "PreProcessorMacros.h"

ifdef DEBUG






ifndef PreProcessorMacros_h

define PreProcessorMacros_h

include <Foundation/Foundation.h>

extern BOOL const DEBUG_BUILD;

endif /* PreProcessorMacros_h */

in Bridged header:

import "PreProcessorMacros.h"

Copy link

You can use enum and protocol extension to provide GCD convenience API:

protocol ExcutableQueue {
    var queue: dispatch_queue_t { get }

extension ExcutableQueue {
    func execute(closure: () -> Void) {
        dispatch_async(queue, closure)

enum Queue: ExcutableQueue {
    case Main
    case UserInteractive
    case UserInitiated
    case Utility
    case Background

    var queue: dispatch_queue_t {
        switch self {
        case .Main:
            return dispatch_get_main_queue()
        case .UserInteractive:
            return dispatch_get_global_queue(QOS_CLASS_USER_INTERACTIVE, 0)
        case .UserInitiated:
            return dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0)
        case .Utility:
            return dispatch_get_global_queue(QOS_CLASS_UTILITY, 0)
        case .Background:
            return dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0)

enum SerialQueue: String, ExcutableQueue {
    case DownLoadImage = "myApp.SerialQueue.DownLoadImage"
    case UpLoadFile = "myApp.SerialQueue.UpLoadFile"

    var queue: dispatch_queue_t {
        return dispatch_queue_create(rawValue, DISPATCH_QUEUE_SERIAL)

Download Something could write like this:

Queue.UserInitiated.execute {

    let url = NSURL(string: "http://image.jpg")!
    let data = NSData(contentsOfURL: url)!
    let image = UIImage(data: data)

    Queue.Main.execute {
        imageView.image = image

Copy link

The improved optional binding in Swift is amazing and I use it virtually everywhere now and avoid the pyramid of doom:

if let
    path     = NSBundle.mainBundle().pathForResource("users", ofType: "json"),
    url      = NSURL(fileURLWithPath: path),
    data     = NSData(contentsOfURL: url),
    userList = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: nil) as? [[String: AnyObject]] 
    // extract individual users
    for userDict in userList {
        if let
            id      = userDict["id"] as? Int,
            name    = userDict["name"] as? String,
            email   = userDict["email"] as? String,
            address = userDict["address"] as? [String: AnyObject]
            users.append(User(id: id, name: name, ...))

Copy link

Using Swift's _ObjectiveCBridgeable (implicit castability between types) to create a generic protocol for Obj-C compatible objects that wrap pure Swift structs (keeping Swift framework API clean, but Obj-C compatible for as long as desired), e.g.:

public protocol BackedObjectType: AnyObject {
    typealias Backing

    var backingObject: Backing { get }

    init(_ backingObject: Backing)

public protocol ObjectBackable: _ObjectiveCBridgeable {
    typealias Backed: BackedObjectType

public extension ObjectBackable where Backed.Backing == Self {
    static func _isBridgedToObjectiveC() -> Bool { return true }

    static func _getObjectiveCType() -> Any.Type { return Backed.self }

    func _bridgeToObjectiveC() -> Backed { return Backed(self) }

    static func _forceBridgeFromObjectiveC(source: Backed, inout result: Self?) { result = source.backingObject }

    static func _conditionallyBridgeFromObjectiveC(source: Backed, inout result: Self?) -> Bool {
        _forceBridgeFromObjectiveC(source, result: &result)
        return true

    func toBridgedObject() -> Backed { return _bridgeToObjectiveC() }

/////     /////     /////     /////     /////     /////     /////     /////     /////     /////     /////     /////     /////     /////     /////     /////     /////

public struct SomeModel {
    public let ID: Int
    public let name: String
    public let category: String

/////     /////     /////     /////     /////     /////     /////     /////     /////     /////     /////     /////     /////     /////     /////     /////     /////

extension SomeModel: ObjectBackable { public typealias Backed = M5SomeModel }

@objc public final class M5SomeModel: NSObject, BackedObjectType {
    public let backingObject: SomeModel
    public init(_ backingObject: SomeModel) { self.backingObject = backingObject }

    public var ID: Int { return backingObject.ID }
    public var name: String { return }
    public var category: String { return backingObject.category }

/////     /////     /////     /////     /////     /////     /////     /////     /////     /////     /////     /////     /////     /////     /////     /////     /////

let model = SomeModel(ID: 2, name: "awesome", category: "music")

let objcCompatibleModel = model as M5SomeModel

let originalModel = objcCompatibleModel as SomeModel

Copy link

The addition of the where clause has made my code simple and compact while remaining readable. In addition, it has a broad application in Swift, so that it can be applied in nearly any kind of control-flow statement, such as for loop, while loop, if, guard, switch, and even in extension declarations. One simple way I like to use it is in my prepareForSegue method:

if let segueID = segue.identifier where segueID == "mySegue" {

The combo of unwrapping and performing a condition check is most commonly where I use the where clause. The where clause is not going to change your life, but it should be an easy and useful addition to your Swift skills.

Copy link

jcavar commented Dec 30, 2015

Additional type safety with phantom types:

struct Kilometer {}
struct Meter {}

struct DistanceT<T> {
    private let value: Int

    init(value: Int) {
        self.value = value

func +<T>(left: DistanceT<T>, right: DistanceT<T>) -> DistanceT<T> {
    return DistanceT(value: left.value + right.value)

extension Int {
    var km: DistanceT<Kilometer> {
        return DistanceT<Kilometer>(value: self)
    var m: DistanceT<Meter> {
        return DistanceT<Meter>(value: self)

let distanceKilometers =
let distanceMeters = 15.m
let newDistance = distanceKilometers + distanceKilometers // Ok
let newDistance = distanceKilometers + distanceMeters // Compiler error

Copy link

orta commented Jan 2, 2016

Copy link

How to tell if your app is running under UI Tests:

UI Tests:

class UITests: TestCase {
    let app = XCUIApplication()

    override func setUp() {
        app.launchArguments = ["UI-TESTING"]

App Code:

func UITesting() -> Bool {
    return NSProcessInfo.processInfo().arguments.contains("UI-TESTING")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment