迅速的不断宣言

时间:2015-04-28 06:07:02

标签: ios objective-c xcode swift

我有一个定义为

的常量字符串
#define kNotificationMessage @"It's time to take your %@"

在目标C中我使用

[NSString stringWithFormat:kNotificationMessage, medicineString]

作为UIAlertView的消息。我们如何在swift中实现这一目标

6 个答案:

答案 0 :(得分:7)

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"

答案 1 :(得分:2)

请使用以下代码可能会解决您的问题。

let kNotificationMessage:String = "It's time to take your %@"
var modifiedString = NSString(format:kNotificationMessage:String, medicineString) as String

答案 2 :(得分:1)

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)

这就是我在Swift中提醒的方式。如果我没有错误地理解这段代码对你有用,我会很高兴的。 :)

答案 3 :(得分:0)

你不能。你将不得不用它来创造一个功能:

func notificationMessage(medicineString: String) -> String {
    return "It's time to take your " + medicineString
}

有关详细信息,this thread非常好读。

答案 4 :(得分:0)

您最好使用struct来处理此类常量:

struct GlobalConstants {
    static let kNotificationMessage = "It's time to take your"
}

println(GlobalConstants.kNotificationMessage)

答案 5 :(得分:-2)

首先创建一个struct

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中学到的一些自定义文本。