Ios 设置超过60的Swift日期分钟返回零

Ios 设置超过60的Swift日期分钟返回零,ios,swift,swiftui,Ios,Swift,Swiftui,IOS非常新,我需要设置一个日期小时和分钟,我喜欢这样: let date=Calendar.current.date(通过设置小时:祈祷时间小时,分钟:祈祷时间分钟+Int(调整)!,秒:0,of:date())! 祈祷分钟+整数(调整)在某些情况下超过60,因此应用程序会崩溃,因为日期是nil让日历为您计算所有数据。没有“09:61”这样的时间,日历告诉你这一点 // It's not clear that you want the current calendar here. It ma

IOS非常新,我需要设置一个日期小时和分钟,我喜欢这样:

let date=Calendar.current.date(通过设置小时:祈祷时间小时,分钟:祈祷时间分钟+Int(调整)!,秒:0,of:date())!

祈祷分钟+整数(调整)在某些情况下超过60,因此应用程序会崩溃,因为日期是
nil

让日历为您计算所有数据。没有“09:61”这样的时间,日历告诉你这一点

// It's not clear that you want the current calendar here. It may not be Gregorian.
let calendar = Calendar(identifier: .gregorian)

// Using ! means you're promising prayerTimeHour and prayerTimeMinutes are valid.
let baseDate = calendar.date(bySettingHour: prayerTimeHour,
                             minute: prayerTimeMinutes,
                             second: 0,
                             of: Date())!

// ! should be safe because the Gregorian calendar will have a date for any number
// of minutes into the future
let adjustedData = calendar.date(byAdding: .minute,
                                 value: adjustement,
                                 to: baseDate)!

斯威夫特不是JavaScript。根据OP询问的是如何处理祈祷时间的事实,而他的名字是穆罕默德,我猜他会想使用伊斯兰历而不是格里高利历。据我所知,在两个日历中,时间计算(如添加分钟)的效果是一样的。@Duncac在IMO的日期中添加分钟也可以,无论日历是什么。主要问题是在设置可能不存在的特定时间(即夏令时转换日期)时强制展开结果(通过设置小时)