The code that displays dictionary data in a table view cell is designed to solve key arrangement and data retrieval problems.

Asked 2 years ago, Updated 2 years ago, 80 views

The code that causes the cells in the table view to display dictionary-type arrays does not know where to retrieve dictionary key arrays or data.Could you let me know?Thank you for your reply.

The errors are as follows:
①Ambiguous reference to member 'subscript' (line 109)
②Value of type '[String:Bool]]' has no member 'updateValue' (line 123, 148)
③Value of type '[String:Bool]]' has no member 'keys'' (line 152)
I searched online and tried, but I couldn't find anything I could solve.

import UIKit

classViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    
    ////
    func numberOfSections (intableView: UITableView) - > Int // Default is 1 if not implemented
    { return sectionTitle.count
    }
    ////
    functableView(_tableView:UITableView, titleForHeaderInSection section: Int) - > String?// fixed font style.use custom view(UILabel) if you want something different
    {
        return sectionTitle [section]
    }
    
    
    // Place the calendar or date picker on the height of the status bar.
    let statusBarHeight=UIApplication.shared.statusBarFrame.height+200
    
    // Section Items
    var sectionTitle= ["H28,01,23", "H27,12,31", "H28,01,12", "H28,02,21", "H28,11,10" ]
    
    
    // Checklist Items and Check Status
    varcheckListItem1: String:Bool = [
        "Item 1": true,
        Item 2 : false,
        "Item 3": true,
        "Item 4": true,
        "Item 5": false
    ]
    varcheckListItem2: String:Bool = [
        "Item 2-1": false,
        "Item 2-2": true,
        "Item 2-3": true,
        "Item 2-4": true,
        "Item 2-5": false
    ]
    varcheckListItem3: String:Bool = [
        "Item 3-1": true,
        "Item 3-2": true,
        "Item 3-3": true,
        "Item 3-4": true,
        "Item 3-5": false
    ]
    varcheckListItem4: String:Bool = [
        "Item 4-1": true,
        "Item 4-2": false,
        "Item 4-3": true,
        "Item 4-4": false,
        "Item 4-5": false
    ]
    varcheckListItem5: String:Bool = [
        "Item 5-1": true,
        "Item 5-2": false,
        "Item 5-3": true,
        "Item 5-4": true,
        "Item 5-5": true
    ]
//    variableData=[checkListItem1, checkListItem2, checkListItem3, checkListItem4, checkListItem5]
  variableData: [[String:Bool]] = [ ]
    
    lettableView=UITableView()

override func viewDidLoad(){
        super.viewDidLoad()
        tableData=[checkListItem1, checkListItem2, checkListItem3, checkListItem4, checkListItem5]
        
        // Creating a UITableView
        tableView.frame = CGRect(
            x —0,
            y —statusBarHeight,
            width —self.view.frame.width,
            height —self.view.frame.height-statusBarHeight
        )
        tableView.delegate=self
        tableView.dataSource=self
        self.view.addSubview(tableView)
    }
    
    // Creating Cells
    //
    functableView(_tableView:UITableView, cellForRowAtindexPath:IndexPath) - >UITableViewCell{
        
        // Obtain an array of Dictionary keys
       //        string.characters.count 
       varkeys = [String] (tableData.keys) // < - Error 1

        
        // Sort by key
        keys.sort()
        
        // Retrieve key string
        letcellText=keys [indexPath.row]
        
        // Cell Creation and Text Configuration
        letcell=UITableViewCell (style:.default, reuseIdentifier: "cell")
        cell.textLabel?.text=cellText
        
        ///
        let sectionData=tableData[(indexPath as NSIndexPath).section]//<-Error 2
        letcellData=sectionData[(indexPath as NSIndexPath).row]
        
        
        
        // If the check state is true, go to the check state from the beginning.
        if self.tableData[cellText]!{
            cell.imageView?.image=UIImage(named: "checked")
        } else{
            cell.imageView?.image=UIImage(named: "unchecked")
        }
        
        return cell
    }
    
    // What to do when a cell is tapped?
    functableView(_tableView:UITableView, didSelectRowAtindexPath:IndexPath){
        
        iflet cell=tableView.cellForRow(at:indexPath){
            
            // Get text for tapped cells
            letcellText=cell.textLabel?.text
            
            // Switch Images and Change Dictionary Values
            ifcell.imageView?.image==UIImage(named: "checked"){
                
                self.tableData.updateValue(false,forKey:cellText!)//<-Error 3
                cell.imageView?.image=UIImage(named: "unchecked")
            } else{
                
                self.tableData.updateValue(true, forKey:cellText!)//<-Error 3
                cell.imageView?.image=UIImage(named: "checked")
            }
            
            // Unselect state
            cell.isSelected=false
        }
    }
    

    functableView(_tableView:UITableView, numberOfRowsInSection section:Int) - > Int{
        let sectionData=tableData[section]
        return self.tableData.count
    }    
}

Previous question
CCannot use instance member… 」 error creating table view

swift swift3

2022-09-30 13:59

1 Answers

Unlike the Swift language-specific errors mentioned in the previous question, if this is caused by misunderstanding about the data type, it will be difficult to find a solution even if you search by error message.

In your code, tableData has a data type of [[String:Bool]] (this is the same as Array<Dictionary>String,Bool>>), and it is called String to Dictionary as an element.

Once the element type is left behind,

  • tableData is an array (Array) type

That's right.

