Skip to content

Instantly share code, notes, and snippets.

View Ben-G's full-sized avatar
💭
💻

Benjamin Encz Ben-G

💭
💻
View GitHub Profile
//: Playground - noun: a place where people can play
import Foundation
/*
Basic Monad according to definition in: http://stackoverflow.com/questions/27750046/is-a-collection-with-flatmap-a-monad
The requirements for being a monad
- A monad has to be a type constructor F[_] that takes one type argument. For example, F could be List, Function0, Option, etc.
@Ben-G
Ben-G / IsUnique.swift
Created May 30, 2015 15:48
isUniquelyReferencedNonObjC, simple example
import Foundation
class A { var x = "a" }
var a:A = A()
isUniquelyReferencedNonObjC(&a)
var b = a
@Ben-G
Ben-G / UnsafePointerPlayground.swift
Last active February 17, 2017 17:24
Playing with unsafe Pointers
//: Playground - noun: a place where people can play
import Foundation
let arr = [1,5,7,8]
let pointer = UnsafeMutablePointer<[Int]>.alloc(4)
pointer.initialize(arr)
let x = pointer.memory[3]
@Ben-G
Ben-G / PFObjectSubscriptExtension.swift
Created March 12, 2015 16:43
Workaround for: 'PFObject' does not have a member named 'subscript'
// Workaround for Parse 1.6.4 and Xcode 6.3, see: http://stackoverflow.com/questions/28953306/pfobject-does-not-have-a-member-named-subscript/29015269#29015269
extension PFObject {
subscript(index: String) -> AnyObject? {
get {
return self.valueForKey(index)
}
set(newValue) {
if let newValue: AnyObject = newValue {
self.setValue(newValue, forKey: index)
}
@Ben-G
Ben-G / DynamicInit.swift
Last active May 27, 2023 13:30
Dynamically create instance based on type in Swift
protocol Initializable {
init()
}
class A : Initializable {
var content:String
required init() {
content = "TestContent"
}
@Ben-G
Ben-G / RandomNumber.swift
Last active August 29, 2015 14:13
Swift Random Number Helper
/*
Generates a random number with upper bound of provided value.
Truncates upper bound to 32-Bit values.
*/
func randomInteger(var maximum: Int) -> Int {
if maximum > Int(Int32.max) {
maximum = Int(Int32.max)
}
return Int(arc4random_uniform(
@Ben-G
Ben-G / gist:571d17e39b51e565991e
Last active August 29, 2015 14:04
Fix for Cocos2D + MGWUSDK
-(void) applicationWillResignActive:(UIApplication *)application
{
[[CCDirector sharedDirector] pause];
}
-(void) applicationDidEnterBackground:(UIApplication*)application
{
[[CCDirector sharedDirector] stopAnimation];
}
@Ben-G
Ben-G / PlaySound.m
Last active August 29, 2015 13:55
Play sound in Cocos2d 3.0
OALSimpleAudio *audio = [OALSimpleAudio sharedInstance];
[audio playEffect:@"your_file_name"]
@Ben-G
Ben-G / ParticleEffect.m
Created January 9, 2014 19:39
Example of creating a particle effect in cocos2d 3.0
- (CCParticleSystem *)createParticleEffectInCode {
CCParticleSystem *emitter = [[CCParticleSystem alloc] initWithTotalParticles:50];
emitter.texture = [[CCTextureCache sharedTextureCache] addImage: @"stars-grayscale.png"];
// duration
emitter.duration = CCParticleSystemDurationInfinity;
// Gravity Mode: gravity
emitter.gravity = CGPointZero;
@Ben-G
Ben-G / RandomNumberGenerator.m
Last active December 25, 2015 01:29
Generates a set of unique random numbers. Useful when you want to pick multiple random elements without duplicates.
- (NSArray *)generate:(int)n randomUniqueNumbersBetween:(int)lowerLimit upperLimit:(int)upperLimit
{
NSMutableArray *randomNumberArray = [NSMutableArray arrayWithCapacity:upperLimit-lowerLimit];
// add all numbers to array
for (int i = lowerLimit; i < upperLimit; i++)
{
[randomNumberArray addObject:@(i)];
}