Skip to content

Instantly share code, notes, and snippets.

View sodastsai's full-sized avatar

Tien-Che Tsai sodastsai

View GitHub Profile
@sodastsai
sodastsai / objc.m
Last active November 8, 2016 21:27
ObjC Generic
@interface BaseViewModel : NSObject
- (void)baseMethod;
@end
@implementation BaseViewModel
- (void)baseMethod {}
@end
// ---------------------------------------------------------------------------------------------------------------------
@sodastsai
sodastsai / NSError Autorelease GG.m
Created October 13, 2016 00:14
Auto release pool and NSError
#import <Foundation/Foundation.h>
// Assume -fobjc-arc ... 假設有 ARC
//
// 存成 `main.m` 然後在 Terminal 跑 `clang -framework Foundation -fobjc-arc -o main main.m && ./main && rm main`
// 然後在 `./main` 就會動囉
//
NSArray *ggFunction1(NSError *__autoreleasing *error) {
@autoreleasepool {
@sodastsai
sodastsai / example1.m
Last active October 12, 2016 23:44
Auto release pool
#import <Foundation/Foundation.h>
// Assume -fno-objc-arc ... 假設沒 ARC
@interface MyObject: NSObject
@end
@implementation MyObject
@sodastsai
sodastsai / String append - interface.swift
Created October 3, 2016 15:41
Argument labels in Swift
struct String {
mutating func append(_ c: Character)
mutating func append(_ other: String)
}
@sodastsai
sodastsai / ViewController.swift
Last active October 3, 2016 15:32
Argument labels in Swift
viewController.dismiss(true) // bad
viewController.dismiss(animated: true)
viewController.dismissAnimated(true) // worse
@sodastsai
sodastsai / example.swift
Created October 3, 2016 15:21
Argument labels in Swift
let friends: [Friend] = ...
friends.remove(ted) // remove(_:)
friends.remove(at: positionOfTed) // remove(at:)
@sodastsai
sodastsai / 01-example.cpp
Created October 2, 2016 06:17
divide function for Swift's argument labels
#include <iostream>
typedef struct {
int quotient;
int remainder;
} DivideResult;
DivideResult divide(int dividend, int divisor) {
return (DivideResult){.quotient=dividend/divisor, .remainder=dividend%divisor};
}
@sodastsai
sodastsai / example.swift
Created October 2, 2016 06:05
Parameter Names and Argument Labels in functions of Swift
func move(from origin: String, to destination: String) -> String {
return "Move from \(origin) to \(destination)."
}
let string = move(from: "Tokyo", to: "Taipei")
print(string)
@sodastsai
sodastsai / example1.swift
Last active October 2, 2016 06:00
Parameter Names and Argument Labels in functions of Swift - incomplete labels
func move(from: String, to: String) -> String {
return "Move from \(from) to \(to)."
}
let string = move(from: "Tokyo", to: "Taipei")
print(string)
@sodastsai
sodastsai / Example.java
Last active October 2, 2016 05:58
Parameter Names and Argument Labels in functions of Swift - Example.java
public class Example {
static String move(String origin, String destination) {
return String.format("Move from %s to %s.", origin, destination);
}
public static void main(String[] args) {
String string = Example.move("Tokyo", "Taipei");
System.out.println(string);
}
}