UrbanPro
true

Learn iOS Developer from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Working With Arrays in Swift

Satish S
12/03/2017 0 0

Swift Arrays:

Like Objective C  arrays Swift arrays are also collection of Objects 

The Difference is in swift we don't work directly with addresses.

The declaration of Swift array is as follows


The above declaration says that myArray is of type Array that holds String objects.

The type of Array Objects needs to be declared using <> brackets.

We can also declare an Array that can hold any type of object using AnyObject.

AnyObject in swift is similar to id data type in Objective C.

The sample declaration of array with AnyObject is shown below



Assigning Array in Swift


                                                    myArray=["hai",32,"hello"]
 

Iterating through Array Objects

Using For Loop


    for item : AnyObject in myArray
    {
        println(item)

    }



Using Half Open Range Operator

Note:Use Count method to get the upper bounds of the Array


  for element in 0..myArray.count
    {
        println(arr[element])

 

    }



Adding Objects in Array 

Use append method on array object to add an object to the existing array at the end 

This method adds an object at the end of the array and increases the bounds of the array.

 
myArray.append("hey")
 
for i in 0..<myArray.count
{
    println(myArray[i]);

}

If you want to add Objects at particular index use insert at index method


myArray.insert("had", atIndex: 1)
for i in 0..<myArray.count
{
    println(myArray[i]);

}
or alternatively you could use 
 
        myArray[3]="hey"
 
 
Reversing an Array
 
Reversing an array in swift is very simple use the reverse() function on array object to do this
 
myArray=myArray.reverse()
for i in 0..<myArray.count
{
    println(myArray[i]);
 
}
 
Removing Objects from Array
 
Removing object at particular index is done using method removeatindex() method
 
 myArray.removeAtIndex(3)
 
        println(myArray)
 
To remove Last Object use removeLast() method on array object
 
   myArray.removeLast()
 
        println(myArray)
 
Replacing Multiple objects using Closed Range Operator
 
    //replacing multiple objects
        myArray[0...2]=[32,"sad","henery"]
 
        println(myArray)
 
0 Dislike
Follow 0

Please Enter a comment

Submit

Other Lessons for You

Swift Constants
Objective C Provides a way to declare constant using Const keyword.In Objective C we often use #define for declaring a macro.Swift the way to declare constants is different from objective C. Let is used...
X

Looking for iOS Developer Classes?

The best tutors for iOS Developer Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn iOS Developer with the Best Tutors

The best Tutors for iOS Developer Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more