swift - 在 Swift 中使用 %@ 格式化字符串

标签 swift formatting

我有以下带有私有(private)结构的字符串类,我想稍后将它们用于格式化字符串。但是,代码在运行时崩溃。

这是为什么?是不是因为它被定义为static let?

以下是剥离的代码:

class LGNotificationHandler {
    private struct Strings {
        static let SentImagesENG = "Sent %@ images to the event"
        static let SentImagesTUR = "Etkinliğe %@ görsel gönderdi"
    }

    func buildNotificationString(imageCount: Int) -> String {
        if imageCount == 1 {
        .
        .
        .

        } else {
            // below line is giving error at run time
            notificationENG = String(format: Strings.SentImagesENG, imageCount)  
            notificationTUR = String(format: Strings.SentImagesTUR, imageCount)  
        }
    }
}

最佳答案

您忽略了提供有关崩溃的任何详细信息,但一个明显的问题是使用 %@ 格式说明符和 Int。您需要将 %dInt 一起使用。

关于swift - 在 Swift 中使用 %@ 格式化字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40626006/

相关文章:

google-sheets - 计算持续时间并以分钟和秒表示

formatting - Erlang:如何将小数转换为用零填充的十六进制字符串

xcode - 从框架加载 xib 文件

ios - ImageView 不遵守约束

swift - Realm - 如何获取父子关系中实体所有者的列表?

docker - 格式化kubectl输出

c# - 修改 C# 中的 "G"字符串格式化程序以在数字包含至少两个零 d.p 时使用指数

javascript - VS2012 : Indentation of switch case in JavaScript when used with JSHint

ios - 是 scrollRangeToVisible(_ :) considered slow if a large attributed text is set in UITextView?

ios - 重新加载表格 View 而不影响单元格中的倒数计时器