Swift basics etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
Swift basics etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster

10 Şubat 2015 Salı

Swift Basics - Array and Dictionary


Simple Values

let -> make a constant
var -> make a variable 

In order to assign a value, we may not write type explicitly

Example:

    let constantValue = 20
        let constantValue2 : Int = 20

 Error:  Cannot assign to 'let' value 'constantValue'
constantValue = 30.5
 Error:  Cannot assign to 'let' value 'constantValue2’
        constantValue2 = 30

Change let to var
  var Value = 20
        var Value2 : Int = 20
      
 Error:  Type 'Int' does not conform to protocol 'FloatLiteralConvertible'
        Value = 30.5
  It’s OK
        Value2 = 30


Convert to “Int” type to “String” type

let stringValue = "The height of frame is "
        let height = 100
        let ​heightLabel​ = stringValue + String(height)
        
        
        println(​heightLabel​)

Convert to any type to String type basically.


  let height = 100
        let stringValue = "The height of frame is  \(height)"
        let ​heightLabel​ = stringValue
        
        
        println(​heightLabel​)


Change an item of array

        var arrayList = ["cat","dog","bird"]
        arrayList[1] = "penguen"
        
        for var i=0; i<arrayList.count; i++
        {println(arrayList[i])
        }

Add an item of array by using append method

        arrayList.append("bear")
 

Add an array of one or more compatible items
        arrayList += ["cow","butterfly"]

To insert an item into the array at a specified index:
        arrayList.insert("bee", atIndex: 4)

To remove an item from array
  arrayList.removeAtIndex(5)