ARC — Automatic Reference Counting & Access Control

Swift uses Automatic Reference Counting for memory management.

Rathish Kannan
3 min readMay 27, 2020

Reference counting applies only to instances of classes. Struct & Enums are not reference types.

Strong

ARC makes sure that this strong referenced object is kept in memory and is not deallocated.

If there is at least one strong reference, ARC makes sure that this strong referenced object is kept in memory and is not deallocated.

Weak

A weak reference is a reference that does not keep a strong hold on the instance it refers to, and so does not stop ARC from disposing of the referenced instance.

Because a weak reference does not keep a strong hold on the instance it refers to, it’s possible for that instance to be deallocated while the weak reference is still referring to it.

ARC automatically sets a weak reference to nil when the instance that it refers to is deallocated. And, because weak references need to allow their value to be changed to nilat runtime, they are always declared as variables, rather than constants, of an optional type

Unowned

Like a weak reference, an unowned reference does not keep a strong hold on the instance it refers to. Unlike a weak reference, however, an unowned reference is used when the other instance has the same lifetime or a longer lifetime..

An unowned reference is expected to always have a value. As a result, ARC never sets an unowned reference’s value to nil, which means that unowned references are defined using non-optional types.

ARC automatically sets a weak reference to nil when the instance that it refers to is deallocated. And, because weak references need to allow their value to be changed to nilat runtime, they are always declared as variables, rather than constants, of an optional type

Access Control

Read : https://cocoacasts.com/improve-swift-performance-through-access-control

  • Internal — This is default access specifier in swift. With this we can access data members and member functions in the same module (target).
  • Public — This is where you can access all data members and member functions within same module and outside of it. But you can’t subclass or override outside the module.
  • Open — same as public, only difference is you can subclass or override outside the module.
  • Fileprivate — As the name say’s, data members and member functions are accessible within the same file.
  • Private — This is where you can have access within the scope of function body or class.

Open vs Public

  • An open class is accessible and subclassable outside of the defining module. An open class member is accessible and overridable outside of the defining module.
  • A public class is accessible but not subclassable outside of the defining module. A publicclass member is accessible but not overridable outside of the defining module.

Method Dispatch in Swift

  1. GCD vs Dispatch queue
  • OperationQueue internally uses Grand Central Dispatch and on iOS.
  • OperationQueue gives you a lot more control over how your operations are executed. You can define dependencies between individual operations for example, which isn't possible with plain GCD queues. It is also possible to cancel operations that have been enqueued in an OperationQueue (as far as the operations support it). When you enqueue a block in a GCD dispatch queue, it will definitely be executed at some point.
  • To sum it up, OperationQueue can be more suitable for long-running operations that may need to be cancelled or have complex dependencies. GCD dispatch queues are better for short tasks that should have minimum performance and memory overhead.
  • Ref: https://stackoverflow.com/questions/7078658/operation-queue-vs-dispatch-queue-for-ios-application

3. Static func vs Class func

  • class functions are dynamically dispatched and can be overridden by subclasses.
  • static func is same as final class func Because it is final, we can not override it in subclass.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response