Skip to content

Instantly share code, notes, and snippets.

func maxProfit(prices: [Int]) -> Int {
if prices.count < 2 {
return 0
}
var profit = 0
var min = prices[0]
for price in prices {
if price - min > profit {
@ecerney
ecerney / gist:d6bfed963eede7c685a0
Last active February 23, 2017 11:03
Guard argument
/*
* For a more in depth look at the guard statement check out
* http://ericcerney.com/swift-guard-statement/
* where I build off the sample shown here.
*/
var x: Int? = 0
// Lets me unwrap x and also check a condition on it
func fooGuard() {
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
tableView.beginUpdates()
if editingStyle == .Delete {
println(self.savedJobs)
println("\(indexPath.row) row will be deleted")
let deletedJob = self.savedJobs.removeAtIndex(indexPath.row)
deleteJobFromCoreData(deletedJob)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Top)
#########################
# .gitignore file for Xcode4 / OS X Source projects
#
# Version 2.1
# For latest version, see: http://stackoverflow.com/questions/49478/git-ignore-file-for-xcode-projects
#
# 2013 updates:
# - fixed the broken "save personal Schemes"
# - added line-by-line explanations for EVERYTHING (some were missing)
#
//Challenge 5
func reverseString(string: String) -> String {
return string.isEmpty ? "" : string.substringFromIndex(string.endIndex.predecessor()) +
reverseString(string.substringToIndex(string.endIndex.predecessor()))
}
int desiredRow = whatever;
int currentRow = 0;
for (int section = 0; section < [self.collectionView numberOfSections]; section++) {
for (int row = 0; row < [self.collectionView numberOfItemsInSection:section]; row++) {
if (currentRow == desiredRow) {
indexPath = [NSIndexPath indexPathForRow:row inSection:section];
}
currentRow++;
}