nsindexpath 访问行到节属性会导致 SIGABRT?

发布于 2024-12-19 10:00:14 字数 726 浏览 5 评论 0原文

针对 iOS 5.0,当我尝试在以下代码中访问 NSIndexPath 对象的行或部分属性时,我看到 SIGABRT:

NSIndexPath* path = [[NSIndexPath alloc] initWithIndex:0];
int row = path.row; //SIGABRT
int section = path.section;//SIGABRT

此示例建议使用 indexPathWithIndex: 相反 http://developer.apple.com/library/mac/#samplecode/SourceView/Listings/BaseNode_m.html#//apple_ref/doc/uid/DTS10004441-BaseNode_m-DontLinkElementID_6

path = [NSIndexPath indexPathWithIndex:0];

结果保持一致。有谁能解释为什么会发生这种情况?难道iOS 5.0中我们就不能操作NSIndexPath了吗?

提前致谢。

Targeting iOS 5.0 I see SIGABRT when I try to access row or section properties of NSIndexPath object in the following code:

NSIndexPath* path = [[NSIndexPath alloc] initWithIndex:0];
int row = path.row; //SIGABRT
int section = path.section;//SIGABRT

This example suggests using indexPathWithIndex: instead http://developer.apple.com/library/mac/#samplecode/SourceView/Listings/BaseNode_m.html#//apple_ref/doc/uid/DTS10004441-BaseNode_m-DontLinkElementID_6

path = [NSIndexPath indexPathWithIndex:0];

The results remain consistent. Does anyone have an explanation of why this might happen? Is it that we just cannot operate on NSIndexPath in iOS 5.0?

Thanks in advanced.

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

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

发布评论

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

评论(3

阳光①夏 2024-12-26 10:00:14

尝试使用 indexPathForRow:inSection: 创建索引路径

NSIndexPath* path = [NSIndexPath indexPathForRow:0 inSection:0];

Try creating the index path using indexPathForRow:inSection:

NSIndexPath* path = [NSIndexPath indexPathForRow:0 inSection:0];
活泼老夫 2024-12-26 10:00:14

我唯一能想到的就是确保您将 UIKit.framework 作为您的框架之一。您可以通过按目标上“构建阶段”下的“链接二进制文件与库”部分中的加号按钮来完成此操作。另外,请确保在尝试访问索引路径的文件中导入 NSIndexPath.h

我以前遇到过这个问题,这解决了它。这是因为虽然 NSIndexPath 在 Cocoa 和 CocoaTouch 之间是通用的,但 Cocoa 版本没有属性 rowsection

如果您已经有 UIKit.framework,请尝试使用 [NSIndexPath indexPathForRow:row inSection:section] 而不是 [NSIndexPath indexPathWithIndex:index]

The only thing I can think of is to make sure you have UIKit.framework as one of your frameworks. You can do this by pressing the plus button in the "Link Binary With Libraries" section under "Build Phases" on your target. Also, make sure that you import NSIndexPath.h in the file you're trying to access the index path from.

I've run into this problem before, and this solved it. That's because although NSIndexPath is universal between Cocoa and CocoaTouch, the Cocoa version doesn't have properties row and section.

If you already have UIKit.framework, try using [NSIndexPath indexPathForRow:row inSection:section] instead of [NSIndexPath indexPathWithIndex:index].

走过海棠暮 2024-12-26 10:00:14

我认为问题在于 indexPathWithIndex: 创建一个单节点索引路径。与 initWithIndex: 相同。部分和行属性来自 UITableView 类别到 NSIndexPath 类。单节点索引路径对于与表视图一起使用无效,因为传递给表视图的索引路径必须包含两个指定节和行的索引(根据我运行代码时收到的错误消息)。以下代码创建一个具有两个索引的索引路径,并且运行时不会抛出 NSInternalInconsistencyException:

NSUInteger x[] = {0 , 0};
NSIndexPath *path = [[NSIndexPath alloc] initWithIndexes: x length: 2];
int row = [path row];
int section = [path section];
row = path.row;
section = path.section;

这就是您所说的“在 iOS 5.0 中对 NSIndexPath 进行操作”的意思吗?

I think the problem is that indexPathWithIndex: creates a one-node index path. Same for initWithIndex:. The section and row properties come from the UITableView category to the NSIndexPath class. A one-node index path is invalid for use with a table view because index paths passed to table views must contain two indices specifying the section and row (according to the error message I get when I run your code). The following code creates an index path with two indices and runs without throwing an NSInternalInconsistencyException:

NSUInteger x[] = {0 , 0};
NSIndexPath *path = [[NSIndexPath alloc] initWithIndexes: x length: 2];
int row = [path row];
int section = [path section];
row = path.row;
section = path.section;

Is this what you meant by "operate on NSIndexPath in iOS 5.0"?

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