Swift without ARC

Is it possible to write applications using manual memory management (instead of automatically counting links) using SWIFT?

Is it possible to invoke retain and release with Swift?

+6
source share
3 answers

You can call retain and release on Unmanaged<T> . Unmanaged are basically pointers to objects that are out of ARC control. But you will need to convert to managed pointers to interact with the Cocoa API.

But you would not want to do this, if only in exceptional circumstances. And a project that is not converted to ARC is not such a situation.

+10
source

Note August 29, 2014 . My answer below is incorrect, maybe look at @newacct answer

No, you can’t. Read more about Automatic Link Counting .

You must have a very good reason not to want ARC

+1
source

There is currently no way to disable ARC for fast as you can with objc (for example, a file with a compiler flag). As others have pointed out, there are Unmanable and UnsafeMutablePointer APIs designed to interact with C APIs that allow you to work with ARC in some cases. However, these APIs are cumbersome and require significant changes to your code (for example, to interact with arrays).

Swift people would usually want you to rewrite your code to use structures and avoid links where possible. But this makes porting existing code painful.

In my other post, there are some suggestions regarding ARC performance: Swift Dictionary is slow even with optimizations: performing incomplete save / release? but none of them are very satisfying.

I went so far as to write code to subsequently process the compiled mach-O object code and remove the ARC swift_retain / release calls from some classes as an experiment. But I really hope that now that Swift is open, we can just change it to make it more flexible in the future.

0
source

Source: https://habr.com/ru/post/974458/


All Articles