Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Properties and Allocating Memory

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

like image 595
jonthornham Avatar asked May 08 '15 05:05

jonthornham


1 Answers

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.

like image 93
Schemetrical Avatar answered Sep 20 '22 15:09

Schemetrical