for
simplifications
Q: How can we simplify this code?
let items = ["one", "two", "three", "four"]
for (_, thing) in items.enumerate() {
print(thing.characters.count)
}
import Swift | |
import Foundation | |
// convert input Collection<T> into an NSMutableArray<NSData> | |
// works with Array<T>, Set<T>, Dictionary<T,U> | |
func convertCollectionToArrayOfData<T: CollectionType>(collection: T) -> NSMutableArray { | |
return NSMutableArray(array: map(collection){ | |
var mutableItem = $0 | |
return NSData(bytes: &mutableItem, length: sizeof(T.Generator.Element.self)) | |
}) |
/// A function for calculating the time in 0.1 hour increments | |
/// i.e. 1 hour and 6 min is 1.1hours, 3 hours 12 minutes is 3.2 hours | |
func calc(hours: Int, min: Int) -> Double { | |
let dbl = Double(min) | |
let roundedMin = (dbl / 60.0) | |
let flr = floor(roundedMin * 10 ) / 10 | |
return Double(hours) + floor(roundedMin * 10) / 10 | |
} | |
/// How to round a number? |
//Turns out in swift Bool is a struct! | |
/// From the Docs. `import Swift` | |
/// A value type whose instances are either `true` or `false`. | |
struct Bool { | |
/// Default-initialize Boolean value to `false`. | |
init() | |
} |
#!/usr/bin/env xcrun swift -F $(xcode-select)/Platforms/MacOSX.platform/Developer/Library/Frameworks | |
import Foundation | |
//need to process any arguements here | |
// | |
// uniq [-c | -d | -u] [-i] [-f num] [-s chars] [input_file [output_file]] | |
// MARK: Standard In , Standard Out | |
func getStandardInput() -> String? { |
import UIKit | |
import Swift | |
import XCPlayground | |
// we want to schedule a timer to fire on Sunday at 4:00am. | |
// NSTimer takes a fire date. | |
// NSTimer.init(fireDate date: NSDate, interval seconds: NSTimeInterval, target target: AnyObject, selector aSelector: Selector, userInfo userInfo: AnyObject?, repeats repeats: Bool) | |
// | |
// So how do we get the date for the next coming Sunday at 4:00am ? | |
// Date Components of course! |
#include <stdio.h> | |
#include <stdlib.h> | |
int in(char value, char* list){ | |
for (char *i = list; *i != '\0'; i++){ | |
if ( *i == value ){ | |
return 1; | |
} | |
} | |
return 0; |
import UIKit | |
func intToBin(num: Int, padding: Bool = true ) -> String { | |
var binString = String(num, radix:2) | |
if padding { | |
binString = padStringTo4DigitChunks(binString) | |
} | |
return binString | |
} |
extension BooleanLiteralType { | |
func if_true(block: () -> () ) -> BooleanLiteralType{ | |
if self { | |
block() | |
} | |
return self | |
} | |
func if_false(block: () -> ()) -> BooleanLiteralType { | |
if !self { | |
block() |
xcode_plugin_update_uuid () { | |
local xcode plugin | |
for xcode in /Applications/Xcode*.app | |
do | |
uuid=$(defaults read "$xcode/Contents/Info" DVTPlugInCompatibilityUUID) | |
echo "$xcode - $uuid" | |
for plugin in ~/Library/Application\ Support/Developer/Shared/Xcode/Plug-ins/*.xcplugin | |
do | |
echo "Updating $plugin" | |
defaults write "$plugin/Contents/Info" DVTPlugInCompatibilityUUIDs -array-add $uuid |
for
simplifications
Q: How can we simplify this code?
let items = ["one", "two", "three", "four"]
for (_, thing) in items.enumerate() {
print(thing.characters.count)
}