如何编写可编码的变压器?

时间:2018-12-14 12:49:34

标签: ios swift codable

让我们考虑以下情况,我想在Codable中填充模型。

struct SampleModel: Codable {
    let showId: String
}

我们从服务器收到的响应是“ showId:“一个”。但是,我想将其另存为“第一”而不是“一个”。

Codable有什么关系吗?

1 个答案:

答案 0 :(得分:1)

如果我正确理解了你

const fs = require('fs')
const promisify = require('util').promisify
const rp = require('request-promise')
const readFile = promisify(fs.readFile)
const filePath = 'json/results.json'

let promises = []

function init() {
  readFile(filePath, 'utf8')
  .then(file => {
    const json = JSON.parse(file)

    for (const country of json) {
      if (country.eventIDs.length === 0) return

      country.eventIDs.forEach(id => promises.push(getEventData(country, id)))
    }

// nothing here is executed,  
    console.log('this is not fired')

    Promise.all(promises)
           .then(results => writeFile(results))
           .catch( err => console.log(err))
  })
  .catch(err => console.log(err))
}

getEventData = (country, id) => new Promise((resolve, reject) => {

  setTimeout(() => {
    rp(url)
    .then((results) => {      
      resolve ({
        ...results
      })
    })
    .catch((err) => reject(`getEventData Error:\n ${err}`))
  }, 2000)
})

writeFile = (results) => {
  const json = JSON.stringify(results)
  // const date = new Date()
  // const filename = `${date.getTime()}-all-event-ids.json`
  const filename = `results-allevents.json`
  fs.writeFile(`json/${filename}`, json, 'utf8', () => console.log(`Succesfully written: ${filename}`))
}

init()