Properties and memory allocation

I crack my way through Swift. I like it, but I think that some things may be too simple, and I doubt it is right.

I am converting a project from Objective-C. In the project, I have a string property that is used in the method. In Objective-C, I did the following to initialize and highlight an object. After initializing and highlighting, 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 that simple or am I missing something?

 self.currentParsedCharacterData = "" 

I think I want to do the following, but I'm not sure if this is necessary.

 var tempString : String = "" self.currentParsedCharacterData = tempString 

Take care

John

+5
source share
1 answer

Yes, that’s easy. In Objective-C, you could type self.currentParsedCharacterData = @"".mutableCopy and get the same effect.

@"" in Objective-C and "" in Swift are object literals that allocate memory and initialize you. Similarly for arrays, you can do @[] for an empty NSArray or [] for an empty Array (in Swift) to select and initialize an empty array.

+6
source

All Articles