I am hacking my way through Swift. I love it but I find myself thinking a few things may be too simple and I question if it's correct.
I am converting a project from Objective-C. In the project I have a string property that is use in a method. In Objective-C I did the following to initialize and allocate the object. Once it's initialized and allocated I set it to an empty string.
NSMutableString *tempString = [[NSMutableString alloc] init];
self.currentParsedCharacterData = tempString;
[currentParsedCharacterData setString: @""];
In Swift I typed the following. Is it really this easy or am I missing something?
self.currentParsedCharacterData = ""
I find myself wanting to do the following but I'm not sure it's necessary.
var tempString : String = ""
self.currentParsedCharacterData = tempString
Take care,
Jon
Yes, its this easy. In Objective-C, you could have typed self.currentParsedCharacterData = @"".mutableCopy
and achieved the same effect.
@""
in Objective-C and ""
in Swift are object literals that allocate memory and initialise for you. Equally for arrays, you can do @[]
for an empty NSArray
or []
for an empty Array
(in Swift) to allocate and initialise an empty array.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With