You have five dictionary data in tableData (checkListItem1 to checkListItem5), but you will not be able to choose a Swift language or iOS framework to access tableData as a dictionary.

Am Ambiguous reference to member's subscription'

*The line number is not displayed in StackOverflow code sample, and the number seems to be much different. If you specify the line where the error occurs in the form of a comment in the source code such as //<-1, it will definitely be easier to see the error.

So は means this part?

let sectionData=tableData[(indexPath as NSIndexPath).section]
    letcellData=sectionData[(indexPath as NSIndexPath).row] //<-1

As I wrote earlier, tableData is an array type with the element String to Bool dictionary type, so it is an integer value ((indexPath as NSIndexPath).section, by the way, indexPath.section).omitted in ) only means that the integer value (indexPath as NSIndexPath).row (also indexPath.row is acceptable).

If you say ambiguous as the error message says, I think there's a way to eliminate ambiguity and clarify it, but Swift's error message around here is still developing and should be read to mean "there are errors related to the data type regarding 'subscript'.

VValue of type'[[String:Bool]]'has no member 'updateValue'

Here and:

self.tableData.updateValue(false, forKey:cellText!)//<-2

Do you mean here:

self.tableData.updateValue(true, forKey:cellText!)//<-2

Exactly what I wrote at the beginning is true, but updateValue(_:forKey:) is a method defined for dictionary types and cannot be used for array types.The array type [String:Bool] has no methods or properties such as updateValue (collectively referred to as member).

*By the way, if you simply want to update the Swift dictionary value, you don't usually use updateValue(_:forKey:).

VValue of type'[[String:Bool]]'has no member 'keys'

Do you mean this?

var keys=[String] (tableData.keys) // <-3

It's almost the same thing over and over again, but tableData is an array type, but using keys (which is a dictionary type property), the same error as と is displayed.

The problem is that tableData has five elements in an array, but you can access it as if it were a dictionary (as if one were automatically selected), so you can choose one of the tableData and use it as a dictionary.

In addition, I added corrections to the relevant parts, but specifically the following are the following: viewDidLoad()The following three methods are subject to correction.

//Create Cells
//
functableView(_tableView:UITableView, cellForRowAtindexPath:IndexPath) - >UITableViewCell{
    //### First of all, you have to choose which one of the 'tableData's' will be used.
    let sectionData=tableData[indexPath.section] // <- `sectionData' is a dictionary type (`[String:Bool])become `)
    
    //### Obtain and sort key sequences for `sectionData`
    //###`sectionData.keys` is originally `[String]` type, so no type conversion is required
    //### Since data before sorting is not required, use `sorted()` to create data after sorting only
    let keys=sectionData.keys.sorted()//<-3
    
    // Retrieve key string
    letcellText=keys [indexPath.row]
    
    // Cell Creation and Text Configuration
    //### This place is supposed to be
    // letcell=tableView.dequeueReusableCell(withIdentifier: "cell")
    // or
    // letcell=tableView.dequeueReusableCell(withIdentifier: "cell", for:indexPath)
    I have to say //, but it will be longer regardless of the error in the question, so I'll just leave it as it is.
    letcell=UITableViewCell (style:.default, reuseIdentifier: "cell")
    cell.textLabel?.text=cellText
    
    //### `sectionData` is `[String:Bool]`, so add a value of `String` type.
    //###`cellData` becomes `Bool?`(==`Options<Bool>`) type
    //### It is better to use a descriptive name such as `cellIsChecked` instead of an abstract name like `cellData`.
    let cellData=sectionData[cellText]//<-1
    
    // If the check state is true, go to the check state from the beginning.
    //###Swift's conditional clause accepts only `Bool`, so we don't usually write `==true`, but `cellData` is `Bool?` type, so this is convenient.
    ifcellData==true{
        cell.imageView?.image=UIImage(named: "checked")
    } else{
        cell.imageView?.image=UIImage(named: "unchecked")
    }
    
    return cell
}

// What to do when a cell is tapped?
functableView(_tableView:UITableView, didSelectRowAtindexPath:IndexPath){
    
    iflet cell=tableView.cellForRow(at:indexPath){
        
        // Get text for tapped cells
        //### `Optional<String>` is difficult to use, so use the `?` operator to set the default value.
        letcellText=cell.textLabel?.text???"
        
        // Switch Images and Change Dictionary Values
        ifcell.imageView?.image==UIImage(named: "checked"){
            
            //### You have to choose which data of `tableData` should be updated.
            //### Dictionary value updates are usually done with subscript
            self.tableData [indexPath.section] [cellText] = false
            cell.imageView?.image=UIImage(named: "unchecked")//<-2
        } else{
            
            //### You have to choose which data of `tableData` should be updated.
            //### Dictionary value updates are usually done with subscript
            self.tableData [indexPath.section] [cellText] = true
            cell.imageView?.image=UIImage(named: "checked")//<-2
        }
        
        // Unselect state
        cell.isSelected=false
    }
}


functableView(_tableView:UITableView, numberOfRowsInSection section:Int) - > Int{
    let sectionData=tableData[section]
    //### `tableData` element count is used as `numberOfSections` instead of numberOfRows
    // ` The number of elements of `tableData` must be the same as the number of elements of `sectionTitle`
    return sectionData.count
}

There may still be other errors in the details, but I think the above correction will prevent the errors you mentioned in this question.Try it.


2022-09-30 13:59

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.