Skip to content

Instantly share code, notes, and snippets.

@mattt
Created November 26, 2012 13:59
Show Gist options
  • Save mattt/4148342 to your computer and use it in GitHub Desktop.
Save mattt/4148342 to your computer and use it in GitHub Desktop.
NSHipster Call for Tips & Tricks!

Howdy howdy, NSHipsters!

If you alloc init an NSCalendar, you'll notice that New Year's Eve falls on a Monday this year, a.k.a. "the day NSHipster is published every week". What fun!

So in celebration of the upcoming year++, I thought it'd be fun to compile a list of some of your favorite tips and tricks of the trade. Submit your favorite piece of Objective-C trivia, framework arcana, hidden Xcode feature, or anything else you think is cool, and you could have it featured in the year-end blowout article. Just comment on this gist below!

Here are a few examples of the kind of things I'd like to see:

Again, just comment on this Gist below with your submission.

Can't wait to see what y'all come up with!

@mattt
Copy link
Author

mattt commented Nov 29, 2012

These are all awesome, guys. Thanks for the great tips! Keep it up!

@0xced
Copy link

0xced commented Dec 3, 2012

See function static id _target(id autoContentAccessingProxy) for accessing a private ivar. You should obviously never ship your app with such code but it may be helpful for debugging.

@0xced
Copy link

0xced commented Dec 3, 2012

extern NSString *const and weak linking does’t work the way you think it works, from https://gist.github.com/3725528:

#import <Social/Social.h>

/* Compiled with iOS 6 SDK
 * Social.framework weak linked (Optional)
 * Run on iOS 5.1 (simulator and device)
 * Output is:
 * 0x0
 * About to crash
 * +++ EXC_BAD_ACCESS +++
 */

int main(int argc, char *argv[])
{
    @autoreleasepool
    {
        NSLog(@"%p", &SLServiceTypeFacebook);
        if (&SLServiceTypeFacebook)
        {
            NSLog(@"About to crash");
            NSString *serviceType = SLServiceTypeFacebook; // crashes here with EXC_BAD_ACCESS (code=2, address=0x0)
            NSLog(@"Not reached %@", serviceType);
        }
    }
    return 0;
}

@0xced
Copy link

0xced commented Dec 3, 2012

@0xced
Copy link

0xced commented Dec 3, 2012

Using NSPropertyListSerialization to implement base64 encoding and decoding in less than 50 lines with support for iOS 2.0 and OS X 10.5.

@SlaunchaMan
Copy link

How about associated objects? You know it’s going down when you see this at the top of one of my files:

#import <objc/runtime.h>

static const void *kMyKey = &kMyKey;

@shpakovski
Copy link

In iOS 6 you can animate a constant property in NSLayoutConstraint:

viewConstraint.constant = ConstantValueFrom;
[view layoutIfNeeded];

viewConstraint.constant = ConstantValueTo;
[view setNeedsUpdateConstraints];

[UIView animateWithDuration:ConstantAnimationDuration animations:^{
     [view layoutIfNeeded];
}];

@0xced
Copy link

0xced commented Dec 3, 2012

Printing NSCache usage information (tested on iOS only):

extern void cache_print(void *cache);

- (void) printCache:(NSCache *)cache
{
    cache_print(*((void **)(__bridge void *)cache + 3));
}

Use for debugging only!

@mattt
Copy link
Author

mattt commented Dec 31, 2012

Thanks again, everyone, for your great submissions! These have now been compiled into this special New Year's Eve Edition of NSHipster. Cheers!

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