Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/118.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 无法使用实例成员';电影1';在属性初始值设定项中,属性初始值设定项在';自我';有空吗_Ios_Uitableview - Fatal编程技术网

Ios 无法使用实例成员';电影1';在属性初始值设定项中,属性初始值设定项在';自我';有空吗

Ios 无法使用实例成员';电影1';在属性初始值设定项中,属性初始值设定项在';自我';有空吗,ios,uitableview,Ios,Uitableview,我正在开发一个可扩展的表视图,遇到了一些问题 这对于测试来说很好: var sections = [ ExpandTVSection(genre: "genre1", movies: ["movie1A", "movie1B","movie1C"], expanded: false), ExpandTVSection(genre: "genre2", movies: ["movie2A", "movie2B","movie2C"], expanded: false), E

我正在开发一个可扩展的表视图,遇到了一些问题

这对于测试来说很好:

var sections = [
    ExpandTVSection(genre: "genre1", movies: ["movie1A", "movie1B","movie1C"], expanded: false),
    ExpandTVSection(genre: "genre2", movies: ["movie2A", "movie2B","movie2C"], expanded: false),
    ExpandTVSection(genre: "genre3", movies: ["movie3A", "movie3B","movie3C"], expanded: false)
]
我从Firebase获取数据,所以我尝试设置第一部电影的名称

var movies1 = [String]()
var movies2 = [String]()
var movies3 = [String]()

func fetchMovies(){
    var datref: DatabaseReference!
    datref = Database.database().reference()
    datref.child("movies1").child("names").observe(
        .childAdded,
        with: {
            (snapshot) in         
                if let dictionary = snapshot.value as? [String: AnyObject]{
                    let movie = MoviesInfo(dictionary: dictionary)
                    movie.setValuesForKeys(dictionary)
                    self.movies1.append(movie.Name!)
                }
            },
        withCancel: nil)
}

// !! program breaks here due to movies1 !!

var sections = [
    ExpandTVSection(genre: "genre1", movies: movies1, expanded: false),
    ExpandTVSection(genre: "genre2", movies: ["movie2A", "movie2B","movie2C"], expanded: false),
    ExpandTVSection(genre: "genre3", movies: ["movie3A", "movie3B","movie3C"], expanded: false)
]
一旦我尝试应用它,我的程序就会崩溃:

无法在属性初始值设定项中使用实例成员“movies1”, 属性初始值设定项在“self”可用之前运行

更多可能有用的信息:

struct ExpandTVSection {
    var genre: String!
    var movies: [String]!
    var expanded: Bool!

    init(genre: String, movies: [String], expanded: Bool) {
        self.genre = genre
        self.movies = movies
        self.expanded = expanded
    }
}

Swift不允许您在初始化表达式中为另一个属性(
部分
)使用属性(
movies1
)。相反,您可以在初始值设定项中初始化属性(在调用超类初始值设定项之后),或者在您的情况下,只需执行以下操作:

ExpandTVSection(genre: "genre1", movies: [], expanded: false)
而不是

ExpandTVSection(genre: "genre1", movies: movies1, expanded: false)

因为您的
movies1
是用空数组初始化的。

我通过添加这些行(
self.sections.append(ExpandTVSection(流派:“genre1”,movies:self.movies1,expanded:false))
)(
self.moviesTableView.reloadData()