original articles, welcome reprint. Reprint Please specify: Dongsheng's blog
Memory managed Objects
Swift called in corefoundation when a function obtains an object, the object is divided into: a memory managed object and a memory unmanaged object.
A memory managed object is a compiler that helps manage memory, and we don't need to call Cfretain function takes ownership of the object and does not need to call Cfrelease function discards object ownership.
The way to get these memory managed objects is to use the cf_returns_retained or cf_returns_not_retained Comment Declaration, sample code:
-(cgpathref) maketopathcf_returns_retained{uibezierpath* triangle = [Uibezierpathbezierpath]; [Triangle Movetopoint:cgpointzero]; [Triangleaddlinetopoint:cgpointmake (self.view.frame.size.width,0)]; [Triangle Addlinetopoint:cgpointmake (0,self.view.frame.size.height)]; [Triangle Closepath]; Cgpathref Thecgpath = [triangle Cgpath]; Return cgpathcreatecopy (Thecgpath);}
Memory managed objects are simple to use and do not require us to do extra things.
Funccfstringcreatewithcstring (_ alloc:cfallocator!, _ Cstr:unsafepointer<int8>, _encoding:cfstrin Gencoding, cfstring! Memory Managed Object Func cfhostcreatecopy (_alloc:cfallocator, _host:cfhost), unmanaged<cfhost>//Memory Unmanaged Object
Memory unmanaged objects
memory unmanaged objects are memory that needs to be managed by the programmer himself. This is because the cf_returns_retained or cf_returns_not_retained Comment Declaration is not used in the method that obtains the object. The compiler cannot help manage memory. We can use the previous section to determine whether a non-memory managed object is used in a specific way .
Memory Unmanaged objects are a bit cumbersome to use and should be handled according to the method of acquiring ownership.
1.If a function name containsCreateorCopy, the caller obtains the object 's ownership at the same time, and the return value unmanaged<t>need to callTakeretainedvalue ()method to obtain the object. When the caller no longer uses the object,Swiftthe code needs to callCfreleasefunction discards the ownership of the object becauseSwiftis aARCmemory-managed.
2. If a function name contains a get, the caller obtains the object without taking ownership of the object, and the return value unmanaged<t> you need to call the takeunretainedvalue () method to get the object.
The sample code is as follows:
Let Host:cfhost =cfhostcreatewithname (Kcfallocatordefault, "127.0.0.1"). Takeretainedvalue () Let hostnames: Cfarray =cfhostgetnames (host, nil)!. Takeunretainedvalue ()
Welcome to follow Dongsheng Sina Weibo@tony_Dongsheng.
Learn about the latest technical articles, books, tutorials and information on the public platform of the smart Jie classroom
650) this.width=650; "title=" 00.png "src=" http://s3.51cto.com/wyfs02/M01/7C/FD/wKioL1beOc_CMTlhAAAs2MBEZnc139.png "alt=" Wkiol1beoc_cmtlhaaas2mbeznc139.png "/>
More ProductsIOS,Cocos, mobile Design course please pay attention to the official website of Chi Jie Classroom:http://www.zhijieketang.com
Smart-Jie Classroom Forum Website:http://51work6.com/forum.php
This article is from the "Dongsheng-ios Technical Consultant" blog, make sure to keep this source http://tonyguan.blog.51cto.com/701759/1748668
Learn Swift from scratch Learning Notes (day)--core Foundation framework memory managed Objects vs. unmanaged objects