Working with localized numbers – NSNumberFormatter Swift 2

In my case, I’m working on a csv importer and I wanted to get from the system the decimal and thousand delimiters. 

This is the way I do it, a workaround sure but works great:

var thousandDelimiter = NSNumberFormatter.localizedStringFromNumber(NSNumber(double: 1234), numberStyle: NSNumberFormatterStyle.DecimalStyle)


thousandDelimiter = thousandDelimiter.substringWithRange(thousandDelimiter.startIndex.advancedBy(1)…thousandDelimiter.startIndex.advancedBy(1))


 

var commaDelimiter = NSNumberFormatter.localizedStringFromNumber(NSNumber(double: 1.02), numberStyle: NSNumberFormatterStyle.DecimalStyle)

 

commaDelimiter = commaDelimiter.substringWithRange(commaDelimiter.startIndex.advancedBy(1)…commaDelimiter.startIndex.advancedBy(1))


Again, documentation in this case is quite good, find more here

Marc

Mouse Down (NSEvent in Swift 2)

As I continue adding features for my upcoming app I’m starting to work with NSEvents to add actions to user interaction. I’ve been amazed at how fast is to do this in Swift. 

For instance, detecting where the user presses a button in your window, and then converting it to the coordinate system of a view is extremely easy. Just type the following within your NSView class:

override func mouseDown(theEvent : NSEvent) {

        Swift.print(“Original: \(theEvent.locationInWindow)”)

        let convertedPoint = self.convertPoint(theEvent.locationInWindow, fromView: theEvent.window?.contentView)

        Swift.print(“Tuned: \(convertedPoint)”)

 

    }

Easy as pie!

Feedback, send it @MarcMasVi on Twitter.