Permanent declaration in quick

I have a constant defined as

#define kNotificationMessage @"It time to take your %@"


In object C, I am using

[NSString stringWithFormat:kNotificationMessage, medicineString]


as a message for the UIAlertView. How do we achieve this in fast


source to share

6 answers

First create the structure

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")


Your result will be:

Check out what I learned about some custom text from Stackoverflow.



let medicineString = "analgesic"
let kNotificationMessage = "It time to take your %@"

let sentence = String(format: kNotificationMessage, medicineString)

println(sentence) // It time to take your analgesic"




Please use the following code, which may solve your problem.

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




let msg = "It 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)


This is how I am warning in Swift. If I am not mistaken and this code is useful for you, I will be glad. :)



You can not. You will need to make a function out of it:

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


For more information, this thread reads pretty well.



Better to deal with a constant like this using struct


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





All Articles