在pageViewController中填充List <t>

时间:2015-11-16 17:27:52

标签: ios swift realm

我有一个League类,其中包含匹配列表。这些匹配我想填充到pageViewController。但是我一直收到以下错误: Cannot invoke 'indexOf' with argument list of type (match)。我想这是因为items是联盟类型的事实。但是我设置了['matches']所以不应该填写所有匹配项吗?那么为什么我不能使用indexOf以及为什么不匹配?

可变

var items: Results<League>?

领域模型

class League:Object {
    dynamic var id: Int = 0
    dynamic var name: String? = ""
    var matches = List<Match>()


    override class func primaryKey() -> String {
        return "id"
    }
}

class Match:Object {
    dynamic var matchId: Int = 0
    dynamic var date: NSDate = NSDate()
    dynamic var homeName: String? = ""
    dynamic var awayName: String? = ""
    dynamic var homeAcro: String? = ""
    dynamic var awayAcro: String? = ""
    dynamic var awayScore: Int = 0
    dynamic var homeScore: Int = 0
    dynamic var leagueName: String? = ""
    dynamic var homeLogo: NSData = NSData()
    dynamic var awayLogo: NSData = NSData()

    override class func primaryKey() -> String {
        return "matchId"
    }

}

pageViewController委托方法

extension ViewController: UIPageViewControllerDataSource {
    func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
        let currentText = (viewController as! ScoreViewController).match

        var currentIndex = items!["matches"].indexOf(currentText!)
        if currentIndex == items!["matches"].count - 1 {
            return viewControllerAtIndex(0)
        } else {
            return viewControllerAtIndex(++currentIndex!)
        }
    }

    func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
        let currentText = (viewController as! ScoreViewController).match
        var currentIndex = items!.indexOf(currentText!)
        if currentIndex == 0 {
            return viewControllerAtIndex(items!["matches"].count - 1)
        } else {
            return viewControllerAtIndex(--currentIndex!)
        }
    }

    func viewControllerAtIndex(index: Int) -> UIViewController {

        let contentViewController = storyboard?.instantiateViewControllerWithIdentifier("ScoreViewController") as! ScoreViewController
        contentViewController.match = self.items!["matches"][index]
        return contentViewController
    }







}

项目已初始化

for (_, item) in result {


    if let leagueId = item["league"].int,
        let leagueName = item["name"].string,
        let allMatches = item["matches"].array {


            let leagueObject = League()
            leagueObject.name = leagueName
            leagueObject.id = leagueId



            for match in allMatches {
                if let matchId = match["matchId"].int,
                    let tournament = match["tournament"].string,
                    let homeTeam = match["homeName"].string,
                    let awayTeam = match["awayName"].string,
                    let homeScore = match["homeScore"].int,
                    let awayScore = match["awayScore"].int,
                    let homeLogo = match["homeLogo"].string,
                    let awayLogo = match["awayLogo"].string,
                    let date = match["date"].string,
                    let homeAcro = match["homeAcro"].string,
                    let awayAcro = match["awayAcro"].string{

                        if let awayLogoUrl = NSURL(string: awayLogo),
                            let homeLogoUrl = NSURL(string: homeLogo) {

                                if let awayLogoData = NSData(contentsOfURL: awayLogoUrl),
                                    let homeLogoData = NSData(contentsOfURL: homeLogoUrl) {

                                        let matchObject = Match()
                                        matchObject.matchId = matchId
                                        matchObject.leagueName = tournament
                                        matchObject.homeName = homeTeam
                                        matchObject.awayName = awayTeam
                                        matchObject.homeScore = homeScore
                                        matchObject.awayScore = awayScore
                                        matchObject.homeLogo = homeLogoData
                                        matchObject.awayLogo = awayLogoData
                                        matchObject.homeAcro = homeAcro
                                        matchObject.awayAcro = awayAcro


                                        let formatter = NSDateFormatter()
                                        formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
                                        formatter.timeZone = NSTimeZone(abbreviation: "CET")
                                        matchObject.date = formatter.dateFromString(date)!



                                        leagueObject.matches.append(matchObject)

                                }
                        }


                }






            }


            try! realm.write {
                realm.add(leagueObject, update: true)
            }


    }



}

1 个答案:

答案 0 :(得分:0)

items的类型是Results<League>,它不允许将字符串作为下标参数。它应该给你这个错误。

Cannot subscript a value of type 'Results<League>' with an index of type 'String'

我认为主要问题是Results<League>是多个联盟的集合,但您尝试将其视为一个League

在不知道您的查询内容的情况下,您可以使用League中的第一个items

let league = items!.first!
var currentIndex = league.matches.indexOf(currentText!)

但是,如果你想要League来自items,可以像这样访问它们。

let league = items![number] // number can be anything in the range 0..<items.count
var currentIndex = league.matches.indexOf(currentText!)
相关问题