1

I am new to Swift. I have a simple class in swift

class ListItem: NSObject {
let itemName: String
var completed: Bool

init(itemName: String, completed: Bool = false)
{
self.itemName = itemName
self.completed = completed
}
}

When I refer to this class in my TableViewController, I get the following error: "Instance member itemName cannot be used on type ListItem".

My TableViewController code (cellForRowAtIndexPath method) is shown below.

    let tempCell = tableView.dequeueReusableCellWithIdentifier("triggerCell")! as UITableViewCell
    let listItem = listItems[indexPath.row]

    // Downcast from UILabel? to UILabel
    let cell = tempCell.textLabel as UILabel!


    cell.text = ListItem.itemName

    if (ListItem.completed)
    {
        tempCell.accessoryType = UITableViewCellAccessoryType.Checkmark;
    }
    else
    {
        tempCell.accessoryType = UITableViewCellAccessoryType.None;
    }

    return tempCell
}

I'm probably making a basic error, but I can't seem to identify where I'm going wrong.

1 Answer 1

1

You're calling the class instead of your instance. Try this:

cell.text = listItem.itemName

if (listItem.completed)
etc...

EDIT: Updating the creation of listItems.

let listItems = [ListItem("Viruses"), ListItem("Changes in weather"), etc...]
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks - I tried that, but get error "Value of type 'String' has no member 'itemName'"
What is the type of the listItems array? [String] or [ListItem]?
String let listItems = ["Viruses", "Changes in weather", "House dust mites", "Animal dander", "Foods", "Exercise", "Upset, distress and emotions", "Smoke - cigarettes and fires"]
Yeah, that's an array of Strings. You need to create ListItem objects and put those in an array. I can update my answer to include that.

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.