You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

DateComponentsFormatter未遵循formattingContext设置问题咨询

DateComponentsFormatter忽略formattingContext设置的问题分析

现象对比

使用NumberFormatter时,设置formattingContext = .beginningOfSentence可以正常让拼写出来的数字首字母大写:

let formatter = NumberFormatter()
formatter.numberStyle = .spellOut
formatter.formattingContext = .beginningOfSentence
let string = formatter.string(for: 5)                 // "Five"; 符合预期

但同样的设置用在DateComponentsFormatter上时,formattingContext被完全忽略,输出的字符串始终是小写开头:

let formatter = DateComponentsFormatter()
formatter.unitsStyle = .spellOut
formatter.formattingContext = .beginningOfSentence
let string = formatter.string(from: 5 * 60)           // "five minutes"; 不符合预期,应为"Five minutes"

已验证的无效调整

以下尝试均无法让DateComponentsFormatter遵循formattingContext设置:

  • 切换不同地区(英文、法文等),本地化生效但首字母仍未大写;
  • 在macOS及多个iOS版本测试,问题一致存在;
  • 调整unitsStyle和formattingContext的设置顺序;
  • 显式指定allowedUnits和maximumUnitCount,排除单位数量变量的干扰;
  • 尝试其他formattingContext选项,输出均为小写字符串。

结论与临时方案

目前可以确认DateComponentsFormatter并未实现对formattingContext的支持,你的手动句首大写方案是当前可行的替代方法。在提交Bug报告前无需额外尝试其他设置,这确实是该API的功能缺失问题。

内容的提问来源于stack exchange,提问作者Rob

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 11:24:23