gpt4 book ai didi

ios - 如何过滤 JSON 数据然后枚举过滤后的数据?

转载 作者:行者123 更新时间:2023-11-28 08:32:28 25 4
gpt4 key购买 nike

我目前正在创建一个应用程序,它使用仅持续特定时间的“定时奖励”,我不熟悉在 Swift 中使用过滤器,我需要满足的当前目标:

  1. 如果 redemptionDate 为 NULL,则显示奖励

  2. 如果 currentDateTime >= redemptiondate + redeemTimeLimit(这些是 JSON 对象)则隐藏奖励

  3. 如果 currentDateTime < redemptiondate + redeemTimeLimit 则显示奖励和倒数计时器

  4. 倒计时计时器的数量是 redemptiondate + redeemTimeLimit 和 currentDateTime 之间的差值

到目前为止,这是我的代码,我遇到过几次崩溃,注释在代码中。我还对我尝试满足上述标准的区域进行了编号:

   var rewardsArray : [MemberRewardsInfo] = []//MemberRewarsInfo is a JSON Object custom class
var timeFilteredRewardsArray : [MemberRewardsInfo] = []

RewardManager.sharedInstance.updateRewards({ rewards in

self.timeFilteredRewardsArray = rewards


//.1
self.timeFilteredRewardsArray = self.timeFilteredRewardsArray.filter ({$0.redemptionDate == nil})


for filteredReward in self.timeFilteredRewardsArray {


let Date = NSDate()
let df = NSDateFormatter()
df.timeZone = NSTimeZone(forSecondsFromGMT: 0)
df.dateFormat = "MM-dd-yyyy"
let currentDate = df.stringFromDate(Date)

//.2
if currentDate >= filteredReward.redemptionDate! + filteredReward.redeemTimeLimit! {

//remove current reward from array, not sure how to do this

//.3
} else if currentDate < filteredReward.redemptionDate! + filteredReward.redeemTimeLimit!{

//.4
var countdownTimer = (filteredReward.redemptionDate! + filteredReward.redeemTimeLimit!) - currentDate

//error occurs here: "Binary operator '-' cannot be applied to two 'String' operands"
}
}



self.rewardsArray = self.timeFilteredRewardsArray
self.rewardsTableView.reloadData()

})

最佳答案

我推荐你使用这个库 SwiftyJSON . Swift 对类型非常严格,而 JSON 不关心类型。链接也变得一团糟,SwiftyJSON 修复了所有这些。它将帮助您专注于过滤器,因为您正在过滤数组然后将其分配给同一个数组...

快速示例,因为我不知道您的 json 结构:

let json = JSON(data: dataFromNetworking)
if let userName = json[0]["data"]["date"].string {
//convert to date and do your filters
}

关于ios - 如何过滤 JSON 数据然后枚举过滤后的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38602644/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com