Skip to main content

Posts

Showing posts from January, 2016

Swift: NSURLSession in the Playground (iOS 9.2.1, Swift 2.1, Xcode 7.2)

A basic GET request You want to retrieve some data from the Internet? This can be done very simply in a Swift Playground. import Foundation if let url = NSURL(string:"https://itunes.apple.com/search?term=jack+johnson&limit=5") { NSData(contentsOfURL: url) } There are a number of types, including NSData , String (bridged from NSString), NSXMLParser , NSArray , and  NSDictionary , that, provided the contents of the URL is appropriate for that type, can be instantiated using the contents of a URL. It's a great way to get started and to experiment with things like JSON by sending simple GET requests through a URL string, but in order to progress beyond this we need to get to know NSURLSession (available from iOS 7 onwards, although some additional elements were added in iOS 8 and iOS 9). NSURLSession The first step in using NSURLSession is to create a session. In a Playground you can get up and running using class method sharedSession . let session =

Fix: Safari Crashing in iOS 9.2.1

You installed iOS 9.2.1? Now whenever you tap the address bar in Safari it crashes? Then you need to go Settings > Safari > Safari Suggestions and turn it off. (It's the third item down.) The fix comes from Reddit via Twitter . Follow @sketchytech

Swift: String Extension to Convert Floating-Point Binary to Double

So far this is just some roughed out code based on this explanation . Feel free to comment, adapt and streamline. I have no doubts that it can be improved, but for a start it works. All comments welcome. extension String { func binaryToDecimal() -> Double { func binaryBeforePoint(binary:String) -> Int { let values = Array(binary.characters).reverse() var num = 0 var div = 1 for v in values { guard let n = Int(String(v)) where n == 1 || n == 0 else {fatalError("Contains invalid character.")} num += n * div div *= 2 } return num } func binaryAfterPoint(binary:String) -> Double { let values = Array(binary.characters) var num:Double = 0 var div:Double = 2 for v in values { guard let d = Double(String(v)) where d == 1 || d == 0 else {fatalError("Contains invalid character.")} num += d/div div

Swift: Replace a range of values in an array (and do other groovy stuff too) the easy way (Xcode 7.2, Swift 2.1)

An introduction to the easy way We have a couple of options when it comes to replacing a range of values within an array, and one might call them the hard way and the easy way. Starting with the "hard way", there is a method called replaceRange:with: var array = [1,2,3,4,5,6,7,8,9,10] array.replaceRange(Range(start: array.startIndex.advancedBy(1),end: array.startIndex.advancedBy(2)), with: [11,11]) // [1, 11, 11, 3, 4, 5, 6, 7, 8, 9, 10] Using this method we must supply a Range to begin with and then a collection of the type we are working with to the with: argument. It needn't be as convoluted as the first example, however, because we can do the following: var array = [1,2,3,4,5,6,7,8,9,10] array.replaceRange(Range(1...2), with: [11,11]) // [1, 11, 11, 3, 4, 5, 6, 7, 8, 9, 10] and we can go even further in our code reduction: var array = [1,2,3,4,5,6,7,8,9,10] array.replaceRange(1...2, with: [11,11]) // [1, 11, 11, 3, 4, 5, 6, 7, 8, 9, 10] But the ultimate si

Back to the UIStackView: The tale of the incredible shrinking UIButton and how it grew again (Xcode 7.2, iOS 9; updated Swift 3, Xcode 8)

A solitary button, all alone in the world Create a UIButton and set the title text and colour: let button = UIButton(frame: CGRect(x: 20, y: 20, width: 100, height: 50)) button.setTitle("My Button", forState: .Normal) button.setTitleColor(.blueColor(), forState: .Normal) button.backgroundColor = .yellowColor() self.view.addSubview(button) Swift 3, Xcode 8 let button = UIButton(frame: CGRect(x: 20, y: 20, width: 100, height: 50)) button.setTitle("My Button", for: .normal) button.setTitleColor(.blue, for: .normal) button.backgroundColor = .yellow self.view.addSubview(button) ... where the poor little button meets the big bad stack The above button should render as expected within a regular view, but now try adding that button to a UIStackView by first creating an extension extension UIStackView { convenience init(axis:UILayoutConstraintAxis, spacing:CGFloat) { self.init() self.axis = axis self.spacing = spacing self.

Fix Kindle Previewer crashing on OS X El Capitan

There is a working solution  for fixing the crash on load bug in Kindle Previewer. What I wanted to do here is simply clarify in absolutely clear terms the text that should change from what to what . The first steps are clear and if you've been running Kindle Previewer on earlier versions of OS X then you should have these two things installed already (or at least X11). Download and install X11 Download and install Java for OSX The next steps are where things get a bit confusing. First right click the Kindle Previewer App in Finder and do as instructed on the Kindle Forums (to display the package contents), locating the Launcher file and opening it in a text editor. Now: Save a copy of the original file, just in case before proceeding Add the following text to the Launcher file export JAVA_HOME="/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home" immediately beneath: #!/bin/sh Replace java -d32 -XstartOnFirstThread -Dfile.encoding=UTF-8 -cp &qu

Changing background colour and theme colours in Android Studio

Change the background colour of your app Create a new project, selecting the full screen app option.  Locate the <AppName>/app/src/main/res/layout/activity_fullscreen.xml file.  Select the "Text" tab at the bottom of the file window (if not already selected) Inside FrameLayout change the background colour: android:background="#FFF500B8" Changing the theme colours of your app Change the status bar and slide over top menu bar colours using the Theme Editor (Tools > Android > Theme Editor). Either change the default theme colours or select a new theme Note that where a warning symbol appears this most likely means that there is a possible lack of contrast between elements and that you should revise your colour choices If you have selected (and/or amended) a new theme, return to the activity_fullscreen.xml file and select the theme from the top bar of the layout preview screen . Notes The theme for a project is defined in the  &l