Skip to content

Instantly share code, notes, and snippets.

View qnoid's full-sized avatar
💭
I have been raised by women. My single mother, my grandma, my godmother.

Markos Zoulias Charatzas qnoid

💭
I have been raised by women. My single mother, my grandma, my godmother.
View GitHub Profile
@qnoid
qnoid / AppDelegate.swift
Last active June 16, 2017 19:24
This is a sample project to demonstrate how you can use a computed property to create a reusable function ("closure") that mimics a constant value. Think of it as a way to encapsulate both the data and the function it applies to.
//
// AppDelegate.swift
// Keychain
//
// Created by Markos Charatzas on 07/09/2014.
// Copyright (c) 2014 qnoid.com. All rights reserved.
//
import Cocoa
@qnoid
qnoid / SuperclassCallsOverridableMethod.playground
Last active August 9, 2017 11:43
Swift ensures that all properties of a derived class are initialised before calling its base class. The compiler will generate a warning. Just move the statement below the assignment to remove the warning. Compared to Java, this is safe. See: http://stackoverflow.com/questions/3404301/whats-wrong-with-overridable-method-calls-in-constructors
import Cocoa
class Plane
{
init()
{
readyToTakeOff();
}
func readyToTakeOff(){
@interface Foo()
@property(nonatomic, strong) dispatch_group_t lock;
@end
@implementation Foo
-(id)init
{
self = [super init];
@qnoid
qnoid / main.m
Created November 12, 2013 14:53
To quickly try a snippet of code in objective-c using clang from the command line. 1. Create a "main.m" file (see example below) 2. Compile using "clang -o main main.m -lobjc -framework Foundation" 3. Run "./main"
#import <Foundation/Foundation.h>
int main(int argc, char *argv[])
{
NSArray *array = @[ @"a", @"b", @"c" ];
for (NSUInteger i = 0; i < array.count; i++)
{
NSLog(@"%@", array[i]);
}
@qnoid
qnoid / QNDUnit.m
Last active December 26, 2015 18:38
An example that shows the do's and don't's on a method contract. Summary, DON'T code defensively DO add precondition checks DO add runtime checks DON'T be paranoid
@implementation QNDUnit
- (id)initWithCalculator:(QNDCalculator*) calculator
{
self = [super init];
if (self) {
_calculator = calculator;
}
@qnoid
qnoid / gist:6140282
Created August 2, 2013 14:29
Animate an UIBarButtonItem that has been assigned a UIButton as its customview. The UIButton uses an image that is mirrored on the X axis hence appear as if it's rotating.
-(IBAction)tap:(id)sender
{
UIBarButtonItem *refresh = self.navigationItem.rightBarButtonItem;
CALayer *layer = ((UIButton*)refresh.customView).imageView.layer;
CABasicAnimation *rotate = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotate.timingFunction = [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionLinear];
rotate.fromValue = @(0);
rotate.toValue = @(2 * M_PI);
public class Main
{
static class Number
{
final int value;
public Number(int value) {
this.value = value;
}
}
@qnoid
qnoid / ApplicationScopeTest.java
Last active December 18, 2015 15:39
Related gists: ApplicationScope.java https://gist.github.com/qnoid/5805659
import org.junit.Test;
import org.junit.Assert;
import java.util.function.Consumer;
public class ApplicationScopeTest
{
@Test(expected=RuntimeException.class)
public void testApplicationScope() throws Exception
{
ApplicationScope applicationScope = new ApplicationScope();
import java.util.function.Consumer;
import java.util.function.Supplier;
public class ApplicationScope
{
private volatile String value;
private final Supplier<String> supplier;
@qnoid
qnoid / gist:5269777
Last active December 15, 2015 13:49
How to use [diagnostic pragmas][1] to annotate a class/method so as to show a message on compile. [1]: http://gcc.gnu.org/onlinedocs/gcc/Diagnostic-Pragmas.html PS. Tested on XCode 4.6.1 http://i.minus.com/iepXiFcByxuKt.png
#define DO_PRAGMA(x) _Pragma (#x)
#define MESSAGE(x) DO_PRAGMA(message (x))
@interface TBFoo : NSObject
@end
@implementation TBFoo
-(void)bar MESSAGE("Hello World!"){}