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

Email Address Validation in iOS 7
-(BOOL) Emailvalidate:(NSString *)tempMail{BOOL stricterFilter = YES;NSString *stricterFilterString = @”+@(+\\.)+{2,4}”;NSString *laxString = @”.+@(+\\.)+{2}*”;NSString *emailRegex...

MapKit Tutorial with Swift in iOS 8
Using Map Kit, the portion of the map that is displayed on the screen is referred to as the region. The region is defined by a center location and a span of the surrounding area to be displayed. Inside...

IOS App Development
Student could work on the normal laptop or system. Some of student are thinking that Mac operating system is mandatory for Learn IOS but this is not mandatory. Thanks Ajay

Developing iOS Apps Part 1
The Basics Swift opts to use a Javascript-like ‘var’ keyword to define any new variable. So for example to create a new variable with a bit of text in it, you would have this: var...

P Balasubramanyam

0 0
0

Phone number validation in swift language
Simple phone number validation in swift ios 8 , Using this function you can easily check phone number . Also have some example for this using NSPredicate etc . you can see lots of method for number...
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