我正在尝试为iOS构建电影存档应用。但是,当我尝试使用在另一个控制器中定义的类时,出现此错误。
输入“电影?”没有成员“姓名”
我有4个Controller文件;
和2个.swift文件;
Movie.swift:
class Movie: NSObject {
var name : String
var director : String
var year : String
var posterURL : String
override init(){
self.name = ""
self.director = ""
self.year = ""
self.posterURL = ""
}
}
当我像应有的那样使用movieListVC时,可以上课。
movieListVC:
var selectedMovie : Movie?
.
.
.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.selectedMovie = Movie()
let temp = self.movies[indexPath.row] as! Dictionary<String, AnyObject>
selectedMovie!.name = temp["name"]! as! String
selectedMovie!.director = temp["director"] as! String
selectedMovie!.year = temp["year"] as! String
selectedMovie!.posterURL = temp["posterURL"] as! String
self.performSegue(withIdentifier: "detail", sender: self)
let movieName = temp["name"] as! String
print("Name of the selected movie: \(movieName)")
}
Xcode没有为我的movieListVC提供任何错误,但是当我在movieDetailVC中使用相同的东西时,它给了我错误。
movieDetailVC:
var movieToShow : Movie? // Expected member name or constructor call after type name
.
.
.
override func viewDidLoad() {
super.viewDidLoad()
if (self.movieToShow == nil) {
return;
}
labelMovieName.text = movieToShow!.name // Type 'Movie?' has no member 'name'
labelMovieDirector.text = movieToShow!.director // Type 'Movie?' has no member 'name'
labelMovieYear.text = movieToShow!.year // Type 'Movie?' has no member 'name'
let image = UIImage(data: try! Data(contentsOf:
URL(string: movieToShow!.posterURL))) // Type 'Movie?' has no member 'name'
imageViewPoster.image = image
}
编辑1:我也可以在此处使用movieToShow获取信息:
override func prepare(for segue: UIStoryboardSegue?, sender: Any!) {
if segue!.identifier == "detail" {
let viewController:movieDetailViewController = segue!.destination as! movieDetailViewController
viewController.movieToShow = self.selectedMovie
}
}
答案 0 :(得分:1)
在movieDetailVC
中,
labelMovieName.text = movieToShow!.name. // Type 'Movie?' has no member 'name'
在上面的行中,您在.
之后使用了另外的 name
。删除它。