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

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

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

On the Job training is always best
On the job training always provides an opportunity to learn the best industry practices. While you work on real time you would encounter many challenges that will force you to learn many new things. Class...
M

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

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