我有一个常量字符串定义为
#define kNotificationMessage @"It's time to take your %@"
在 Objective-C 中,我使用:
[NSString stringWithFormat:kNotificationMessage, medicineString]
如何在Swift中将消息传递给UIAlertView?
我有一个常量字符串定义为
#define kNotificationMessage @"It's time to take your %@"
[NSString stringWithFormat:kNotificationMessage, medicineString]
如何在Swift中将消息传递给UIAlertView?
let medicineString = "analgesic"
let kNotificationMessage = "It's time to take your %@"
let sentence = String(format: kNotificationMessage, medicineString)
println(sentence) // It's time to take your analgesic"
let kNotificationMessage:String = "It's time to take your %@"
var modifiedString = NSString(format:kNotificationMessage:String, medicineString) as String
let msg = "It's time to take your" as String
let alertController = UIAlertController(title: "iOScreator", message:
"\(msg)" , preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.Default,handler: nil))
self.presentViewController(alertController, animated: true, completion: nil)
不行,你必须将其制作成一个函数:
func notificationMessage(medicineString: String) -> String {
return "It's time to take your " + medicineString
}
如需更多信息,这个帖子 是一个不错的阅读材料。
你最好使用 struct
来处理这样的常量:
struct GlobalConstants {
static let kNotificationMessage = "It's time to take your"
}
println(GlobalConstants.kNotificationMessage)
首先创建一个结构体
struct AStructConstants
{
static let sampleString : NSString = NSString(string: "Check out what I learned about %@ from Stackoverflow.")
}
var aString : NSString = NSString(format: AStructConstants.sampleString, "some custom text")
println(aString)
你的输出将是:
看看我从Stackoverflow学到的一些自定义文本。
let kNotificationMessage = "Check out what I learned about %@ from Stackoverflow."; let aString = String(format: kNotificationMessage, medicineString)
。 - Airspeed Velocity