ios - 为 NSCalendarIdentifier 返回 nil 使用变量属性值

标签 ios nsdate nscalendar

我正在尝试使用如下可变值初始化 NSCalendar:

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:jam.calenderType];

但是calendar总是返回nil

我知道:

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierIslamic];

并且:

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

两者都工作正常,但我希望该值为jam.calenderType,它是一个NSString,其值为@"NSCalendarIdentifierIslamic"@"NSCalendarIdentifierGregorian"

最佳答案

您的问题来自您在变量中存储日历标识符的方式。当您编写 @"NSCalendarIdentifierIslamic" 时,它只是表示包含这些字符的字符串,而不是 NSCalendarIdentifierIslamic 标识符指向的字符串。

因此只需设置您的变量 jam.calenderType = NSCalendarIdentifierIslamic;jam.calenderType = NSCalendarIdentifierGregorian; 即可正常工作。

另一个简单的解决方案是将您的 calenderType 设置为 BOOLint,然后根据它的值选择 日历初始化中的 NSCalendarIdentifierIslamicNSCalendarIdentifierGregorian

关于ios - 为 NSCalendarIdentifier 返回 nil 使用变量属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30936955/

相关文章:

ios - exc_bad_access 访问类别中的静态成员

swift - 如何在日历中使用 NSCalendar 范围函数?

ios - NSDateFormatter 无法将 '1988-04-10' 解析为 iOS 6 中的 NSDate 实例。这是一个错误吗?

ios - 创建一个每 24 小时运行一次的事件?

ios - 新 XCODE 6.3 更新 : Apr 8, 2015 构建 : 6D570 buggy with Unit Tests?

ios - 离开 View 时停止异步 block 请求(AFNetworking;iOS)

objective-c - 转义 NSDateFormatter 字符串

ios - 如果缺少字段,则解析注册到下一个 View Controller

ios - 如何使用 DeviceCheck.DCDevice Xamarin Forms C# 生成 token 代码 iOS DeviceId

ios - UIDatePicker 最小日期(不含星期日)