this trying in playground file. result want second println
result using stringbyappendingstring
. isn't stringbyappendingstring
supposed return modified string? doing wrong here?
var reportsummary:string = "total: \t\t300.00" reportsummary.stringbyappendingstring("net: \t\t200.00") println(reportsummary) // total: 300.00 var reportsum = "total: \t300.00\n" var netsum = "net: \t\t200.00\n" var sum = reportsum + netsum println(sum) // total: 300.00 // net: 200.00
the stringbyappendingstring
message creates , returns new string
. doesn't modify receiver (the original string
).
if want use combined string
, need return value. code posted ignores return value. can, example, store returned string
in reportsummary
variable this:
var reportsummary:string = "total: \t\t300.00" reportsummary = reportsummary.stringbyappendingstring("\nnnet: \t\t200.00") println(reportsummary)
you might use +=
operator, though:
var reportsummary = "total: \t\t300.00" reportsummary += "\nnet: \t\t200.00" println(reportsummary)
or can use extend
method:
var reportsummary = "total: \t\t300.00" reportsummary.extend("\nnet: \t\t200.00") println(reportsummary)
or can explicitly use foundation type nsmutablestring
:
let reportsummary: nsmutablestring = "total: \t\t300.00" reportsummary.appendstring("\nnet: \t\t200.00") println(reportsummary)
Comments
Post a Comment