Code Monkey home page Code Monkey logo

dhfoundation's Introduction

DHFoundation

Common repository of useful iOS code.

DHNotificationStore

This is a mechanism to make dealing with NSNotificationCenter's block based methods simpler.

Instead of

// We need to hang on to this observer object to remove later
id observer = 
[[NSNotificationCenter defaultCenter] addObserverForName:DHNotificationName
                                                  object:nil
                                                   queue:[NSOperationQueue currentQueue]
                                              usingBlock:^(NSNotification *note) {
                                                  // Handle notification
                                              }];
[[NSNotificationCenter defaultCenter] removeObserver:observer];

DHNotificationStore wraps this, and using associated objects and weak references, allows the object lifecycle to handle removing the notification observer, leaving us with

[self.dh_notificationStore addObserverForName:DHNotificationName usingBlock:^(NSNotification *note) {
    // Handle notification
}];

DHConfiguration

I wish I could remember the blog post where I discovered this idea so I could credit it. If it was your idea, please let me know so I can.

DHConfiguration is a class for wrapping access to a plist configuration file. It uses the objective-c runtime to add accessor methods at runtime to read the class. Simply subclass DHConfiguration, add your own properties to your subclass of the correct type, and declare them as @dynamic in the implementation. Out of the box, this supports integers, floats, bools, any type natively supported by the plist format, as well as converting a string value to NSURL. It is trivial to extend to support further type conversion from string inputs in future.

DHReachabilityEventHandler

This is a simple way to handle reachability changes within a class. Via a category on NSObject, each class is given a dh_reachability class that exposes both a DHReachability object, which is simply Apple's Reachability class but namespaced, as well as a changed event handler block that will be fired whenever the reachability status changes.

UIViewController+DHPrepareForSegue

Category method to try to avoid endless if/else blocks in prepareForSegue:sender: methods. Simply call dh_prepareSegueWithIdentifier:destinationViewController:sourceViewController:sender within prepareForSegue:sender: and the method will try to call a method with the same name as the segue identifier. For example, if a segue within a view controller has the identifier showDetailViewController:, we can simply write

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    [self dh_prepareSegueWithIdentifier:segue.identifier
              destinationViewController:segue.destinationViewController
                   sourceViewController:segue.sourceViewController
                                 sender:sender];
}

- (void)showDetailViewController:(UIViewController *)viewController {
    // Configure the destination view controller in this method.
    // No need for if statements above.
}

Depending on the number of colon-separated sections in the selector name, the method above can pass different arguments to your method. Zero colons will pass no arguments, one will pass the destination, two will pass destination and source, and three will also pass the sender. The sender can be passed with fewer colons if there is a section called sender. For example

- (void)showDetailViewController:(UIViewController *)destination source:(UIViewController *)source {
}

will receive the destination and source view controllers, whereas

- (void)showDetailViewController:(UIViewController *)destination sender:(id)sender {
}

will receive the destination and sender.

Categories

There are various useful categories in this library, see the documentation for more info.

dhfoundation's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.