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

+3


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")
println(aString)

      



Your result will be:

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

-2


source


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

let sentence = String(format: kNotificationMessage, medicineString)

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

      



+6


source


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

      

+2


source


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. :)

+1


source


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.

0


source


Better to deal with a constant like this using struct

:

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

println(GlobalConstants.kNotificationMessage)

      

0


source







All Articles