Understanding the Release Action When Migrating from Objective-C to Swift

Asked 1 years ago, Updated 1 years ago, 109 views

I'm thinking of moving the app from objective-C to swift.

The following is dealloc for UIWindow*window; in AppDelegate.m.
When I was writing in objective-C, I wrote the release process in dealloc as follows.
There is no release with swift, so I am worried about how to release it in the same way.

-(void)dealloc{
    [_window release];
    super dealloc;
}

Is it not necessary to write down the processing in Swift?
Also, I would appreciate it if you could let me know when you need to describe it.

As I am a beginner, I think there is a lot of information that is not enough to ask questions.
I would appreciate it if you could point that out as well.

Thank you for your cooperation.

swift memory-management

2022-09-30 16:00

1 Answers

Is there no need to write any particular processing on swift?
You do not need to write a standard release like the one presented in Swift.You don't even need to write dealloc because you don't need a release.

I think this area is relatively easy to accept if you use ARC even with Objective-C.If you haven't tried it yet, you can see Convert>To Objective-CARC... on the Edit menu of Xcode, so try to apply it to a simple Objective-C project.You will see that the release is deleted from everything.
 Simply put, ARC is a feature that says, "If the compiler thinks it needs release, retain, it automatically generates that code in that location." If you look at the assembly that you have compiled with ARC enabled, you can see that retain, release is embedded here and there.

Therefore, ARC is enabled by default on Swift and cannot be turned off by configuration.If you write a regular app, you don't have to write release, retain, etc. by yourself.
(By the way, the release and retention of CFType that Objective-C ARC did not manage automatically, so it is very easy to use a lot of CF, but if you have never used Objective-C ARC, you may not appreciate it.)

Also, I would appreciate it if you could let me know when you need to write.
Basically, the description itself cannot be written as described above.All methods that manipulate the retain count, such as release, retain, etc., are disabled from Swift.(If it's not basic, you can use the Unmanaged type, but if you use it incorrectly, it doesn't have Unsafe, but it's a very scary type, so I won't introduce it in short words.)

When I use ARC, I almost forget that instances are managed using the retain count, but when circular references occur, ARC cannot manage them, which means memory leaks.Just be careful and think that when you write code on Swift, you don't write retain, release yourself.


2022-09-30 16:00

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.