marksimon232
marksimon232
Reputation Top 1%
marksimon232
A Programming Freak!
46 Snippets  (9th place)
Published
5 Channels
Created
18 Channels
Following
8516 points  (13rd place)
Reputation
Top 5% Publisher
Junior Code Generator
Serious Code Generator
Senior Code Generator
Master Code Generator
Junior Publisher
Serious Publisher
Senior Publisher
Junior Topic Creator
Junior Topic Hub
Serious Topic Hub
Senior Topic Hub
Junior Trend Maker
Serious Trend Maker
Senior Trend Maker
Master Trend Maker
Guru Trend Maker
Junior Influencer
Junior Judge
Serious Judge
Senior Judge
Master Judge
Junior Popular Coder
Serious Popular Coder
Senior Popular Coder
Master Popular Coder
Junior Autobiographer
Serious Autobiographer
Junior Famous Coder
Serious Famous Coder
Master Famous Coder
Serious Wise Coder

Recent Snippets See all snippets by marksimon232

public by marksimon232  3551  2  6  0

Swift: .max and .min properties

If you have code around that uses lots of F’s (e.g. 0xFFFFFFFF) or capital letters (e.g. UINT32_MAX), consider replacing these constants with built-in Swift versions.
Swift offers .max and .min properties for many numeric types. For example:

public func Random01() -> Double {
    return Double(arc4random()) / Double(UInt32.max)
}
;

public by marksimon232  3610  0  6  0

Swift2: DebugPrint differentiation

Find items that printed differently in debugPrint than they did in print.
import Cocoa

// Items to check
//let x = "Snoop"
//let x = 1...5
let x = UnicodeScalar(0x1f601)

// Build test cases
var a = ""; print(x, &a, appendNewline:false); print(a)
var b = ""; debugPrint(x, &b, appendNewline:false); print(b)

// Compare and alert
func ExitAfter(t: Double, _ status: Int32) {
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 
        numericCast(UInt64(t * Double(NSEC_PER_SEC)))), 
        dispatch_get_global_queue(
            DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), 
        {exit(status)})
}

if a != b {
    // Success
    print("YES!")
    NSSound(named: "Sosumi")?.play()
    ExitAfter(0.5, 0)
    CFRunLoopRun() // stick around to produce sound
} else {print("No")}
;

public by marksimon232  4526  2  6  1

Swift 2: Creating the date Formatter

internal func BuildSimpleTimeFormatter() -> NSDateFormatter {
    let dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = NSDateFormatter.dateFormatFromTemplate("mm:ss:SSS", options: 0, locale: NSLocale.currentLocale())
    return dateFormatter
}

internal let dateFormatter = BuildSimpleTimeFormatter()

public func SWLog(format: String, _ args: CVarArgType...) {
    #if DEBUG
        let timeString = dateFormatter.stringFromDate(NSDate())
        print("\(timeString): " + String(format: format, arguments: args), &errStream)
    #endif
}
;

public by marksimon232  2964  0  7  1

Swift: Revisiting printing with string formats

You pass NSLog a format string followed by a variadic list of Objective-C-compatible parameters. 
The parameters map to specifiers embedded within the format string.

NSLog("Dictionary: %@, Double: %0.2f", ["Hello":3], 2.7)            
;

public by marksimon232  283252  0  7  1

Swift 2: Custom logger that mimics NSLog

Using this initializer, you can easily build a custom logger that mimics NSLog:
public func SWLog(format: String, _ args: CVarArgType...) {
    let dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = 
        NSDateFormatter.dateFormatFromTemplate(
            "mm:ss:SSS", options: 0, 
            locale: NSLocale.currentLocale())
    let timeString = 
        dateFormatter.stringFromDate(NSDate())
    print("\(timeString): " + 
        String(format: format, arguments: args))
}
;