Skip to content

Instantly share code, notes, and snippets.

View McZonk's full-sized avatar

Max McZonk

  • Germany
View GitHub Profile
@McZonk
McZonk / gist:4556864
Created January 17, 2013 15:43
instancetype benefits
@interface Octopus : NSObject
+ (instancetype)octopus;
- (instancetype)init;
@property (nonatomic, assign) int legs;
@end
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
NotificationCell* cell = [tableView dequeueReusableCellWithIdentifier:NotificationCell.defaultReuseIdentifier forIndexPath:indexPath];
cell.notification = self.notifications[indexPath.row];
return cell;
}
//
// UIPCenteredScrollView.m
// Plus
//
// Created by Maximilian Christ on 5/30/12.
// Copyright (c) 2012 mczonk.de. All rights reserved.
//
#import "UIPCenteredScrollView.h"
- (void)myMethod {
NSError* error = [NSError errorWithDomain:@"myDomain" code:42 userInfo:@{ @"Foo": @"Bar" }];
NSString* file = [NSBundle.mainBundle pathForResource:@"Info" ofType:@"plist"];
NSData* data = [NSData dataWithContentsOfFile:file options:0 error:&error];
// safe
if(data == nil) {
// only access error if data was nil
#import <Foundation/Foundation.h>
@interface MyObject : NSObject
- (id)initWithName:(NSString*)name;
@property (nonatomic, retain) NSString* name;
@end
#import <Foundation/Foundation.h>
@interface MyObject : NSObject
- (id)initWithName:(NSString*)name;
@property (nonatomic, retain) NSString* name;
@end
@McZonk
McZonk / No-Default.c
Created April 17, 2013 10:55
If you use switch(enum) you should not implement default: You will get a helpful warning when a value is not implemented. This is useful when new values will be added to the enum. You don't forget to handle the new value because of the warning. When a value doesn't need to be handle, add it at the end of the switch with a break.
typedef enum MyEnum {
MyEnum1,
MyEnum2,
MyEnum3,
} MyEnum;
static int function1(MyEnum e) {
switch(e) { // warning: Enumeration value 'MyEnum3' not handled in switch
case MyEnum1:
return 1;
@McZonk
McZonk / main.m
Created April 23, 2013 13:48
Dispatch Behavior
int main(int argc, const char * argv[])
{
@autoreleasepool {
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_block_t block = ^{
int a = 5;
NSLog(@"%p = %d", &a, a);
@McZonk
McZonk / gist:5516682
Last active December 16, 2015 23:49
overriding self might seem strange, but it ensure that you cannot accidentally use the strong self.
- (void)foo
{
__weak __typeof__(self) weakSelf = self;
dispatch_async(queue, ^{
__typeof__(self) self = weakSelf;
[self bar];
});
}
@implementation IrisSegue
- (void)perform
{
CATransition *shutterAnimation = [CATransition animation];
[shutterAnimation setDelegate:self];
[shutterAnimation setDuration:0.5];
shutterAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
[shutterAnimation setType:@"cameraIris"];