Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Swift incompatible with pre-ARC Objective-C?

I'm starting to add Swift files to a very large, legacy Objective-C project that doesn't use ARC.

When I compile the project, I get warnings for every property emitted in the ProjectName-Swift.h bridge header:

  • No 'assign', 'retain', or 'copy' attribute is specified - 'assign' is assumed
  • Default property attribute 'assign' not appropriate for non-GC object

It seems like Swift is emitting ARC-based Objective-C code.

Is this a limitation/bug in this particular release of Swift, or is Swift designed to work only with ARC code?

like image 811
Bill Avatar asked Jun 07 '14 13:06

Bill


People also ask

Should I use Swift or Objective-C?

Objective-C has a superior runtime compared to Swift. It's probably going to be several years before Swift can catch up. If you're using powerful SDKs, Objective-C is also your best option here as well. I'd still recommend that new developers start off learning Swift.

What is Swift Objective-C?

Swift is a successor to both the C and Objective-C languages. It includes low-level primitives such as types, flow control, and operators. It also provides object-oriented features such as classes, protocols, and generics, giving Cocoa and Cocoa Touch developers the performance and power they demand.

How do I disable arc in Objective-C?

You can disable ARC for a specific class using the -fno-objc-arc compiler flag for that class. In Xcode, in the target Build Phases tab, open the Compile Sources group to reveal the source file list. Double-click the file for which you want to set the flag, enter -fno-objc-arc in the pop-up panel, then click Done.


1 Answers

You can mix ARC and non-ARC code. All ARC-compiled code does is manage its memory automatically to the user. You should enable ARC for your project and then disable ARC for individual files using -fno-objc-arc or enable ARC for just Swift files using -fobjc-arc (this may not be necessary as the compiler may automatically be adding it). Swift code is eventually compiled to use the same Objective C runtime, and ARC calls inserted by the compiler are the same as in Objective C. After compilation, ARC and non-ARC code behave the same.

like image 175
Léo Natan Avatar answered Oct 10 '22 04:10

Léo Natan