Find the best tutors and institutes for iOS Developer

Find Best iOS Developer Training

Please select a Category.

Please select a Locality.

No matching category found.

No matching Locality found.

Outside India?

Search for topics

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

Learning Path For Mobile Application Development
I. Traditional Approach: 1. Native Android Application Development: Android is a mobile operating system that runs on phones, tablets, smartwatches, TVs and more. You can learn Android Application Development...

Aman Juneja | 23/11/2017

1 0
0

10 Ways My Life Changed When I Learned To Code
1. I have freedom to make my own schedule. 2. I earn more, but work fewer hours. 3. I can say “no” to job offers. 4. People come to me asking if I can work for them. 5. I never have to...

Progressive IT | 28/06/2017

1 0
0

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

Naga Kranthi | 24/05/2017

0 0
0

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...

Naga Kranthi | 24/05/2017

0 0
0

iOS Interview Questions
Q1.What is property? Ans:Properties are used to access the instance variables from outside of class. Q2. What is synthesized? Ans:After you declare the property in objective­c . we have to tell...

Naga Kranthi | 23/05/2017

0 0
0

Find Best iOS Developer Training?

Find Now »

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 25 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 6.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