需要帮助理解 NSDictionary 对象

发布于 2024-10-31 04:22:59 字数 471 浏览 4 评论 0原文

我试图围绕 NSDictionary 类进行思考,以使我的 TableView 发挥作用。 有谁能够为我分解并解释以下代码中发生的情况吗?干杯

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

im trying to wrap my head around the NSDictionary class, to make my TableView functional.
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];

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

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

发布评论

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

评论(3

各自安好 2024-11-07 04:22:59

我认为如果您只是解压所有方法调用以查看各种对象的隐含嵌套,那么代码本身就相当清晰了。评论:

// 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

我认为苹果文档 TableDataSource< /a> 可能会为您提供有关此代码正在执行的操作的更大上下文。特别是,字典、数组、字典、数组的嵌套(这就是最后一行中发生的情况)可能是对行、列等的所有信息进行编码的标准方法,例如数据驱动 UITableView。

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. 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.

初心 2024-11-07 04:22:59

您应该将 DataSource 和 Delegate 设置为指向 tableView 的所有者,然后实现 UITableViewDelegate 函数以正确设置表的内容。

编辑:
如果您计划实现带有部分的表视图,我建议您使用 MxN 数据结构:NSArray of NSArray,这样您就可以通过以下方式轻松访问单元格数据:

cellData = [[datasource objectAtIndex:indexPath.section] 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.

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:

cellData = [[datasource objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
零度℉ 2024-11-07 04:22:59

NSDictionary 对象存储键引用的其他对象。键是用于引用字典中的对象的唯一字符串。对象可以是任何对象。 NSMutableDictionary 是相同的东西,但允许在创建字典后编辑(添加/删除/更改)键对象。

将 NSDictionary(或任何不支持 UITableViewDataSource 协议的类)设置为表视图的 dataSource 属性是不正确的。最常见的是,您的视图控制器是 UITableViewController 子类,或者是支持 UITableViewDelegateUITableViewDataSourceUIViewController 子类。财产。

这就是你问题的直接答案。为了更全面地理解,您应该返回并阅读 Apple 的文档,以确保您理解我的答案。表视图控制器编程指南将向您展示如何在视图控制器中实现表视图。对于您制作的其他东西,需要有关 Objective-C 和其他 iOS 对象的信息。

NSDictionary objects store other objects referenced by keys. 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.

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. Most commonly, your view controller is a UITableViewController subclass or it is a UIViewController subclass that supports the UITableViewDelegate or UITableViewDataSource property.

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. The Table View Controller Programming Guide will show you how to implement a table views in a view controller. For other things you made need info on Objective-C and other iOS objects.

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