Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift string format specifier equivalent to %@

I am writing a simple app with a form in Swift, I had the app written in Objective-C to populate an e-Mail with data from text fields in a view, I used the following code to do this:

NSString *messageBody = [NSString stringWithFormat:@"Name: %@ \nDate: %@", NameField.text, DateField.text];

I am trying to achieve the same thing in Swift, I have the following so far:

let messageBody = NSString(format: "Name: %@ \nDate: %@", NameField, DateField)

I am looking for swifts equivalent to "%@" to make the app look to the format of the string to find the data to place after "Name:".

like image 719
DanielPetters386 Avatar asked Dec 05 '22 01:12

DanielPetters386


1 Answers

In Swift you use \(some_printable_object_or_string)

like

let string = "Name: \(NameField) \nDate: \(DateField)"

Or you can use ObjectiveC-style formatting

let string = String(format: "Name: %@ \nDate: %@", NameField, DateField)
like image 52
Anton Belousov Avatar answered Dec 25 '22 18:12

Anton Belousov