working on it ...

Filters

Explore Public Snippets

Sort by

Found 5 snippets

    public by marksimon232  3547  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  3608  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  4523  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  2963  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  283249  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))
    }

    List Complete ( 5 snippets total )

    • Public Snippets
    • Channels Snippets