Public 在 Visual Studio 中设置 table 视图时出现列表错误

Public List error when setting up a table view in Visual Studio

我正在尝试在 Xcode 和 Visual studio 中处理 table 视图,并将其设置为导入数据,但列表中一直出现错误.

using System.Collections.Generic;
using AppKit;

namespace HCATester
{
    public class NormsLogDataSource : NSTableViewDataSource
    {
        public NormsLogDataSource(){}

        public List Norms = new List();
        public override nint GetRowCount(NSTableView tableView)
        {
            return Norms.Count;
        }
    }
}

每当我 select 它看看有什么问题时,这就是我得到的:

Implements the System.Collections.Generic.IList interface. The size of a List is dynamically increased as required. A List is not guaranteed to be sorted. It is the programmer's responsibility to sort the List prior to performing operations (such as BinarySearch) that require a List to be sorted. Indexing operations are required to perform in constant access time; that is, O(1).

您的问题出在 List 的声明上。您正在使用 System.Collections.Generic 命名空间,它(在其他集合中)包含通用 List<T>。但是您没有指定泛型类型参数。

你看,在我们拥有泛型之前,我们使用了 ArrayList,它只包含 objects 的集合,所以我们总是需要将它转换为我们真正想要的。例如,如果我们想要一个整数的 ArrayList,我们可以这样声明我们的列表:

ArrayList list = new ArrayList();
list.Add(1);
list.Add(2);
list.Add(3);

但是在使用它时,我们需要将我们的项目从 object 转换为我们想要的任何类型,例如:

int item1 = (int) list[0];
int item2 = (int) list[1];
// ...

这会让人感到困惑并且容易出错,例如,如果方法采用 ArrayList 作为参数,您将始终需要确保所述 ArrayList 的每个元素都是正确的类型。
在泛型和泛型 List<T> 中,我们可以用它来定义强类型列表。与上面相同的示例,但使用 List 更容易阅读和理解:

List<int> list = new List<int>();
list.Add(1);
list.Add(1);
list.Add(1);

int item1 = list[0];
int item2 = list[1];
// ...

现在我们不需要转换我们的对象,因为我们已经知道它是什么数据类型。这也增加了更多的安全性,因为我们可以定义列表中哪些类型是有效的。

现在回答你的问题,你需要指定你正在使用的数据类型,你说 string 所以我会告诉你如何。您需要做的就是将声明列表的行替换为:

public List<string> Norms = new List<string>();

P.S class 的通用 () 部分可以读作 "of" 或 "for",例如 List<T> 将读作 "A List of T",List<int> 将是 "A List of Integers"。 ViewModel<TView> 将被读作 "A ViewModel for TView"