Skip to content

Instantly share code, notes, and snippets.

View astannard's full-sized avatar

Andy Stannard astannard

View GitHub Profile
(UIApplication.sharedApplication().delegate as AppDelegate).saveContext()
@astannard
astannard / gist:894c2100f6ed33ab628e
Created February 2, 2015 08:24
iOS swift clear cache on memory warning
func applicationDidReceiveMemoryWarning(application: UIApplication) {
NSURLCache.sharedURLCache().removeAllCachedResponses()
}
@astannard
astannard / gist:fc42d95ec49e167915dd
Created February 2, 2015 08:23
iOS swift setup cache
//Withing APP Delegate
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
let cache = NSURLCache(memoryCapacity: 8 * 1024 * 1024, diskCapacity: 20 * 1024 * 1024, diskPath: nil)
NSURLCache.setSharedURLCache(cache)
return true
}
var alertController = UIAlertController(title: "Alert", message: "Your device does not support camera or photo libary", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alertController, animated: true, completion: nil)
@astannard
astannard / gist:eca5c880d40f96adcd3c
Created January 27, 2015 08:45
iOS camera action
@IBAction func snapBarButtonItemTapped(sender: UIBarButtonItem) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera){
var cameraController = UIImagePickerController()
cameraController.delegate = self
cameraController.sourceType = UIImagePickerControllerSourceType.Camera
let mediaTypes:[AnyObject] = [kUTTypeImage]
cameraController.mediaTypes = mediaTypes
}
@astannard
astannard / gist:9ccf5dd3611b0ad048c6
Last active August 29, 2015 14:01
SQL genarated ID's
@@IDENTITY - returns the last identity value generated for any table in the current session, across all scopes. You need to be careful here, since it's across scopes. You could get a value from a trigger, instead of your current statement.
SCOPE_IDENTITY - returns the last identity value generated for any table in the current session and the current scope. Generally what you want to use.
IDENT_CURRENT - returns the last identity value generated for a specific table in any session and any scope. This lets you specify which table you want the value from, in case the two above aren't quite what you need (very rare). Also, as @Guy Starbuck mentioned, "You could use this if you want to get the current IDENTITY value for a table that you have not inserted a record into."
The OUTPUT clause of the INSERT statement will let you access every row that was inserted via that statement. Since it's scoped to the specific statement, it's more straightforward than the other functions above. However, it's a litt
@astannard
astannard / razor
Created April 15, 2014 13:57
DropDownListFor
@Html.DropDownListFor(x=>x.ListName,
Model.ListName.Select(i => new SelectListItem
{
Value = i.Id.ToString(),
Text = i.Title
}),
new { @class = "input-full" })
@astannard
astannard / gist:8706894
Created January 30, 2014 11:44
Gets the browser name and version
navigator.sayswho = (function () {
var ua = navigator.userAgent, tem,
M = ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*([\d\.]+)/i) || [];
if (/trident/i.test(M[1])) {
tem = /\brv[ :]+(\d+(\.\d+)?)/g.exec(ua) || [];
return 'IE ' + (tem[1] || '');
}
M = M[2] ? [M[1], M[2]] : [navigator.appName, navigator.appVersion, '-?'];
if ((tem = ua.match(/version\/([\.\d]+)/i)) != null) M[2] = tem[1];
return M.join(' ');
@astannard
astannard / gist:7526517
Created November 18, 2013 11:47
HTML5 email field
<input type="text" title="email" required pattern="[^@]+@[^@]+\.[a-zA-Z]{2,6}" />
@astannard
astannard / gist:7332814
Created November 6, 2013 08:37
Stop javascript treating a number as text
// This does not work and gives the wrong figure due to concatination
var shiftPrem = $('#Contract_Reward_ShiftPremium').val();
var basePay = $('#Contract_Reward_BasePay').val();
var total = basePay + (basePay * (shiftPrem/100));
//This works by forcing javascript to treat the variables as numbers
shiftPrem = $('#Contract_Reward_ShiftPremium').val();
basePay = $('#Contract_Reward_BasePay').val();
total = +basePay + +(basePay * (shiftPrem/100));