2
我正在寫一個方法將數據從表格視圖複製到粘貼板,但如果選擇了任何行,它應該專門只複製選定行中的數據,所以我需要遍歷索引集...NSIndexSet.enumerateIndexesUsingBlock使用Swift Closure
func createExportText(fromArrayController:NSArrayController) -> String
{
var indexSet = fromArrayController.selectionIndexes;
/* None were selected, so process them all! */
if (indexSet.count == 0)
{
indexSet = NSIndexSet(indexesInRange: NSMakeRange(0, fromArrayController.arrangedObjects.count));
}
var rows = "";
indexSet.enumerateIndexesUsingBlock
{
// What code goes here?
}
return rows;
}
我在解釋方法簽名到如何編寫閉包代碼時遇到了一些麻煩。我得到的錯誤信息也不是特別清楚(Cannot invoke 'enumerateIndexesUsingBlock' with an argument list of type '((_, _) -> Int)'
)。有人可以幫助我嗎?
編輯:
工作方法:
func createExportText(fromArrayController:NSArrayController) -> String
{
var rows = "";
var indexSet = fromArrayController.selectionIndexes;
let objects = (fromArrayController.arrangedObjects as! NSArray);
/* No rows were selected, so process them all! */
if (indexSet.count == 0)
{
indexSet = NSIndexSet(indexesInRange: NSMakeRange(0, objects.count));
}
indexSet.enumerateIndexesUsingBlock
{
(i, stop) -> Void in
let obj:AnyObject = objects[i];
let str = obj.valueForKey("string") as! String;
rows += "\(str)";
if (i < indexSet.lastIndex) { rows += "\n"; }
}
return rows;
}
感謝提示,Kishikawa-san!我沒有看到它是需要的元組。我已經更新了我上面的方法代碼。 – BadmintonCat