[iOS] swift : use of %s in String(format: …)

Posted in :

Apple 官方文件:

https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html


使用範例:

"IdelTimeOutMessage" = "Idle time out exceeded, please input your password for account:%1$@";

附註:請使用 %@ , 不要用 %s ,不然會 crash

完整的範例:

        let idleTimeoutMessage = String(format: NSLocalizedString(“IdelTimeOutMessage”, comment: “”), user_id)

        let alert:UIAlertController = UIAlertController(title: NSLocalizedString(“Alert”, comment: “”),

                                                        message: idleTimeoutMessage,

                                                        preferredStyle: UIAlertControllerStyle.alert)

        let defaultAction:UIAlertAction = UIAlertAction(title: “OK”,

                                                        style: UIAlertActionStyle.default,

                                                        handler:{

                                                            (action:UIAlertAction!) -> Void in

                                                            print(“OK”)

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *