Snip2Code is shutting down.
It has been quite a ride, since 2013 when we launched our first prototype: thanks to the effort of you guys we collected more than 3 million snippets!
We are very proud to help all our users to be more efficient in their jobs, and to be the central point to share programming knowledge for everyone.
Our basic service is free, so we always survived on our own resources to give you Snip2Code.
Unfortunately, we are no more in the financial position to sustain this effort, and therefore we are announcing here our permanent shut down,
which will take place on August 1st, 2020.
Please save your private snippets using our backup function in the settings, here.
IF YOU WANT TO SAVE SNIP2CODE, PLEASE CONSIDER DOING A DONATION!
This will allow us to pay for the servers and the infrastructure. If you want to donate, Contact Us!
marksimon232
marksimon232
Reputation Top 1%
marksimon232
A Programming Freak!
46 Snippets  (9th place)
Published
5 Channels
Created
18 Channels
Following
8582 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
Serious Famous Coder
Master Famous Coder
Serious Wise Coder

Recent Snippets See all snippets by marksimon232

public by marksimon232  3620  2  5  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  3676  0  5  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  4606  2  5  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  3015  0  6  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  283322  0  6  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))
}
;