简体   繁体   English

需要帮助了解NSDictionary对象

[英]Need help understanding NSDictionary Objects

im trying to wrap my head around the NSDictionary class, to make my TableView functional. 我试图把我的头缠在NSDictionary类上,以使TableView正常运行。 Is anyone able to break down and explain whats happening in the following code for me? 有谁能够分解并解释以下代码中发生的事情? cheers 干杯

if (!self.tableDataSource)
    {
        self.tableDataSource = [NSArray arrayWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @"MenuData" ofType: @"plist"]];
        self.navigationItem.title = @"Menu";

    } 

NSDictionary *dictionary = [[[self.tableDataSource objectAtIndex: indexPath.section] objectForKey: @"Children"] objectAtIndex: indexPath.row];

You should set the DataSource and Delegate to point to the tableView's owner and then implement the UITableViewDelegate functions to correctly set the content of the table. 您应该将DataSource和Delegate设置为指向tableView的所有者,然后实现UITableViewDelegate函数以正确设置表的内容。

Edit: If you're planning on implementing a table view with sections, I advise you to use a MxN datastructure of: NSArray of NSArray, that way you will be able to access vey easily your cell data with: 编辑:如果您打算实现带有部分的表视图,我建议您使用以下MxN数据结构:NSArray NSArray,这样您就可以通过以下方式轻松访问vey单元数据:

cellData = [[datasource objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];

I think the code itself is fairly clear if you just unpack all the method calls to see the implied nesting of various objects. 我认为,如果只是解压缩所有方法调用以查看各种对象的隐式嵌套,代码本身就很清楚。 Comments: 评论:

// if the current object's tableDataSource property has not been defined yet
if (!self.tableDataSource) {
    // then define it.

    // Set it to be an NSArray initialized by reading ...
    self.tableDataSource = [NSArray arrayWithContentsOfFile: 
    // ... the plist file MenuData.plist found within the application's bundle
                      [[NSBundle mainBundle] pathForResource: @"MenuData" ofType: @"plist"]];

    // and also set the current object's property nagitationItem.title to "Menu"
    self.navigationItem.title = @"Menu";

} 

// Now create a dictionary

// Get this dictionary by ...

// going to the element numbered indexPath.section in the tableDataSource
NSDictionary *dictionary = [[[self.tableDataSource objectAtIndex: indexPath.section] 
                  // (assume that element is an NSDictionary)
                  // get that element's value stored witht the key "Children"
                  objectForKey: @"Children"] 
                 // (assume that retrieved value is an NSArray)
                 // then get the element numbered indexPath.row from that array
                 objectAtIndex: indexPath.row];
                             // expect that value to be an NSDictionary

I think the Apple docs on TableDataSource will probably give you the larger context on what this code is doing. 我认为TableDataSource上的Apple文档可能会为您提供有关此代码正在执行的操作的较大上下文。 In particular, the nesting of a dictionary, in an array, in a dictionary, in an array, which is what's going on in the last line, may be a standard way of encoding all the info for rows, columns, etc., for data driving a UITableView. 特别是,将字典嵌套在数组中,在字典中,在数组中嵌套(这是最后一行的内容),可能是对行,列等所有信息进行编码的标准方法,数据驱动UITableView。

NSDictionary objects store other objects referenced by keys. NSDictionary对象存储键引用的其他对象。 A key is a unique string used to reference the object in the dictionary. 键是用于引用字典中对象的唯一字符串。 An object can be any object. 一个对象可以是任何对象。 An NSMutableDictionary is the same thing but allows editing (adding/removing/changing) objects for keys after the dictionary has been created. NSMutableDictionary是同一件事,但是允许在创建字典之后编辑(添加/删除/更改)键的对象。

It is incorrect to set an NSDictionary (or any class that does not support the UITableViewDataSource protocol) to the dataSource property of a table view. 将NSDictionary(或任何不支持UITableViewDataSource协议的类)设置为表视图的dataSource属性是不正确的。 Most commonly, your view controller is a UITableViewController subclass or it is a UIViewController subclass that supports the UITableViewDelegate or UITableViewDataSource property. 最常见的是,视图控制器是UITableViewController子类,或者它是支持UITableViewDelegateUITableViewDataSource属性的UIViewController子类。

That's the direct answer to your question. 那是您问题的直接答案。 For a fuller understanding you should go back and read Apple's docs to make sure you understand my answer. 为了更全面的了解,您应该返回并阅读Apple的文档,以确保您理解我的回答。 The Table View Controller Programming Guide will show you how to implement a table views in a view controller. 《 Table View Controller编程指南》将向您展示如何在视图控制器中实现表视图。 For other things you made need info on Objective-C and other iOS objects. 对于其他事情,您需要获得有关Objective-C和其他iOS对象的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM