如何在 Objective-C 中表示表格数据

发布于 2024-10-19 14:54:33 字数 915 浏览 7 评论 0原文

表示此数据结构的最佳方式是什么?我使用 Objective-C 进行 iPhone 开发,但总体来说还不错。

             Location1          Location2         Location3
Location1        0                  8                 23
Location2        8                  0                 16
Location3        23                 16                0

我可以做嵌套数组……

table = ['Location1' => ['Location 1' => '0',
                         'Location 2' => '8',
                         'Location 3' => '23'],
         'Location2' => ['Location 1' => '8',
                         'Location 2' => '0',
                         'Location 3' => '16'],
         'Location3' => ['Location 1' => '23',
                         'Location 2' => '16',
                         'Location 3' => '0']]

但我不确定这是最有效的方法,因为有大量数据复制。也许某种面向对象的方法?

此外,是否有任何特殊的 iPhone Objective-C 数据结构适合此类数据?

What is the best way to represent this data structure? I'm using Objective-C for iPhone development but just generally is fine.

             Location1          Location2         Location3
Location1        0                  8                 23
Location2        8                  0                 16
Location3        23                 16                0

I could do nested arrays...

table = ['Location1' => ['Location 1' => '0',
                         'Location 2' => '8',
                         'Location 3' => '23'],
         'Location2' => ['Location 1' => '8',
                         'Location 2' => '0',
                         'Location 3' => '16'],
         'Location3' => ['Location 1' => '23',
                         'Location 2' => '16',
                         'Location 3' => '0']]

... but I'm not sure this is the most efficient way as there's a lot of data replication. Maybe some sort of object-oriented method?

Additionally, are there any special iPhone Objective-C data structures that would lend themselves to this kind of data?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

西瓜 2024-10-26 14:54:33

您可以使用数组的数组,即包含其他包含数据的 NSArray 对象的 NSArray 对象。这种方法的缺点是,由您的程序来强制限制数组的大小,以便每行和每列具有一致的大小。如果您的数据是静态且可 plist 的(仅包含字符串、数字、日期、数据、数组或字典),请考虑将其存储在 plist 文件中,并使用 [NSArray arrayWithContentsOfFile:].

如果您希望使用 Location1 之类的键访问数据,那么您需要使用 NSDictionary 对象而不是 NSArray 对象;但是,这样做您将失去按顺序或通过数字索引访问数据的能力(必须始终通过键访问)。

此外,如果您的数据是可变的(即,您希望能够更改数据),则需要使用 NSMutableArrayNSMutableDictionary

You could use an array of arrays, that is NSArray objects that contain other NSArray objects that contain the data. The downside to this approach is that it is up to your program to enforce limits on the size of the array so that each row and column has a consistent size. If your data is static and plist-able (contains only strings, numbers, dates, data, arrays or dictionaries), consider storing it in a plist file, and loading it in one go using [NSArray arrayWithContentsOfFile:].

If you wish to access the data using keys such as Location1, then you'll need to use NSDictionary objects instead of NSArray objects; however doing this you lose the ability to access the data sequentially or by numerical index (it must always be accessed by key).

Also, if your data is mutable (that is, you want to be able to alter the data), you'll need to use NSMutableArray or NSMutableDictionary.

夜深人未静 2024-10-26 14:54:33

我建议创建一个 NSobject 位置类,其中包含成员 location1、location2 和 location3。然后您可以使用位置对象来获取如下值:
[位置对象位置1];
稍后您也可以制作这些对象的 nsarray 或 mutablearray

I would recommend to make an NSobject class of location, with members location1, location2 and location3. and then you can use location object to get the values like:
[locationobj location1];
later On you can make nsarray or mutablearray of these objects too

旧夏天 2024-10-26 14:54:33

您可以创建一个在内部使用 NSMutableArray 的 Mutable2DArray 类,使用 2D 索引和 1D 索引或标记之间的转换:

index1D = numColumns * row2DIndex + column2DIndex;

您甚至可以在内部使用纯 C 数组,具体取决于表格数据类型和其他要求(重用性、可扩展性、效率、内存使用情况等)

You could create a Mutable2DArray class which used an NSMutableArray internally, using a conversion between 2D indexes and a 1D index or tag:

index1D = numColumns * row2DIndex + column2DIndex;

You could even use plain C arrays internally, depending on the tabular data type and other requirements (reuse, extensibility, efficiency, memory usage, etc.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文