What is the advantage of this:
NSArray *array = [[NSArray alloc] initWithObjects:@"Year", "@Capital", ..., nil]; self.hintArray = array; [array release];
Instead of directly assigning my class variable as follows:
self.hintArray = [[NSArray alloc] initWithObjects:@"Year", "@Capital", ..., nil];
Why do we create a temporary local object and then free it, and not just assign it to our class variable?
You could, but you must remember to release it once before moving on. Assignment self.hintArray(assuming that it is a synthesized setter that is stored on the set) will remove keepCount:
self.hintArray
NSArray *array = [[NSArray alloc] initWithObjects:...]; // retainCount is 1 self.hintArray = array; // retainCount is 2 [array release]; // retainCount is 1
and
self.hintArray = [[NSArray alloc] initWithObjects:...]; // retainCount is 2: // one for the alloc // and one for the assign [self.hintArray release]; // retainCount is 1
Others have already noted memory issues, but here is the best way to do this in one step:
self.hintArray = [NSArray arrayWithObjects:@"Year", "@Capital", ..., nil];
+arrayWithObjects , , . . (, , hintArray retain copy).
+arrayWithObjects
hintArray
retain
copy
Since the reference to creating an array in Objective-C's counted memory management scheme will increase the reference count, and if you do not store the return value in a variable, you can send a release message, then there will be no way to reduce that counting and lead to a memory leak.
Source: https://habr.com/ru/post/1716709/More articles:How does Django determine if the uploaded image is valid? - pythonPHP ftp_put () - "Can not STOR". - phpSetting property value of parent class viewcontroller from child viewcontroller? - scopeNHibernate ITransaction and the clean domain model - nhibernateAllow embed / object / param HTML tags using HTMLPurifier? - htmlLoading jQuery script on click event - javascriptФоновое чтение для анализа неаккуратных/причудливых/ "почти структурированных" данных? - parsingC # plugin architecture with strong names: misunderstanding - c #Управление путём Python при перемещении кода с компьютера разработки - pythonВозобновление потоков С# - multithreadingAll Articles