[AnyObject]?' does not have a member named 'subscript'
Solution 1
The array is declared as:
private var items: [AnyObject]?
so, as you also said, it's an optional
In swift an optional
is an enum, so a type on its own - and as an optional, it can contain either a nil
value or an object of the contained type.
You want to apply the subscript to the array, not to the optional, so before using it you have to unwrap the array from the optional
items?[indexPath.row]
but that's not all - you also have to use the conditional downcast:
as? Activity
because the previous expression can evaluate to nil
So the correct way to write the if statement is
if let act = items?[indexPath.row] as? Activity {
Solution 2
First of all you need to unwrap or optional chain the items as it can be nil
. AnyObject
has different behaviour when getting as element from array, due to the fact that AnyObject
can be a function. You would have to cast from items like this:
if let act = items?[indexPath.row] as AnyObject? as Activity? {
if act.client != nil {
// ...
}
}
If items
will never contain a function you can use
private var items: [Any]?
instead and cast with:
if let act = items?[indexPath.row] as? Activity {
if act.client != nil {
// ...
}
}
Solution 3
I have fixed my problem by convert index var type from UInt to Int
let obj = items[Int(index)]
Hope this can help someone who still get this problem.
Isuru
Started out as a C# developer. Turned to iOS in 2012. Currently learning SwiftUI. Loves fiddling with APIs. Interested in UI/UX. Want to try fiddling with IoT. Blog | LinkedIn
Updated on July 28, 2022Comments
-
Isuru over 1 year
I'm loading a list of objects from a core data database into a table view.
class ScheduleViewController: UITableViewController { private var items: [AnyObject]? // MARK: - Table view data source override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if let itemCount = items?.count { return itemCount } } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("DayScheduleCell", forIndexPath: indexPath) as DayScheduleCell if let act = items[indexPath.row] as Activity { if act.client != nil { // ... } } return cell } }
The data is retrieved inside a closure so I have declared an
items
array as an optional because it might be nil in the first run.I'm getting the error '[AnyObject]?' does not have a member named 'subscript' at this line
if let act = items[indexPath.row] as? Activity
.I can't figure out how to resolve this.
-
Isuru over 9 yearsThanks for the reply. But unfortunately I still get the same error at the same line.
-
Kirsteins over 9 yearsSorry, I had a typo. Try now.
-
Isuru over 9 yearsThat error is gone. But I get a new error Cannot invoke '!=' with an argument list of type '(@lvalue Client, NilLiteralConvertible)' at the line
if act.client != nil
-
Kirsteins over 9 yearsIs
client
property declared as optional value? -
Isuru over 9 yearsIt's another object in my core data database. Here is the
NSManagedObject
subclass of it. In the Data Model Inspector under Properties, its checked as Optional though. -
Isuru over 9 yearsThanks for the reply. I'm getting a new error Cannot invoke '!=' with an argument list of type '(@lvalue Client, NilLiteralConvertible)' at the line
if act.client != nil
. The client property is a property that holds aClient
object. Please see theNSManagedObject
subclass ofActivity
. In the Data Model inspect under properties for client, its checked as optional though. -
Kirsteins over 9 years
client
is marked as as optional (perhaps in NSManagedObject underlying subclass it is) and cannot be compared tonil
. I think you have to manually markclient
property as optional. At least this question has similar problem: stackoverflow.com/questions/25485273/… -
Antonio over 9 years
client
is not an optional, and as such it cannot benil
- hence you cannot check if it's not nil. Thatif
is conceptually redundant and of course considered an error for the compiler -
Isuru over 9 yearsYes, I just saw this question as well and manually marked the optional and now the error is gone. Thank you.
-
Isuru over 9 yearsSo according to this question, even though properties are marked optional in Core Data, its not mentioned in the generated
NSManagedObject
classes. You have to manually add it. I added a?
in front of the client property and now it works fine. -
Isuru over 9 yearsI got 2 answers to this question and both of them are correct. I'll have to accept @Antonio's answer because he replied first. Sorry. But thank you again for helping me out. :)