简体   繁体   English

在Swift中用自定义对象填充数组

[英]Fill an Array with Custom Objects in Swift

For my Current project I'd like to fill a TableView from my CoreData DB. 对于我当前的项目,我想从CoreData DB中填充一个TableView。

I've a Class named Movies(movieNAme, cinemaName, rate) 我有一个名为Movies(movieNAme,cinemaName,rate)的课程

Thanks to this function : 由于此功能:

    var appDel:AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
    let context:NSManagedObjectContext = appDel.managedObjectContext
    var request:NSFetchRequest = NSFetchRequest(entityName: "Movies")
    myMovies = context.executeFetchRequest(request, error: nil)

However, I'd like to specify the type of the objects in this array(I want an Array of object Movie) 但是,我想指定此数组中对象的类型(我想要一个对象Movie的数组)

I've tried that 我试过了

    var myMovies:Array<Films> ...

but with the Array type, I don't have access to the objectAtIndex method 但是使用数组类型,我无权访问objectAtIndex方法

Any ideas to solve this issue ? 有解决这个问题的想法吗?

Thanks 谢谢

Thanks for your answer ! 感谢您的回答 ! But I decided to use this instead : 但是我决定改用这个:

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath:    NSIndexPath) -> UITableViewCell {
    let cell:MainTableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as MainTableViewCell

    println((myMovies.objectAtIndex(indexPath.row) as Movies).name)
    cell.CellTitle.text = (myMovies.objectAtIndex(indexPath.row) as Movies).MovieName)
    return cell
}

But it doesn't work either... 但这也不起作用...

EDIT : It works now ! 编辑:现在可以使用! I missed to link my class to my Entity in CoreData ! 我错过了将类链接到CoreData中的Entity的功能! Sorry 抱歉

您应该能够执行myMovies [0],与objectAtIndex一样,没有objectAtIndex的原因是因为它是一个Swift数组,您可以使用[]来访问索引

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM