Skip to content

Instantly share code, notes, and snippets.

View ryantxr's full-sized avatar

Ryan Teixeira ryantxr

View GitHub Profile
<?php
class Adventurer {
public $class;
public $name;
public $inventory = [];
public $handler;
function __construct($name, $class, $handler=null) {
<?php
class Animal {
public $species;
public $numLegs;
public $name;
function __construct($species, $legs, $name) {
@ryantxr
ryantxr / timer.swift
Last active February 27, 2017 21:12
Use a Timer in Swift 3
// Swift 3
class MyController: UIViewController {
var timer : Timer?
var startDate: Date?
@IBOutlet weak var timerLabel: UILabel!
func startTimer() {
print("startTimer()")
let timeInterval = TimeInterval(1.0/10.0)
startDate = Date()
@ryantxr
ryantxr / swift3Dates.swift
Created December 21, 2016 03:40
Swift 3 date comparison
// Dates compared in Swift 3
let date1 = Date()
var date2 = Date()
let interval = TimeInterval(exactly: 11.0)
date2.addTimeInterval(interval!)
date1.compare(date2) == .orderedDescending // true if date1 is before date2
date1.compare(date2) == .orderedAscending // true if date1 is after date2
date1.compare(date2) == .orderedSame // true if date1 is equal to date2
@ryantxr
ryantxr / TextViewFileWrite.java
Last active December 14, 2016 05:38
Android write a TextView to a file
File path = context.getFilesDir();
// OR For external SD :
File path = context.getExternalFilesDir(null);
// create your file :
File file = new File(path, "FILENAME.txt");
// Get Strings from text view :
TextView t = (TextView)findViewById(R.id.mainText);
@ryantxr
ryantxr / Preg.swift
Created April 21, 2016 05:06
Regular Expression simplified wrapper
// Preg.match("[a-z]+", "alpha, beta, core")
// let newString = Preg.replace("([a-z]+)", "__$1__,", "alpha, beta, core")
class Preg {
/**
* regular expression matching.
*
*/
class func match(pattern: String, subject: String) -> [String] {
var mm = [String]()
@ryantxr
ryantxr / DictionaryHttpArgs.swift
Created April 19, 2016 14:48
Swift Dictionary extension to convert to HTTP arg string
protocol ArgType {}
extension String: ArgType {}
extension Dictionary where Key: ArgType, Value: ArgType {
func toHttpArgString() -> String {
var r = String()
for (n, v) in self {
if !r.isEmpty { r += "&" }
r += "\(n)=\(v)"
@ryantxr
ryantxr / StringRemoveChars.swift
Created April 5, 2016 14:34
Removing certain characters from a string
extension String {
// Modifies string in place
mutating func removeCharsInCharacterSet(charSet: NSCharacterSet) {
if let range = self.rangeOfCharacterFromSet(charSet, options: .LiteralSearch, range: nil) {
self.replaceRange(range, with: "")
}
}
// Creates a new string without modifying existing string
func stringByRemovingCharactersInCharacterSet(charSet: NSCharacterSet) -> String {
var newStr = self
@ryantxr
ryantxr / Operator.swift
Created March 31, 2016 00:19
Example of infix operator overload in Swift
struct Vector
{
var x: Double = 0
var y: Double = 0
init(x: Double, y: Double)
{
self.x = x
self.y = y
}
@ryantxr
ryantxr / UIView+Border.swift
Created March 26, 2016 07:44
Swift extension for iOS to allow setting borders
extension UIView {
public func addTopBorderWithColor(color: UIColor, width: CGFloat) {
let border = CALayer()
border.backgroundColor = color.CGColor
border.frame = CGRectMake(0, 0, self.frame.size.width, width)
self.layer.addSublayer(border)
}
public func addRightBorderWithColor(color: UIColor, width: CGFloat) {
let border = CALayer()