neda Derakhshesh
neda Derakhshesh

Reputation: 1163

how to initialize array in 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

Upvotes: 6

Views: 24440

Answers (1)

Charles-olivier Demers
Charles-olivier Demers

Reputation: 1058

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]);

Upvotes: 14

Related Questions