Claudia Laurie Claudia Laurie - 6 months ago 10
Swift Question

swift xcode error: "instance member 'name' cannot be used on type 'clothing_category'

I am currently building an ios app using swift on xcode and I keep getting the error

instance member 'name' cannot be used on type 'clothing_category.'


I have set up a tableviewcell and I need to fetch the appropriate
clothig_category
in the clothing_categories array. The line that has the error is:
cell.nameclothing_category.text = clothing_category.name


Below is the full code:

class clothing_category: NSObject {
let name: String!
init (name: String) {
self.name = name
}
}

class categoryTableViewController: UITableViewController {

// MARK: Properties

// Create an array to hold the clothing.
var clothing_categories = [clothing_category]()

override func viewDidLoad() {
super.viewDidLoad()
// Load sample data
loadSampleClothing_categories()
}
func loadSampleClothing_categories() {
clothing_categories.append(clothing_category(name:"Jackets"))
clothing_categories.append(clothing_category(name: "Accessories"))
clothing_categories.append(clothing_category(name: "Pants"))
clothing_categories.append(clothing_category(name: "Color"))
clothing_categories.append(clothing_category(name: "Tops"))
clothing_categories.append(clothing_category(name: "Dressing for an Occaision"))



// Load up the array when the view is loaded.

clothing_categories.append(clothing_category(name:"Jackets"))
clothing_categories.append(clothing_category(name: "Accessories"))
clothing_categories.append(clothing_category(name: "Pants"))
clothing_categories.append(clothing_category(name: "Color"))
clothing_categories.append(clothing_category(name: "Tops"))
clothing_categories.append(clothing_category(name: "Dressing for an Occaision"))

// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false

// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return clothing_categories.count
}

// Table view cells are reused and should be dequeued using a cell identifier.
let cellIdentifier = "categoriesTableViewCell"


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cellIdentifier", forIndexPath: indexPath) as! catgeoriesTableViewCell
//Fetches the appropriate clothing_catgeory for the data source layout.
_ = clothing_categories[indexPath.row]
**cell.nameclothing_category.text = clothing_category.name**
return cell

}


How can I resolve this problem?

Answer

Okay I see where you have gone wrong. Naming conventions would of actually prevented this but anyway. You're actually accessing the name like it's a static property, when in fact it's an instance property. Doing something like this will solve your problem:

let category = clothing_categories[indexPath.row]
cell.nameclothing_category.text = category.name
Comments