onCreate() of My IOS Development Journey

Hey. I know it has been like forever. Life happened and I had to attend to its calls.

I started IOS development and it has been awesome, if you ask me. I will like to say that, for me, because of the android background I have, it is a different ball game entirely. Currently, if I have to choose between Android Development and IOS development, I will choose the former over and over again. Well, I can not say what will happen when I become so familiar with the IOS development more.

Ok!

As a newbie, I was writing a program that allows a user to input numbers from the console and then append the numbers to an array. I wrote the logic quick enough but I was stuck when the input was returning NIL. This made the program crash at that point. After few hours, I was able to realize that:

  • Default data type from the console is a STRING.
  • Then a new line gets appended to each number entered.
  • When trying to convert the string (plus the newline) into an integer, it could not. So it thought it wise to use a NIL.
  • The Error:

    fatal error: unexpectedly found nil while unwrapping an Optional value

    The temporary (or maybe permanent) solution is to trim the string like this:

    let inte = input().stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())

    See complete snippet below:

    print("Enter ten numbers")
    var arrLoop = [Int] ();
    for(var i=0;i<10;i++){
        print("Enter number " , i)
        let inte = input().stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
        let myInt: Int? = Int(inte)
        print("value is \(myInt!)")
        arrLoop.append(myInt!)
    }