Swift - 从<anyobject>数组构造一个句子

时间:2015-09-01 12:06:15

标签: ios swift for-loop

美好的一天!我正在构建一个需要根据Core Data的数据构建句子的应用程序。我有这个:

var players = Array<AnyObject> = []
@IBOutlet weak var sentenceLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        let appDelegate: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        let context: NSManagedObjectContext = appDelegate.managedObjectContext!
        let freq = NSFetchRequest(entityName: "Orders")
        players = context.executeFetchRequest(freq, error: nil)!
      }


func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return players.count
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCellWithIdentifier("checkCell") as! SummaryCustomCell!
    if cell == nil {
        cell = SummaryCustomCell(style: UITableViewCellStyle.Default, reuseIdentifier: "checkCell")
    }
    var data: NSManagedObject = players[indexPath.row] as! NSManagedObject

    cell.playerNameLabel.text = data.valueForKey("playerName") as? String
    cell.teamLabel.text = data.valueForKey("team") as? String
    cell.yearsLabel.text = data.valueForKey("yearsOfPlaying") as? String

    for var i = 0; i < players.count;i++ {
            var dataLooped: NSManagedObject = myOrders[i] as! NSManagedObject
            var playerName = dataLooped.valueForKey("playerName") as? String
            var team = dataLooped.valueForKey("team") as? String
            var years = dataLooped.valueForKey("yearsOfPlaying") as? String

            var constructedSentence: NSString = NSString(format: "%@ was playing for %@ for %@ years.", playerName, team, years)
                sentenceLabel.text = constructedSentence as! String

            }

        }


    return cell
}

然后它只给出数据库中的最后一行。它必须在单个字符串中循环句子。例如。 "Kobe Bryant was playing for LA Lakers for 5 years; Lebron James was playing for Cavs for 3 years." and so on..

我该如何实现?非常感谢你!

0 个答案:

没有答案