如何获取文本以显示科普特日历日期 SwiftUI

How do I get text to display Coptic Calendar date SwiftUI

我正在尝试使用 NSCalendar 初始值设定项,但我无法理解文档。我目前拥有的是

    struct SwiftUIView: View {
    var body: some View {
        
        
        let Date = NSCalendar.init(calendarIdentifier: .coptic)
        
        Text("\(Date)")
        
        
        
    }
}

struct SwiftUIView_Previews: PreviewProvider {
    static var previews: some View {
        SwiftUIView()
    }
}

它给我一个错误提示 “可选类型 'NSCalendar?' 的值必须解包为类型 'NSCalendar' 的值”

如果有人能帮我解决这个问题,那就太好了。谢谢

您想要的 Swift 界面是 Calendar 而不是 NSCalendar:

let calendar = Calendar(calendarIdentifier: .coptic)

NSCalendar 接口来自 ObjC,如果日历名称未知(您可以在 ObjC 中为此参数传递任意字符串),则记录为 return nil。

Swift 日历界面不能失败,因为它不允许您创建未知标识符。

有许多以“NS”开头的类型是从 Objective-C 桥接而来的。删除“NS”前缀的 Swift 版本是很常见的。看到这里,一般应该使用Swift版本。它通常会以更 Swift 的方式表现。

但是,如果您的目标是在科普特日历上显示当前日期,您需要的不止于此。日历代表整个日历,而不是特定日期。您需要 DateFormatter 才能创建本地化字符串。

例如:

let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .medium
dateFormatter.calendar = Calendar(identifier: .coptic)
dateFormatter.eraSymbols = ["BC", "AM"] // See below
print(dateFormatter.string(from: Date()))

// Baramouda 18, 1737 AM

(我相信日历中有一个错误,科普特日历有纪元符号“ERA0”和“ERA1”。我相信这个日历的正确符号是“BC”和“AM”。你可以强制这是通过将它们直接分配给日期格式化程序来实现的。如果我认为这是一个错误并且它影响了你,我建议你打开一个 Apple Feedback. See also the DateFormatter 文档来了解如何自定义这个字符串。)

就 xTwisteDx 的观点而言,要将其放入 SwiftUI 中,您需要遵循以下原则:

struct ContentView: View {

    let dateFormatter: DateFormatter = {
        let dateFormatter = DateFormatter()
        dateFormatter.dateStyle = .medium
        dateFormatter.eraSymbols = ["BC", "AM"] // Workaround for Foundation bug
        dateFormatter.calendar = Calendar(identifier: .coptic)
        return dateFormatter
    }()

    func today() -> String {
        dateFormatter.string(from: Date())
    }

    var body: some View {
        Text(today())
            .padding()
    }
}

好的,这是 Optionals 的问题。每当您看到 ?! 时,您就可以假设它是可选的。本质上,它是 Type? 的原因,请注意 ? 是因为它可能返回了 nil 值。例如,以下片段可能出现在 Swift

let forceUnwrappedString: String!
let optionalString: String?
let safeString: String = "Hello World!"

print(forceUnwrappedString) //This WILL crash, the variable is nil.
print(optionalString) //This MAY crash, if the value is nil.
print(safeString) //This WON'T crash.

您处理这些问题的方式是使用 if letguard 语句进行检查。或者,您也可以在检查 nil.

后强制解包

如果让

在此示例中,如果 optionalString 为 nil,则不会执行打印语句。但是,如果 optionalString 包含一个值,它会。它本质上是一种告诉编译器检查 nil 值并且只检查 运行 安全代码的方法。

if let safeString = optionalString {
    print(safeString)
}

后卫

在这个例子中,它与 If Let 的工作方式相同,但显着的区别在于 safeString 可以在比 If Let 更大的范围内访问,而且它 returns 如果值未“展开”,因此如果未安全展开,则不会调用它下面的任何代码。

guard let safeString = optionalString { else return }
print(safeString)

强制展开

强制展开是您应该避免的事情,但在某些情况下它是可以接受的。这是强制展开的示例。请注意,我使用了 !,它基本上表示“我知道这个值不为零,请使用它。”那是你唯一应该使用它的时候,你可以保证没有 nil 值。

let someString: String? 

someString = "Hello World!"

print(someString!)

你的问题

在您的问题的上下文中,NSCalendar.init(calendarIdentifier: .coptic) returns 可选类型,或者 someType? 意味着它有可能是 nil 解决方案如下。

let Date = NSCalendar.init(calendarIdentifier: .coptic)
        
if let safeDate = Date {
        Text("\(safeDate)")
} else {
    //The date value was nil, do something else
}