有人可以在Swift中发布MVC模式的好例子吗?
问题描述:
一个实现了MVC模式的简单项目.到目前为止,我对它的状态有一个简短的了解,但我想看看实际的实现.
A simple project which has implemented the MVC pattern. So far,I have a brief understanding of how it is like but I want to see the practical implementation.
答
这是Swift中Model View Controller的典型示例:
This is a typical example of Model View Controller in swift:
class Article {
var title: String
var body: String
var date: NSDate
var thumbnail: NSURL
var saved: Bool
}
class ArticleViewController: UIViewController {
var bodyTextView: UITextView
var titleLabel: UILabel
var dateLabel: UILabel
var article: Article {
didSet {
titleLabel.text = article.title
bodyTextView.text = article.body
let dateFormatter = NSDateFormatter()
dateFormatter.dateStyle = NSDateFormatterStyle.ShortStyle
dateLabel.text = dateFormatter.stringFromDate(article.date)
}
}
}