2011-12-05 14 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獨特的表達方式是「向對象發送一條消息」,這意味着調用一個對象的實例方法。 –