2011-12-05 17 views
2

定位的iOS 5.0我看到SIGABRT当我尝试访问NSIndexPath对象的行或部分的属性在以下代码:nsindexpath访问行到section属性导致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?

谢谢先进。

回答

3

尝试创建使用indexPathForRow:inSection:

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

谢谢。抱歉:我通过课程参考第一次错过了这个笔记。任何想法他们为什么不在类方法下列出这个方法? – stephen

+0

这是因为这些方法被定义为NSIndexPath上的一个类别,而不是NSIndexPath类本身。你可以在这里找到文档:http://developer.apple.com/library/ios/#documentation/uikit/reference/NSIndexPath_UIKitAdditions/Reference/Reference.html –

2

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

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

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

0

我认为问题在于indexPathWithIndex:创建单节点索引路径。与initWithIndex相同:. section和row属性从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”是什么意思?

+0

嗨。感谢您的反馈意见。我不太清楚。我的意思是在NSIndexPath实例数据成员上操作。 '在对象上运行'在C++中相当普遍。这在obj-c中不是标准约定吗? – stephen

+1

“在对象上运行”并不是我在Objective-C中听到的表达式。 (我不记得在C++语境中听到过这个问题。)Objective-C独特的表达方式是“向对象发送一条消息”,这意味着调用一个对象的实例方法。 –