Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to initialize array in swift

Tags:

arrays

ios

swift

I am completely new in swift and iOS developing. I want to initialize this array

I defined the array like this when my class starts

 var i = 0
 var ArrayTitle = [NSString]()

and then I need to initialize it in one of the functions like this

 let ArrayTitl[i] = "text"

and I checked it even like this but the error is same

let ArrayTitl[0] = "text"

but it has this Error Cannot assign to immutable expression of type '[Int]'

Appreciate any help. thanks

like image 986
neda Derakhshesh Avatar asked Sep 03 '26 07:09

neda Derakhshesh


1 Answers

You are using a dynamic array so you need to append the value.

Here's a small exemple:

var arrayTitle = [String]()

arrayTitle.append("text")

print(arrayTitle[0]);
like image 93
Charles-olivier Demers Avatar answered Sep 04 '26 21:09

Charles-olivier Demers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!