Hi There,
Myself Bazzy…!!
There is a small confusion while practicing Arrays.
While adding or removing using myArray.insert / myArray.append / myArray += [“the data to add”}
For removing - myArray.remove(at:0)
while doing this cant get the edited or added / removed data while running
playground.swift is attached …if any errors in my swift playground…requisting you to guide me…!!!
Thank you
Post your playground code as text in a reply rather than an image. This saves anyone attempting to answer your question having to type out the code.
1 Like
Yes sure will do now itself…!!!
Here it …as you said…i didnt taught of it…
If there is any mistake in the codes…pls.guide and rectify me…!!
// this is lesson 16
var a = “dog”
var b = “cat”
var c = “bird”
//you can do a = a+“my” …but to have to do 3 times this…instead use array
//so :-
var myArray = [“dog” , “cat” , “bird”]
//to add my to evert pets ,
for counter in 0…2 { //here if you dont know the range… use 0…myArray.counter -1
myArray[counter] = “My” + myArray[counter]
print(myArray[counter])
}
// OR
//to display or show the items in the array without reassingning
// for item in myArray{
// print(item)
// }
//how to declare an empty array
//add items to array - 2 ways
myArray.insert(“Frog”, at:0)
myArray += [“Frog” , “Bear”]
myArray.append(“Raccoon”)
//remopve items in the array
myArray.remove(at:10)
//search an items in your array
myArray.firstIndex(of: String)
Here is the playground code amended and with added indented comments where I have made the amendments:
import UIKit
// this is lesson 16
var a = "dog"
var b = "cat"
var c = "bird"
//you can do a = a+“my” …but to have to do 3 times this…instead use array
//so :-
var myArray = ["dog" , "cat" , "bird"]
//to add my to evert pets ,
for counter in 0...2 { //here if you dont know the range… use 0…myArray.counter -1
myArray[counter] = "My" + myArray[counter]
print(myArray[counter])
}
// OR
//to display or show the items in the array without reassingning
// for item in myArray{
// print(item)
// }
//how to declare an empty array
//add items to array - 2 ways
myArray.insert("Frog", at:0)
myArray += ["Frog" , "Bear"]
myArray.append("Raccoon")
//remopve items in the array
// myArray.remove(at: 10)
// The index value of 10 was outside the array index range. There are only 7 elements in the array.
// The maximum index range is therefore 6 (starts at 0)
myArray.remove(at: 6) // Remove the last entry (Raccoon)
//search an items in your array
// myArray.firstIndex(of: String)
// When using the array method firstIndex(of: Type) you have to specify a string value in the case of myArray
myArray.firstIndex(of: "Mybird")
1 Like
Thanks a lot for the faster help…!!!
A small mistake can make complications… 
One more thing a small doubt…i am getting o/p when i wrote print(myArray) at last …is
[“Frog”, “Mydog”, “Mycat”, “Mybird”, “Frog”, “Bear”]…the square brackets are coming …is it right or its like default…???
Thank you once again…!!! 
If you print your array myArray
the output you see in the Console output will show you the array structure. So that’s why you see:
["Frog", "Mydog", "Mycat", "Mybird", "Frog", "Bear"]
If you print the first element in the array:
print(myArray[0])
you will see:
Frog
1 Like