Swift:延迟加载变量与 MVC 模型?
Swift: Lazy load variable vs MVC model?
我正在使用 MVC 模型构建应用程序。
我使用延迟加载技术来填充一个变量。 (模型)
而这个变量是由一个 UIViewController (Controller)
但我不知道如何在模型操作完成后重新加载或触发视图控制器。这是我的代码
模型(延迟加载数据)
class func allQuotes() -> [IndexQuotes]
{
var quotes = [IndexQuotes]()
Alamofire.request(.GET, api_indexquotes).responseJSON { response in
if response.result.isSuccess && response.result.value != nil {
for i in (response.result.value as! [AnyObject]) {
let photo = IndexQuotes(dictionary: i as! NSDictionary)
quotes.append(photo)
}
}
}
return quotes
}
以及视图控制器的部分
class 索引:
UIViewController,UICollectionViewDelegate,UICollectionViewDataSource {
var quotes = IndexQuotes.allQuotes()
var collectionView:UICollectionView!
override func viewDidLoad() {
这是一个非常严肃的问题,我很困惑将使用什么技术来完全满足我的目的?
这是异步操作,这里只需要使用回调:
class func allQuotes(callback: () -> Void) -> [IndexQuotes]
{
var quotes = [IndexQuotes]()
Alamofire.request(.GET, api_indexquotes).responseJSON { response in
if response.result.isSuccess && response.result.value != nil {
for i in (response.result.value as! [AnyObject]) {
let photo = IndexQuotes(dictionary: i as! NSDictionary)
quotes.append(photo)
}
}
callback()
}
return quotes
}
在您的 UIViewController 中:
var quotes = IndexQuotes.allQuotes() {
self.update()
}
var collectionView:UICollectionView!
override func viewDidLoad() {
update()
}
private func update() {
// Update collection view or whatever.
}
实际上,我强烈不建议在这种情况下(以及许多其他情况下)使用 class 函数,它不可扩展且一段时间后难以维护。
首先从 viewdidLoad 中调用函数。其次使用块或委托将控制权交还给 ViewController。我更喜欢块方法。您可以有完成和失败块。在完成块中,您可以重新加载视图,如果失败,您可以使用 alertcontroller 或什么都不做。
您可以将 AFNetworking 视为块的示例。
由于 Alamofire
异步工作,您需要一个完成块来 return 收到数据后
class func allQuotes(completion: ([IndexQuotes]) -> Void)
{
var quotes = [IndexQuotes]()
Alamofire.request(.GET, api_indexquotes).responseJSON { response in
if response.result.isSuccess && response.result.value != nil {
for photoDict in (response.result.value as! [NSDictionary]) {
let photo = IndexQuotes(dictionary: photoDict)
quotes.append(photo)
}
}
completion(quotes)
}
}
或者有点"Swiftier"
... {
let allPhotos = response.result.value as! [NSDictionary]
quotes = allPhotos.map {IndexQuotes(dictionary: [=11=])}
}
我还建议使用原生 Swift 集合类型,而不是 NSArray
和 NSDictionary
在视图控制器中的 viewDidLoad
中调用 allQuotes
并在主线程的完成块中重新加载 table 视图。
以小写字母开头的indexQuotes
属性假定为table视图
的数据源数组
var indexQuotes = [IndexQuotes]()
override func viewDidLoad() {
super.viewDidLoad()
IndexQuotes.allQuotes { (quotes) in
self.indexQuotes = quotes
dispatch_async(dispatch_get_main_queue()) {
self.tableView.reloadData()
}
}
}
我正在使用 MVC 模型构建应用程序。 我使用延迟加载技术来填充一个变量。 (模型) 而这个变量是由一个 UIViewController (Controller)
但我不知道如何在模型操作完成后重新加载或触发视图控制器。这是我的代码
模型(延迟加载数据)
class func allQuotes() -> [IndexQuotes]
{
var quotes = [IndexQuotes]()
Alamofire.request(.GET, api_indexquotes).responseJSON { response in
if response.result.isSuccess && response.result.value != nil {
for i in (response.result.value as! [AnyObject]) {
let photo = IndexQuotes(dictionary: i as! NSDictionary)
quotes.append(photo)
}
}
}
return quotes
}
以及视图控制器的部分
class 索引:
UIViewController,UICollectionViewDelegate,UICollectionViewDataSource {
var quotes = IndexQuotes.allQuotes()
var collectionView:UICollectionView!
override func viewDidLoad() {
这是一个非常严肃的问题,我很困惑将使用什么技术来完全满足我的目的?
这是异步操作,这里只需要使用回调:
class func allQuotes(callback: () -> Void) -> [IndexQuotes]
{
var quotes = [IndexQuotes]()
Alamofire.request(.GET, api_indexquotes).responseJSON { response in
if response.result.isSuccess && response.result.value != nil {
for i in (response.result.value as! [AnyObject]) {
let photo = IndexQuotes(dictionary: i as! NSDictionary)
quotes.append(photo)
}
}
callback()
}
return quotes
}
在您的 UIViewController 中:
var quotes = IndexQuotes.allQuotes() {
self.update()
}
var collectionView:UICollectionView!
override func viewDidLoad() {
update()
}
private func update() {
// Update collection view or whatever.
}
实际上,我强烈不建议在这种情况下(以及许多其他情况下)使用 class 函数,它不可扩展且一段时间后难以维护。
首先从 viewdidLoad 中调用函数。其次使用块或委托将控制权交还给 ViewController。我更喜欢块方法。您可以有完成和失败块。在完成块中,您可以重新加载视图,如果失败,您可以使用 alertcontroller 或什么都不做。
您可以将 AFNetworking 视为块的示例。
由于 Alamofire
异步工作,您需要一个完成块来 return 收到数据后
class func allQuotes(completion: ([IndexQuotes]) -> Void)
{
var quotes = [IndexQuotes]()
Alamofire.request(.GET, api_indexquotes).responseJSON { response in
if response.result.isSuccess && response.result.value != nil {
for photoDict in (response.result.value as! [NSDictionary]) {
let photo = IndexQuotes(dictionary: photoDict)
quotes.append(photo)
}
}
completion(quotes)
}
}
或者有点"Swiftier"
... {
let allPhotos = response.result.value as! [NSDictionary]
quotes = allPhotos.map {IndexQuotes(dictionary: [=11=])}
}
我还建议使用原生 Swift 集合类型,而不是 NSArray
和 NSDictionary
在视图控制器中的 viewDidLoad
中调用 allQuotes
并在主线程的完成块中重新加载 table 视图。
以小写字母开头的indexQuotes
属性假定为table视图
var indexQuotes = [IndexQuotes]()
override func viewDidLoad() {
super.viewDidLoad()
IndexQuotes.allQuotes { (quotes) in
self.indexQuotes = quotes
dispatch_async(dispatch_get_main_queue()) {
self.tableView.reloadData()
}
}
}