0

I had this variable in a my viewController var category : QCategoryy? and I decided to turn it into an array so I created this: var categories: [QCategoryy?]? = [] but after that i get all errors like this

"Value of type '[QCategoryy?]' has no member 'name'"

in this line self.title = categories?.nameand other lines like that.

Why i get these errors and how can i solve it?

First to turn the var into an array all worked well. This is the class of QCategoryy

struct QCategoryy {
    var name:String
    var isSelected = false
    init(name:String) {
        self.name = name
    }
}

extension QCategoryy: ExpressibleByStringLiteral {
    init(stringLiteral value: String) {
        self.name = value
    }
    init(unicodeScalarLiteral value: String) {
        self.init(name: value)
    }
    init(extendedGraphemeClusterLiteral value: String) {
        self.init(name: value)
    }
} 
1
  • if you have an optional array with optional items then getting the optional first item's name'd be like self.title = categories?.first??.name, for instance. Commented Oct 27, 2017 at 11:12

4 Answers 4

1

Very simple, you have converted that variable to array. So, first of all you have to give index of the array to invoke the value of object at specific index. like given below

for index in 0..<10 {
    if let category = categories[index] {
         self.title = category.name   
     }
  }

conditional binding will prevent from crashing app if categories[0] is nil.

UPDATE

To prevent array index out of bound in loop. You can use below example.

for category in categories {
    title = category.name
}
Sign up to request clarification or add additional context in comments.

6 Comments

in the conditional state, i have to add always [0] as index of the array?
No, that's index of the array, check updated answer.
the app i crushing for this error "fatal error: Index out of range" , which may be the cause?
Your array index is overlapping the actual array size. Suppose you have only 3 objects in array, that will be 0,1 & 2. So when it'll try to search for categories[3] or above. It will give you this error.
because the max number of objects that my array can contain is 10 and minimum si 0 (i use a stepper to choose the number of objects)
|
1

Here categories is an array. So do something like this

 self.title = categories?[0]?.name

2 Comments

i have to add always [0] as index of the array?
Whichever index you want to access. It is up to you.
0

categories in now an array. So you'll have to refer to some object of it whose name you name you're referring to.

More like, categories?[index]?.name

Comments

0

Or you can use forEach:

categories?.forEach{ value in
    self.title = value?.name           
}

Comments

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.