Skip to content

Instantly share code, notes, and snippets.

View lerno's full-sized avatar

Christoffer Lerno lerno

View GitHub Profile
extension GameCenter
{
struct NotificationKey
{
static let Error = "error"
static let Achievement = "achievement"
}
struct Notification
{
@lerno
lerno / gist:bd70286ca7c9bc5e5f7d
Created August 17, 2014 13:43
Trivial method for setting properties
- (void)setProperties:(NSDictionary *)keyToPropertyDictionary fromDictionary:(NSDictionary *)properties
{
for (id key in [keyToPropertyDictionary keyEnumerator])
{
id value = properties[key];
if (!value) continue;
value = [NSNull null] == value ? nil : value;
id realKey = keyToPropertyDictionary[key];
NSError *error = nil;
if (![self validateValue:&value forKey:realKey error:&error])
@lerno
lerno / gist:d6305411fe95410d68c5
Last active August 29, 2015 14:05
Object serialization
#import <objc/runtime.h>
@interface TestObject : NSObject
@property (nonatomic, strong) NSString *aString;
@property (nonatomic, assign) int aNumber;
@property (nonatomic, strong) NSArray *anArray;
@property (nonatomic, strong) NSDictionary *aDict;
@property (nonatomic, strong) TestObject *childObject;
@end
class Bar : NSObject
{
var ref : (()->Bool)?
deinit { println("Bye from bar!") }
}
class Foo : NSObject
{
var bar : Bar
override init()
//C
unsigned char result[CC_MD5_DIGEST_LENGTH];
const char *cString = [self UTF8String];
CC_MD5(cString, (CC_LONG)strlen(cString), result);
return bytesToHexString(result, CC_MD5_DIGEST_LENGTH);
//Swift
let digestLength = 16
var result = Array<UInt8>(count: digestLength, repeatedValue: 0)
let cString = cStringUsingEncoding(NSUTF8StringEncoding)!
@lerno
lerno / Blocks
Last active August 29, 2015 14:14
// Synchronous
@implementation A
{
B *b;
}
- (void)doSomething {
/* ... */
BOOL x = [b performOne];
if (x) [b performTwo];
@lerno
lerno / gist:6d820600fbf30797ccf4
Last active August 29, 2015 14:15
Implicit unwrap vs optionals - trivial example to illustrate implicit conversion from "possibly null" to "not null" with annotations.
// Assume we also have the following:
void doSomethingWithFoo(@NotNull foo) { ... }
void doSomethingNull() { ... }
void doSomethingMore() { ... }
void doSomething(@Nullable Foo foo)
{
// doSomethingWithFoo(foo); <- this would have been an error in the IDE
// Since foo is @Nullable at this point
if (foo == null)
@lerno
lerno / macro.c
Created November 6, 2018 21:48
Macro proposal
// 1. Macros with arguments
macro @foo(int &v) {
v++;
if (v > 10) return 10;
return v;
}
int bar() {
int a = 10;
@foo(a);
@lerno
lerno / macro2.c
Created November 6, 2018 22:29
Macro proposal II
// Switch
macro char * @foo(&x) {
$switch (@typeof(x)) {
$case int: return "int";
$case float: return "float";
$default: return "???";
}
}
char *x = @foo(10); // char *x = "int";
// Copyright (c) 2020 Christoffer Lerno. All rights reserved.
// Use of this source code is governed by a LGPLv3.0
// a copy of which can be found in the LICENSE file.
#include "vmem.h"
#include "common.h"
#if defined( _WIN32 ) || defined( __WIN32__ ) || defined( _WIN64 )
#define PLATFORM_WINDOWS 1
#define PLATFORM_POSIX 0