Decoding Base64URL encoded data

import Foundation

class Decoder {
  class func getDecodedString(#encodedString: String)->String?{
    var base64EncodedString = Decoder().convertBase64URLtoBase64(encodedString: encodedString)
    if let decodedData = NSData(base64EncodedString: base64EncodedString, options:NSDataBase64DecodingOptions(rawValue: 0)){
      return NSString(data: decodedData, encoding: NSUTF8StringEncoding) as? String
 }
    return nil
}
 
 private func convertBase64URLtoBase64(#encodedString: String)->String{
   var tempEncodedString = encodedString.stringByReplacingOccurrencesOfString("-", withString: "+", options: NSStringCompareOptions.LiteralSearch, range: nil)
   tempEncodedString = tempEncodedString.stringByReplacingOccurrencesOfString("_", withString: "/", options: NSStringCompareOptions.LiteralSearch, range: nil)
   var equalsToBeAdded = (encodedString as NSString).length % 4
   if(equalsToBeAdded > 0){
     for _ in 0..<equalsToBeAdded {
       tempEncodedString += "="
     }
   }
   return tempEncodedString
 }
 
}

Piglatin Game!

SWIFT 1.2
Xcode 6.3 Beta 2

import UIKit

var str = "Hello, playground"

func translate(plainText:String)->String{
let vowels = Set(Array("aeiou"))
 var plainTextArray = Array(plainText)
 if let firstChar = plainTextArray.first{
 if !vowels.contains(firstChar){
 plainTextArray.removeAtIndex(0)
 plainTextArray.append(firstChar)
 }else{
 plainTextArray.append("w")
 }
 plainTextArray.append("a")
 plainTextArray.append("y")
 }
 return String(plainTextArray)
}

func pigLatinFromPlainText(plainText: String)->String{
 let lowercase = plainText.lowercaseString
 let words = lowercase.componentsSeparatedByString(" ")
 var translation = [String]()
 
 for word in words{
 translation.append(translate(word))
 }
 return " ".join(translation)
}

pigLatinFromPlainText("app")
pigLatinFromPlainText("app sweet awesome")
pigLatinFromPlainText("pig latin")

Extensions!!

SWIFT 1.2
Xcode 6.3 Beta 2

// Create an extension for Int isEven() that returns true if
// an integer is even.

extension Int{
 func isEven()->Bool{
 return self % 2 == 0
 }
}

4.isEven()
3.isEven()
0.isEven()
(-4).isEven()

// Create an extension for Range called "each" that iterates over
// the values in the and calls a user specified function.

extension Range{
 func each (function: (T)->()){
 for i in self{
 function(i)
 }
 }
}

(1..<10).each { x in
 println(x)
}

// Create an extension for Range that implements Reduce
// Using reduce, implement a sum function for Range

extension Range {
 func reduce<U>(initial: U, combine: (U, T) -> U) -> U {
 var val = initial
 each { index in val = combine(val, index) }
 return val
 }
}

func sum(range: Range<Int>) -> Int{
 return range.reduce(0) { $0 + $1 }
}

// Create a sum function that takes a Range<Int> and computes the
// value in O(1)

sum(-20...2003)

//GAUSS Method
// 1+2+3
// 3+2+1
// -----
// 4+4+4 = 12/2 = 6
// value in O(1)
// Sum = (n * n+1)/2
func sumFastRange(range: Range<Int>) -> Int{
 let start = range.startIndex
 let end = range.endIndex
 return (start + (end - 1)) * (end - start) / 2
}

sumFastRange(-20...2003)

CREATE CUSTOM DELEGATE AND PROTOCOL IOS | SWIFT & OBJECTIVE-C

One of the most powerful tools an iOS developer has is the ability to create protocols and implement delegates that let your classes notify when an event occurs. I use them all the time in my projects and wanted to show how easy it is to implement your iOS Project. I will demonstrate them in both Objective-C and Swift so you can see a side by side comparison. In this example we will be implementing timer that fires a timerFinished method to let the UIViewController know the timer finished and start the timer over.

Protocols

A protocol is a list of methods that specify an interface that your delegate will implement. There are two kinds of delegates we can use: Option and Required. They are pretty self explanatory but the difference is Required will throw an error letting you know your class is not conforming to the protocol. Also protocol methods are required by default so if you want it optional don’t forget that optional keyword. If you are using swift you will also need to add the @objc prefix if you want an optional method.

Continue reading

Inout Parameter

SWIFT 1.2
Xcode 6.3 Beta 2

// Inout

import UIKit

// The following code reads and writes inputOutput how many times?

func method1(inout inputOutput:Int) {
 for _ in 0 ..< 10 {
 inputOutput = inputOutput + 1
 }
}

func method2(inout inputOutput:Int) {}

// Hint build an observable integer that counts when it is read
// to and written from. (Unfortunately there is no willGet didGet
// so you will need to implement it as a computed property.


struct Counter {
 var value: Int{
 mutating get{
 ++readCount
 return internalValue
 }set{
 ++writeCount
 internalValue = newValue
 }
 }
 var readCount: Int = 0
 var writeCount: Int = 0
 var internalValue: Int = 0
}

var setGetCounter = Counter()
method1(&setGetCounter.value)
setGetCounter

var setGetCounter2 = Counter()
method2(&setGetCounter2.value)
setGetCounter2

Function Argument Excercise.

SWIFT 1.2
Xcode 6.3 Beta 2


// Free Function Argument Practice

func addAll1(x: Int, y: Int, boom z: Int) -> Int {
 return x + y + z
}

// Change the above signature of add all so that it is called with
// Don't change the definition of the fuction.
addAll1(1,2, boom: 3)

func addAll2(x: Int, #longName: Int, z: Int) -> Int {
 return x + longName + z
}

// Change the above signature of addAll2 so that it is called with
// Don't repeat yourself
addAll2(1, longName: 2, 3)


func addAll3(x: Int, y: Int, _ z: Int = 10) -> Int {
 return x + y + z
}

// Change the above signature of addAll3 so that it is called with

addAll3(1,2,3)
addAll3(1,2)