Learn Swift from scratch Learning Notes (day)--core Foundation framework memory managed Objects vs. unmanaged objects

Source: Internet
Author: User


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.


cfretain cfrelease

The way to get these memory managed objects is to use the cf_returns_retained or cf_returns_not_retained note Declaration, sample code:



-(CGPathRef)makeToPath CF_RETURNS_RETAINED

{

    UIBezierPath* triangle = [UIBezierPath bezierPath];

    [triangle moveToPoint:CGPointZero];

    [triangle addLineToPoint: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.




func CFStringCreateWithCString (_ alloc: CFAllocator !,

         _ cStr: UnsafePointer <Int8>,

          _ encoding: CFStringEncoding)-> 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 method in which the object was obtained is not usedcf_returns_retainedorcf_returns_not_retainedComment Declaration, 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 containsGet, the caller obtains the object without taking ownership of the object, and the return value unmanaged<t>need to callTakeunretainedvalue ()method to obtain 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
?
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




Learn Swift from scratch Learning Notes (day)--core Foundation framework memory managed Objects vs. unmanaged objects


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.