I am using DateFormatter with a custom calendar in Text(_:formatter:), but the text does not display the correct date. But if using DateFormatter.string(from:), the correct date will be displayed.
So, what is the difference?
The example:
extension DateFormatter {
static func hijri(_ format: String) -> DateFormatter {
let formatter = DateFormatter()
formatter.dateFormat = format
formatter.calendar = .init(identifier: .islamicUmmAlQura)
return formatter
}
}
struct TextFormatterView: View {
var body: some View {
VStack {
Text(DateFormatter.hijri("dd, MMMM yyyy").string(from: Date()))
// 24, Rabiʻ I 1445 👍
Text(Date(), formatter: DateFormatter.hijri("dd, MMMM yyyy"))
// 09, October 2023 ❌
}
}
}
Post
Replies
Boosts
Views
Activity
protocol UIViewConstraint {
associatedtype Anchor: AnyObject
var anchor: NSLayoutAnchor<Anchor>? { get }
var attribute: NSLayoutConstraint.Attribute { get }
var value: UIView.ConstraintValue { get }
}
extension UIView {
struct Constraint<Anchor: AnyObject>: UIViewConstraint {
var anchor: NSLayoutAnchor<Anchor>?
var attribute: NSLayoutConstraint.Attribute
var value: UIView.ConstraintValue
}
}
extension UIViewConstraint where Anchor == NSLayoutYAxisAnchor {
static func top(_ value: UIView.ConstraintValue, to anchor: NSLayoutYAxisAnchor? = nil) -> UIView.Constraint<NSLayoutYAxisAnchor> {
UIView.Constraint(anchor: anchor, attribute: .top, value: value)
}
static func bottom(_ value: UIView.ConstraintValue, to anchor: NSLayoutYAxisAnchor? = nil) -> UIView.Constraint<NSLayoutYAxisAnchor> {
UIView.Constraint(anchor: anchor, attribute: .bottom, value: value)
}
}
extension UIView {
struct ConstraintValue {
var constant: CGFloat
}
}
extension UIView.ConstraintValue: ExpressibleByIntegerLiteral {
init(integerLiteral value: Int) {
self.init(constant: CGFloat(value))
}
}
extension UIView {
func active<each Anchor>(_ constraint: repeat UIView.Constraint<each Anchor>) {
(repeat _active(each constraint))
}
func active2<each Constraint: UIViewConstraint>(_ constraint: repeat each Constraint) {
(repeat _active(each constraint))
}
func active3(_ constraint: any UIViewConstraint...) {
}
private func _active<T: UIViewConstraint>(_ anchor: T) {
//
}
}
let superView = UIView()
let view = UIView()
superView.addSubview(view)
view.active(
.top(10), // Cannot convert value of type 'Int' to expected argument type 'UIView.ConstraintValue'
.bottom(20) // Cannot convert value of type 'Int' to expected argument type 'UIView.ConstraintValue'
)
view.active2(
.top(10), // Cannot convert value of type 'Int' to expected argument type 'UIView.ConstraintValue'
.bottom(20) // Cannot convert value of type 'Int' to expected argument type 'UIView.ConstraintValue'
)
let top: UIView.Constraint<NSLayoutYAxisAnchor> = .top(10) // ✅
let bottom: UIView.Constraint<NSLayoutYAxisAnchor> = .bottom(20) // ✅
view.active(top, bottom) // ✅
view.active2(top, bottom) // ✅