如何在Swift中转换NSArray的元素类型

时间:2014-07-16 11:16:02

标签: swift

我创建了NSFetchedResultsController

let fetchRequest = NSFetchRequest(entityName: "User")
        fetchRequest.sortDescriptors = [NSSortDescriptor(key: "recordName", ascending: true)]

        _frcU = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: _managedObjectContext, sectionNameKeyPath: nil, cacheName: nil)
        _frcU!.delegate = self
        _frcU!.performFetch(nil)

然后在numberOfRowsInSection方法中,我想检查第一部分中的行号

    override func tableView(tableView: UITableView?, numberOfRowsInSection section: Int) -> Int {

            var numberOfRowsInSection = 0
            if _frcU {
/*here something  wrong -> */ let o: NSFetchedResultsSectionInfo = _frcU?.sections().firstObject as NSFetchedResultsSectionInfo
                numberOfRowsInSection = o.numberOfObjects
            }
        }

但它说:ST6(firstObject)不符合协议' NSFetchedResultsSectionInfo'

为什么,我应该如何改写呢?

2 个答案:

答案 0 :(得分:0)

工作解决方案是:

numberOfRowsInSection = ((_frcU!.sections as NSArray).firstObject as NSFetchedResultsSectionInfo).numberOfObjects

有两个基本区别

1)!标记

2)“施放”到NSArray

答案 1 :(得分:0)

在这里你可以这样做:

    let sectionInfo = _frcU.sections[section] as NSFetchedResultsSectionInfo
    let numberOfRowsInSection = sectionInfo.numberOfObjects

您可以在xCode中查看默认的Master-Detail Application模板。可以在Swift中创建项目,因此有很多东西可以学习如何在Swift中完成任务。