Skip to content

Instantly share code, notes, and snippets.

View ryantxr's full-sized avatar

Ryan Teixeira ryantxr

View GitHub Profile
@ryantxr
ryantxr / Logger.php
Last active May 7, 2019 20:53
Super simple logger for php. For those times when you are just throwing something together for a quick hack.
<?php
class Logger {
static function debug($msg) {
$file = dirname(__FILE__) . "/debug.log";
$output = strftime('%Y-%m-%d %T ') . ":" . $msg . "\n";
file_put_contents($file, $output, FILE_APPEND);
}
}
@ryantxr
ryantxr / ExceptionTestSwift.swift
Last active March 11, 2016 17:40
Example swift exception throw and catch.
enum ArrayError : ErrorType{
case OutOfBounds
}
class SomeClass {
var test: NSArray! = ["Test1", "Test2"]
func testCode() {
<?php
// DATABASE-HOSTNAME-OR-IPADDRESS-GOES-HERE
// MYSQL-DBNAME-GOES-HERE
class LoginHandler {
public $dbHostname = 'DATABASE-HOSTNAME-OR-IPADDRESS-GOES-HERE';
public $dbDatabaseName = 'MYSQL-DBNAME-GOES-HERE';
public $user = 'DATABASE_USERNAME';
public $password = 'DATABASE_PASSWORD';
//public $port = 3307;
public $message;
@ryantxr
ryantxr / StringIndexSubstring.swift
Created March 11, 2016 23:01
Get substring for a string by searching for a character
// Given the string "Fox is in the hole", how to get a substring up to the first space.
let myMessage = "Fox is in the hole"
var result: String?
if let index = myMessage.characters.indexOf(" ") {
result = myMessage.substringToIndex(index)
}
print(result)
// Approach #1
let fruit = ["012Apple", "03218Banana", "11 Orange"]
var stringsOnly: [String] = []
for f in fruit {
stringsOnly.append(f.stringByTrimmingCharactersInSet(NSCharacterSet.letterCharacterSet().invertedSet))
}
// Approach #2
let fruits = ["012Apple", "03218Banana", "11 Orange"]
var stringOnly = [String]()
@ryantxr
ryantxr / LocalNotification.swift
Created March 19, 2016 22:44
Create and cancel a local notification on iOS / Swift
class LookAtMeNotification {
func createNotification(key: String) {
let notification = UILocalNotification()
notification.fireDate = NSDate(timeIntervalSinceNow: 5)
notification.repeatInterval = NSCalendarUnit.Minute
notification.alertBody = "Please Look at me"
notification.soundName = UILocalNotificationDefaultSoundName
notification.userInfo = ["ID": key]
@ryantxr
ryantxr / PrintDates.swift
Last active March 23, 2016 14:25
Date ranges with NSCalendar in Swift
print("Day test = \(rangeOfPeriod(.Day, date: NSDate()))")
print("Week test = \(rangeOfPeriod(.WeekOfYear, date: NSDate()))")
print("month test = \(rangeOfPeriod(.Month, date: NSDate()))")
print("Year test = \(rangeOfPeriod(.Year, date: NSDate()))")
@ryantxr
ryantxr / StoryboardViewControllerLaunch.swift
Created March 24, 2016 19:08
Launch a view controller from another storyboard.
func settingsButtonPressed(sender:UIButton) {
let storyboard = UIStoryboard(name: "AccountLinking", bundle: nil)
let linkingVC = storyboard.instantiateViewControllerWithIdentifier("AccountLinkingTable")
self.navigationController?.pushViewController(linkingVC, animated: true)
}
@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()
@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
}