如何在 Objective-C 中表示表格数据
表示此数据结构的最佳方式是什么?我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用数组的数组,即包含其他包含数据的
NSArray
对象的NSArray
对象。这种方法的缺点是,由您的程序来强制限制数组的大小,以便每行和每列具有一致的大小。如果您的数据是静态且可 plist 的(仅包含字符串、数字、日期、数据、数组或字典),请考虑将其存储在 plist 文件中,并使用[NSArray arrayWithContentsOfFile:].
如果您希望使用
Location1
之类的键访问数据,那么您需要使用NSDictionary
对象而不是NSArray
对象;但是,这样做您将失去按顺序或通过数字索引访问数据的能力(必须始终通过键访问)。此外,如果您的数据是可变的(即,您希望能够更改数据),则需要使用
NSMutableArray
或NSMutableDictionary
。You could use an array of arrays, that is
NSArray
objects that contain otherNSArray
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 useNSDictionary
objects instead ofNSArray
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
orNSMutableDictionary
.我建议创建一个 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
您可以创建一个在内部使用 NSMutableArray 的 Mutable2DArray 类,使用 2D 索引和 1D 索引或标记之间的转换:
您甚至可以在内部使用纯 C 数组,具体取决于表格数据类型和其他要求(重用性、可扩展性、效率、内存使用情况等)
You could create a Mutable2DArray class which used an NSMutableArray internally, using a conversion between 2D indexes and a 1D index or tag:
You could even use plain C arrays internally, depending on the tabular data type and other requirements (reuse, extensibility, efficiency, memory usage, etc.)