Learning Swift

There are some great tutorials from Ayaka Nonaka in this blog. Well worth checking it out. Especially the part about optionals and enums.

Posted in Uncategorized

Great tutorial for iOS animations

Do you think the standard UIActivityIndicatorView is a bit boring (has only gray an white) and that the size is too small? Despair no more. Here is a great tutorial and it is just a few lines of code and you have a fantastic activity indicator! Thanks a lot to Marin Todorov

Posted in Uncategorized

Testing callback with delegate methods in Swift

Apple has now made it possible to test callback methods in unit tests for swift.

It works great on callback methods directly, but also works if you have delegates reacting to the callback in another class

The trick is to use the XCTestExpectation class.

In the function where you call the callback method, you must instantiate an expectation object like this:

//Declare the expectation object
var expectation: XCTestExpectation!
func doSomeTesting(){
  expectation = expectationWithDescription("Waiting for delegates”)
  …… do other stuff call the callback
  //start the waiting
  waitForExpectationsWithTimeout(5) { (error) in  //5 seconds waiting time
     println( error )
  }
  //assert something
  XCTAssert(true, "Pass")

}

func theMethodFromDelegateProtocol( someThing:AnyObject){
//Do some other stuff with the someThing object
//and fulfill the expectation to avoid errors
   expectation.fulfill()
}

That is relatively, actually very, easy to set up and use. Just remember to set the waiting time realisticly (low or high)

Posted in Swift Programming

Be careful with Int and 32 and 64 bit architectures!

While I was doing a new App, I did all the development using Iphone 5s as the base model in the simulator, just testing it a few times om 6 and 6 plus. However, as I had submitted it to the appstore, I tried it on the 4s simulator, just to learn that the app crashed every time it should convert a String to an Int. This happened even though I had checked the string for nil and everything.

What happened was a bit annoying and scaring.

My code looked like this, a clousure making an int out of a string from a string where the int is embedded:

internal func makeUnixTimeExtracor(firstString:String, secondString:String) -> ( ( dsbDateString:String) -> Int){

func extractUnixDate( dsbDateString: String ) -> Int {

let startRange = dsbDateString.rangeOfString(firstString )
let endRange = dsbDateString.rangeOfString(secondString )
let startIndex = startRange?.endIndex
let endIndex = endRange?.startIndex

if( startRange != nil && endRange != nil){

let range = startIndex!..<endIndex!
let unixTimeString = dsbDateString.substringWithRange( range )
return unixTimeString.toInt()!

}else{

let dateIntervalNow = NSDate().timeIntervalSince1970
return Int(dateIntervalNow)

}

}
return extractUnixDate

}

The culprit was this one: return unixTimeString.toInt() On a 32 bit architecture is does NOT throw an exception, saying it can’t convert it to a 64 bit Int, but it just returns a “nil”, which later made my app crash. On 64 bit platforms, it ran perfectly.

The workaround was to make in a NSString and then convert that to a 32 bit int like this:

let unixTimeString = dsbDateString.substringWithRange( range )
return (unixTimeString  as NSString).intValue

Of course you must check that the value does not get any bigger than a 32 bit Integer.

Also this shows the importance og testing the code in all environments.

Posted in Swift Programming

Flappy Birds Clone: Using SPRITEKIT for the first time ever

As a complete beginner in SpriteKit, I am amazed how easy it was to make a Flappy Bird clone (with some help from a course).

flappybirds

Quite fun too to play around with actually.

 

Posted in Swift Programming

Handling strings in Swift is STRANGE!

Some hints and links

 

1) Handling substrings

See this excellent website about using the strange new string.substringwithrange( range: Range<String.Index>) and the String.substringFromIndex( index: String.index)


let myString = "[ABCDEFGHI]"
let startIndex = advance(myString.startIndex, 1)
let endIndex = advance(myString.endIndex, -1)
//start- and endIndexes are now String.Indexes
//lets build a range
let range = startIndex..<endIndex
//Finally get the substrings
let myNewString = myString.substringWithRange( range )
let mySubString = myString.substringFromIndex(startIndex)

Posted in Uncategorized

Using the UIAlertController in Swift (iOS8.1)

Swift (and objective C) comes with a handy way to handle login, confirmations, etc in alerts.
Here is an example


func handleParseLogin(){
        
        let alertController = UIAlertController(title: "Login!", message: "Please log in to the app", preferredStyle: .Alert)
        let loginAction = UIAlertAction(title: "Login", style: .Default) { (_) in
            let loginTextField = alertController.textFields![0] as UITextField
            let passwordTextField = alertController.textFields![1] as UITextField
            self.login(loginTextField.text, passwd: passwordTextField.text)
        }
        loginAction.enabled = false
        
        //let forgotPasswordAction = UIAlertAction(title: "Forgot Password", style: .Destructive) { (_) in }
        let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (_) in }
        
        alertController.addTextFieldWithConfigurationHandler { (textField) in
            textField.placeholder = "Login"
            
            NSNotificationCenter.defaultCenter().addObserverForName(UITextFieldTextDidChangeNotification,
                object: textField, queue: NSOperationQueue.mainQueue()) { (notification) in
                loginAction.enabled = textField.text != ""
            }
        }
        
        alertController.addTextFieldWithConfigurationHandler { (textField) in
            textField.placeholder = "Password"
            textField.secureTextEntry = true
        }
        
        alertController.addAction(loginAction)
        //alertController.addAction(forgotPasswordAction)
        alertController.addAction(cancelAction)
        self.presentViewController(alertController, animated: true, completion: nil)
        

    }
   
Here is how it look, I am sure there are plenty of ways to make it better looking

iOS Simulator Screen Shot 04 Nov 2014 17.52.09

By the way, this is a log in to Parse, go and have a look, it seems to be great.
Posted in Swift Programming