6

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

4
  • 6
    There are so many issues, please read A Swift Tour Commented Oct 4, 2017 at 20:01
  • 2
    Welcome to Swift! Start off by reading the language guide. It will explain basics such as this. 👍 Commented Oct 4, 2017 at 20:02
  • 1
    developer.apple.com/library/content/documentation/Swift/… Commented Oct 4, 2017 at 21:20
  • Also, you wouldn't ever use "let" in this way. Let is used to define a variable - which MAY include an assignment but doesn't have to. But, once it is defined (as your array is), you wouldn't use a let. Commented Oct 20, 2022 at 18:14

1 Answer 1

13

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]);
Sign up to request clarification or add additional context in comments.

2 Comments

Do not suggest NSString and variable names starting with capital letters in Swift by keeping the bad habits
I just copied the code above, but yes, I will correct it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.