c# - 在 Visual Studio 中设置 TableView 时出现公共(public)列表错误

标签 c# xcode macos visual-studio cocoa

我正在尝试在 Xcode 和 Visual Studio 中处理 TableView ,并将其设置为导入数据,但列表中不断出现错误。

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;
        }
    }
}

每当我选择它来查看问题所在时,这就是我得到的结果:

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 类的通用 () 部分可以读作“of”或“for”,例如 List<T>将被读作“T 列表”,List<int>将是“整数列表”。和ViewModel<TView>将被读作“TView 的 ViewModel”

关于c# - 在 Visual Studio 中设置 TableView 时出现公共(public)列表错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58336107/

相关文章:

c# - Request.Files 始终为空

c# - 如何在四边形中找到一个随机点?

objective-c - NSRunAlertPanel 导致内存泄漏

objective-c - Cocoa NSBrowser 和 CFURLEnumeratorCreateDirectoryURL

ios - 如何使用 HP SCA 扫描我的 iOS 项目并获取报告

c# - Xamarin 表格 : This functionality is not implemented in the portable version of this assembly

c# - 命名类型不用于构造函数注入(inject)

objective-c - 找不到 SystemConfiguration/SystemConfiguration.h 文件

ios - dyld:未加载库:@rpath/libswiftCore.dylib

ios - XCODE 错误 : "...the field reciever doesn' t exist in your schema (Code: 102, 版本 : 1. 7.5)。”