当前位置: 首页 > 开发杂谈 >

D26 - 用 Swift 和公开资讯,打造投资理财的 Apps { 三大法人成交比重实作.1 }

为了完成三大法人的比重,我们需要两个数值

  • 三大法人成交金额
  • 台股日成交金额 - 这一项在前面已经完成了,直接使用 TwMarketTradingInfoManager 即可。
import Foundation

protocol MajorInvestorsModelDelegate: AnyObject {
    func didRecieve(investors: [MajorInvestor], error: Error?)
    func didRecieve(dailyTradingInfo: [TwMarketTradingInfo], error: Error?)
}

class MajorInvestorsModel {
    
    weak var delegate: MajorInvestorsModelDelegate?
    
    var majorInvestors = [MajorInvestor]()
    
    var dailyTradingInfo = [TwMarketTradingInfo]()
    
    private lazy var threeMajorManager: ThreeMajorInvestorsManager = {
        let manager = ThreeMajorInvestorsManager()
        return manager
    }()
    
    private lazy var marketManager: TwMarketTradingInfoManager = {
        let manager = TwMarketTradingInfoManager()
        return manager
    }()
    
    func requestMajorInvestorsAndTwMarket() {
        threeMajorManager.requestInvestorsInfo { [weak self] investors, error in
            self?.majorInvestors = investors
            self?.delegate?.didRecieve(investors: investors, error: error)
        }
        
        marketManager.requestTwMarketDailyTradingInfo(date: Date()) { [weak self] dailyTradingInfo, error in
            self?.dailyTradingInfo = dailyTradingInfo
            self?.delegate?.didRecieve(dailyTradingInfo: dailyTradingInfo, error: error)
        }
    }
}

接下来,在 VC 上拉出两个 UIButton,一个发动拉取资料,另一个将下一个 vc push 进来,并把拉取的资料 array 传进去。

这个 VC 还有个 Label 显示当下的状态,可以让你知道拉下来的资料状态。

https://ithelp.ithome.com.tw/upload/images/20211005/20140622bRCLrR2diP.png

MajorInvestorsViewController VC 的程序码如下

import UIKit

class MajorInvestorsViewController: UIViewController {
    
    @IBOutlet weak var stateLabel: UILabel!
    
    private lazy var model: MajorInvestorsModel = {
        let model = MajorInvestorsModel()
        model.delegate = self
        return model
    }()

    // MARK: - life cycle
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    // MARK: - IBAction
    @IBAction func requestMajorInvestorsButtonDidTap(_ sender: Any) {
        model.requestMajorInvestorsAndTwMarket()
    }
    
    @IBAction func pushToThreeMajorInvestorButtonDidTap(_ sender: Any) {
        guard let storyboard = self.storyboard,
              let vc = storyboard.instantiateViewController(withIdentifier: "MajorInvestorsDashboardViewController") as? MajorInvestorsDashboardViewController else {
            return
        }
        
        vc.dailyTradingInfo = model.dailyTradingInfo
        vc.majorInvestors = model.majorInvestors
        navigationController?.pushViewController(vc, animated: true)
    }
}

extension MajorInvestorsViewController: MajorInvestorsModelDelegate {
    
    func didRecieve(investors: [MajorInvestor], error: Error?) {
        
        if let error = error {
            print("you got error during fetch investor info: \(error.localizedDescription)")
            return
        }
        
        print("major investors: \(investors)")
        stateLabel.text = "\(investors)"
    }
    
    func didRecieve(dailyTradingInfo: [TwMarketTradingInfo], error: Error?) {
        
        if let error = error {
            print("you got error during fetch daily trading: \(error.localizedDescription)")
            return
        }
        
        print("daily trading info: \(dailyTradingInfo)")
    }
}

传下去的 VC - MajorInvestorsDashboardViewController 目前程序码很单纯,只有两个 array。後续的计算会在下一篇开始写。

import UIKit

class MajorInvestorsDashboardViewController: UIViewController {

    var majorInvestors = [MajorInvestor]()
    
    var dailyTradingInfo = [TwMarketTradingInfo]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
}

台股申购日历
IT铁人赛Demo App

下方是这次 D1 ~ D12 的完成品,可以下载来试
App Store - 台股申购日历

https://ithelp.ithome.com.tw/upload/images/20210924/20140622ypOBM0tgrZ.png


相关文章:

  • [DAY 29] _STM32_内部Flash
  • Proxmox VE 群组管理与双因素认证
  • 【领域展开 14 式】 Favicon 的好助手!Canva 使用与 5 下搞定网站设定
  • Day 25:专案06 - 股市趋势图02 | 整年股市资料、Postman
  • 无法验证账户信息常见情况解决办法
  • 【Laravel 】虚拟主机配置
  • 怎么保持新店铺的活跃度?
  • DAY 16:Abstract Factory Pattern,膜拜那个工厂之神吧!
  • 舍弃伪双向绑定 v-model
  • Day08 - 【入门篇】OAuth 2.0 Playground
  • wish平台如何手动添加/修改图片?
  • DAY18-JAVA的抽象类别(1)
  • day[11] Hello Line - 第一个Line讯息
  • AI ninja project [day 24] 决策树森林 --排名资料
  • 铁人赛 Day7 -- PHP SQL基本语法(二) -- Session 你到底可以干麻
  • Goolge play怎么更改地区下载Apps : 谷歌应用商店更改地区教程
  • 礼品卡和虚拟信用卡的区别:Prepaid Debit Card和Virtual Credit Card区别
  • DGCHOST稳定吗?现在还可以购买DGCHOST的服务器VPS吗
  • 一周要闻:谷歌母公司、Facebook、亚马逊等几大互联网公司一季度财报
  • 国外代发货教程:教你如何一件代发做跨境电商国外市场
  • Yandex Money虚拟卡申请:俄罗斯银行卡预付卡申请教程
  • 让 Rank Math SEO 输出关键词 keywords meta 字段信息
  • Google Play Store报错DF-DFERH-01怎么办
  • Windows服务器推荐:便宜好用稳定靠谱的国外Windows服务器推荐
  • 韩国Moack蘑菇CN2服务器评测和特价服务器,优惠码
  • 最好用的云服务器推荐:什么云服务器稳定好用又便宜
  • 国外虚拟主机大全:便宜好用靠谱的国外网站空间推荐大全
  • 外贸电商网络营销之心理营销如何做?让客户更信任你的产品和网站
  • 挖矿是什么?怎么挖矿怎么挖比特币?虚拟币比特币挖矿原理
  • WordPress 5.7.1 修复2个安全问题,请及时更新