【问题标题】:Populating a List<t> in a pageViewController在 pageViewController 中填充 List<t>
【发布时间】:2015-11-16 17:27:52
【问题描述】:

我有一个包含匹配列表的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)
            }


    }



}

【问题讨论】:

  • 你试过items!.matches.indexOf(currentText!) 吗?根据您的描述,items! 不是字典。
  • 是,但项目没有名为matches的成员
  • 你能说明items的初始化位置吗?
  • 我现在已经添加了,顺便说一句,我正在使用 realm.io 作为数据库
  • 下面的答案显示了您的解决方案。

标签: ios swift realm


【解决方案1】:

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

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

我认为主要问题是 Results&lt;League&gt; 是多个联赛的集合,但您试图将其视为单个 League


在不知道您的查询是什么的情况下,您可以像这样使用items 中的第一个League

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

但是,如果您想要与 items 不同的 League,您可以像这样访问它们。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多