项目转换为 Swift 3: "Type [Any] has no subscript members" 后



我最近将我的项目迁移到 Swift 3.0,现在收到错误"类型 [Any] 没有下标成员"。我的视图控制器和模型.swift类已附加。你可以在下面看到。

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath) as UITableViewCell

它在表视图中控制器...

    // Configure the cell...
    let rowArray:AnyObject  = (model.allEmployees() [indexPath.row])
    print("ID = (rowArray[0])")
    let fName = rowArray[1] as! String
    let lName = rowArray[2] as! String
    let fullName = "(fName)(lName)"
    let phone = rowArray[3] as! String
    cell.textLabel?.text = fullName
    cell.textLabel?.text = phone
    return cell
}

这是我的模型.swift类:

-(NSArray *) allEmployees
{
    [self openDB];
    NSMutableArray *employeeData = [[NSMutableArray alloc] initWithCapacity:5];
    //prepare the select query:
    NSString *selectSQL = @"SELECT names.ID, firstName, lastName, phone FROM Name INNER JOIN Phones WHERE Names.ID = phones.NamesID";
    const char *query = [selectSQL UTF8String];
    sqlite3_stmt *query_stmt = NULL;
    if (sqlite3_prepare_v2(database, query, -1, &query_stmt, NULL) != SQLITE_OK) {
        NSLog(@"error preparing %@", selectSQL);
        return nil;
    }
    while (sqlite3_step(query_stmt) == SQLITE_ROW) {
        // for each row, bind the values to c types, then
        //convert to objects
    long long ID = sqlite3_column_int(query_stmt, 0);
    char *fName = (char *) sqlite3_column_text(query_stmt, 1);
    char *lName = (char *) sqlite3_column_text(query_stmt, 2);
    char *pNumber = (char *) sqlite3_column_text(query_stmt, 3);
    NSNumber *rowID = [NSNumber numberWithLongLong:ID];
    NSString *firstName = [[NSString alloc] initWithUTF8String:fName];
    NSString *lastName = [[NSString alloc] initWithUTF8String:lName];
    NSString *phoneNumber = [[NSString alloc] initWithUTF8String:pNumber];
    //marshal the data:
    NSArray *entry = @[rowID,firstName,lastName,phoneNumber];
    [employeeData addObject:entry];
}
[self closeDB];
return employeeData;

像这样使用:

if let employees = model.allEmployees() as? [[Any]] {
    let rowArray = employees[indexPath.row]
    let fName = rowArray[1] as! String
    let lName = rowArray[2] as! String
    let fullName = "(fName)(lName)"
    let phone = rowArray[3] as! String
    cell.textLabel?.text = fullName
    cell.textLabel?.text = phone
}

相关内容

最新更新