Reputation: 133
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:"
.
Upvotes: 6
Views: 22754
Reputation: 1141
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)
Upvotes: 14
Reputation: 2587
Don't just port Obj-c to Swift. Swift has it's own way of doing things.
let message = "Name: \(nameField) \nDate:\(dateField)"
Upvotes: 3
Reputation: 1390
let firstName = "Hai"
let date = NSDate()
let messageBody = NSString(format: "Name: \(firstName) \n Date: \(date)")
NSLog(messageBody as String)
Upvotes: 0