247

Trying to step into AFNetworking code generates following warning:

[Project Name] was compiled with optimization - stepping may behave oddly; variables may not be available.

And of course I'm not able to debug the code. To be specific I'm trying to debug UIImageView+AFNetworking category which seems impossible. Changing the code has no effect (tried NSLog, etc) and when trying to step in compilers goes to assembly code and shows UIImageView+TVASTAFNetworking as category name which does not exist anywhere in the code base.

enter image description here

Using Xcode 7. iOS 9 & 8. Cocoapods (no Framework)

UPDATE I forgot to mention that Optimizer is set to none for both release and debug configuration and I am in fact using Debug config.

enter image description here

UPDATE 2

Strip Debug Symbols Is off as well.

Mojtaba
  • 6,012
  • 4
  • 26
  • 40
  • I recall there being some sort of a "Strip Debug Symbols" option. That probably would cause this problem. Is it off? – NobodyNada Sep 27 '15 at 20:00
  • 1
    @NobodyNada Yes `Strip Debug Symbols` is off. – Mojtaba Sep 28 '15 at 23:56
  • Weird I just tried to turn Strip Debug Symbol on, and the warning disappeared 8-| – Gomino Oct 01 '15 at 14:21
  • @Mojtaba Hey, did you find out how to solve the problem? I'm stuck with the same problem since updated to Xcode 7 – Hadi Sharghi Oct 12 '15 at 09:55
  • @Hadu: Unfortunately No – Mojtaba Oct 13 '15 at 23:15
  • Argh! So painfully annoying, same issue here and wasted hours trying to sort it out. If I start a brand new project everything works fine :/ – Rog Oct 14 '15 at 06:03
  • Hi! Mojtaba! Have you already solved the problem? I have the same problem, so if you've already solve this, plz let me know. – Toshi Oct 26 '15 at 23:17
  • @yomihito unfortunately no. But try answers below one might work for you. – Mojtaba Oct 27 '15 at 17:09
  • 1
    @Mojtaba Hi, I am stuck in a similar situation and have tried everything mentioned in this thread, all in vain though. Have you found any solution ? – user3752049 Feb 08 '16 at 07:11
  • @user3752049 not yet. I copied all files to a brand new project and it worked fine tho. (including pods) – Mojtaba Feb 11 '16 at 01:05
  • I have had a similar issue with waiting for test expectations (optimisation turned off). You should check which tests if any are hanging. – StackRunner Nov 18 '16 at 11:07
  • As said earlier. "There are actually two optimisation level fields under Build Settings, one for LLDB and one for Swift compiler." This did it for me, thanks. – Klajd Deda Aug 10 '17 at 18:10

16 Answers16

204

If your project is using Swift, there are two separate "Optimization Level" settings in the project/target configuration.

Make sure you set them both correctly:

  1. Select your project in the Project Navigator pane
  2. Select your project's settings under the "PROJECT" tree
  3. Click "Build Settings" tab
  4. Search for "Optimization Level" and you'll see two settings, one for LLVM and one for swift.
  5. Set the appropriate setting (None [-O0] for LLVM and None [-0none] for Swift) for the build config in question.

was compiled with optimization stepping may behave oddlyvariables may not be available

Doing this resolved that warning for me.

Albert Bori
  • 9,832
  • 10
  • 51
  • 78
  • 1
    What with mixed language projects...? – Nat Aug 28 '18 at 11:06
  • What's the difference between LLVM & Swift? Is it that Swift is what I wrote in the project and LLVM is for whatever I'm doing in regards to debugging in the debugger ie things like `po` , `p`, `expr` ... ? – mfaani Oct 07 '18 at 22:47
  • Thank you, I just don't understand how it changed by itself... – Medhi Aug 30 '22 at 15:56
142

It looks like your project is in Release mode. Release mode compiles the app with lots of optimizations, but debuggers hate optimizations, so to reliably debug the app, you need to switch it to Debug mode which reduces optimization and adds a bunch of debugging information. To switch it to Debug mode:

  • Click on your scheme in the top-left corner of Xcode.

Click on your scheme in the top-left corner of Xcode.

  • Select "Edit Scheme..."

Select "Edit Scheme..."

  • Click on the "Build Configuration" dropdown. and change it to Debug mode.

Click on the "Build Configuration" dropdown.

Iulian Onofrei
  • 9,188
  • 10
  • 67
  • 113
NobodyNada
  • 7,529
  • 6
  • 44
  • 51
  • 10
    I am using debug. and sure optimizer is OFF. – Mojtaba Sep 24 '15 at 23:40
  • This was interesting. Also because I had it set to **Release** mode some of my breakpoints were not getting hit while some others were getting hit ¯\_(ツ)_/¯ – mfaani Oct 07 '18 at 22:49
68

This warning only appears when you hit a breakpoint but the source is in a project has optimization enabled. This prevents you from watching real variable values (every object is shown as nil, even if it's not)

Are you using CocoaPods?

In my case, it only happened when debugging step by step through a cocoapod dependency.

So even if you have your main target and project settings correctly set (Strip Debug Symbol=OFF, and Optimization level None), you need to make sure it is the same for the Pod project your hitting the breakpoint from.

enter image description here

mfaani
  • 33,269
  • 19
  • 164
  • 293
Gomino
  • 12,127
  • 4
  • 40
  • 49
  • 1
    Read other answers, they are suggesting the same. but they are all off in my project (aka `None [-O0]`) – Mojtaba Oct 02 '15 at 17:30
  • 2
    I know you said it is off in your projet, just like it was in mine, but did you take a look at your Pod dependencies projects configuration? – Gomino Oct 02 '15 at 19:57
  • Same problem here with Xcode 7. Probably the problem appeared when I started using the trachkerbird library. But the breakpoint is inside my own code. – Mike Dec 06 '15 at 09:01
  • 1
    For Pods project, change the "PROJECT" optimization level setting to None works. (no changes for any pod target). – ooops Dec 26 '16 at 06:51
  • 2
    This is incorrect. It will get overridden when you `pod install` again. Change it [like this](https://github.com/CocoaPods/CocoaPods/commit/81d3550d719cc7595e9322ee4c6dc2c2ef73893f#commitcomment-24019622). – Iulian Onofrei Sep 01 '17 at 08:25
  • `This warning only appear when you hit a breakpoint and the source is in a project where optimization is enabled` should mean that I would not be looking for a solution without any breakpoints, so I have a feeling this isn't the case. – Abandoned Cart Jul 30 '19 at 10:59
14

Turns out that after importing an old project (Xcode 7.x+) to new Xcode 8.3 (8E162), probably due to compiler optimization, the Swift Compiler - Optimization Level was set by default to Fast, Single-File Optimization:

Before

Changing it to none, solved the issue:

after

valvoline
  • 7,737
  • 3
  • 47
  • 52
12

Editor -> Validate Settings then confirm all changes. Then you should get Swift Compiler Optimisation Level in place

Set Debug to None.

Box Box Box Box
  • 5,094
  • 10
  • 49
  • 67
james sa
  • 631
  • 5
  • 14
  • 1
    I did it. Not worked for me. I m facing this issue in ios build which is genereated by Unity. – Chandni Jun 01 '18 at 08:33
12

This was the solution for me...

Along the lines of gimino's answer, if you are using cocoapods, add a line like this to the Podfile:

xcodeproj 'MyProject', 'Debug - local'=>:debug, 'Debug - staging'=>:debug, 'Debug - PRODUCTION'=>:debug

or for cocoapods versions >= 1.0 (thanks Diejmon)

project 'MyProject', 'Debug - local'=>:debug, 'Debug - staging'=>:debug, 'Debug - PRODUCTION'=>:debug

Where MyProject has 'Debug - local', 'Debug - staging', 'Debug - PRODUCTION' as debug configuations in addition to the standard 'Debug'

By default, cocoapods will usually generate pod configurations as Release, this Podfile line allows you to tell it they are debug.

wils
  • 791
  • 7
  • 7
10

I ran into the same issue today, and figured it out (at least in my case). I'm also using CocoaPods, and I was having this issue when running my test target (Swift mixed with ObjC).

I'm using Xcode 7.2, with iOS 9.2 SDK.

In the image below, you can see the optimizations for the target and project before my change:

optimization level before change

The surprising thing is that even though the resolved Optimization is None [-O0], only after changing the project setting from -Os to -O0 did the compiler stop optimizing the target.

Below you can see my final settings:

optimization level after change

Box Box Box Box
  • 5,094
  • 10
  • 49
  • 67
André Neves
  • 1,066
  • 10
  • 14
10

It's been a long time but I finally solved the issue. There is a third optimization flag LTO or Link Time Optimization and Surprisingly no one have mentioned it here and for some reason I didn't pay attention to it either. It's right there above the Optimization Level setting as you can see in many screen shots posted here.

So to summarize it there are 3 different optimization flags you want to turn off for debugging :

  • LLVM Link Time Optimization (-flto)
  • LLVM Optimization Level (-O)
  • Swift Compiler Optimization Level

enter image description here

More information about LTO: http://llvm.org/docs/LinkTimeOptimization.html

Mojtaba
  • 6,012
  • 4
  • 26
  • 40
6

If you need to disable optimisations for your Swift pods so you can debug into them, add the following to your Podfile. This will disable the optimisations for debug builds only.

post_install do |installer|
  installer.pods_project.targets.each do |target|
    target.build_configurations.each do |config|
      if config.name == "Debug"
        config.build_settings["SWIFT_OPTIMIZATION_LEVEL"] = "-Onone"
      end
    end
  end
end
Al Priest
  • 183
  • 3
  • 7
2

Are you sure your debug configuration doesn't optimize code (it shouldn't)? It looks like you've accidentally enabled optimizations for debug configuration and you should turn it off from target's settings.

Can Poyrazoğlu
  • 33,241
  • 48
  • 191
  • 389
1

This error happened to me twice, and in every case was a mistake in the URL parameter used to request a service. In one case the URL had some space in the port section in the other case some Optional Value wasn't being unwrapped.

So the fix was to be sure the url for the request is well formed. More information about my case, and similar reporting the same here.

le0diaz
  • 2,488
  • 24
  • 31
  • Thanks for this answer. The URL was incorrect in my case as well. I did not have to change any compiler optimization settings like the other answers have suggested. – Dhruv Saraswat Mar 19 '21 at 11:49
0

This may be an oversimplification, but are you building for Release or with optimization (which remove symbols from Swift or LLVM) too high? If so edit your scheme and switch to Debug, or edit your Build Settings for swift or LLVM optimization to None (0).

Aaron
  • 583
  • 1
  • 5
  • 12
0

Just in case someone is facing this issue while debugging a pod that uses a C library internally, there is another thing you have to change in the project settings to make it work in addition to everything else listed in the thread.

Go to Pods project settings -> Your C-using target -> Build Settings -> Apple Clang - Custom Compiler Flags -> Other C Flags and remove the -O3 flag that got there somehow.

Dmitry Serov
  • 861
  • 11
  • 22
0

In my case, I had created a new scheme for a custom configuration ("Mock") which was not using optimizations, but had overlooked the Test and Analyze contexts of the scheme, which were still using the Release configuration (I must've duplicated the Release scheme initially).

Changing these to use my custom configuration removed the runtime optimizations.

Nathan Hosselton
  • 1,089
  • 1
  • 12
  • 16
0

If you use cocoapods and Objc, put the following code in your Podfile, it works for my case, pay attention to choose the config.name for you debug and not change Release

post_install do |installer|
    installer.pods_project.targets.each do |target|
      target.build_configurations.each do |config|
          if config.name == 'Test'
              config.build_settings['GCC_OPTIMIZATION_LEVEL'] = 0
          end
      end
    end
end
leaveslife
  • 342
  • 2
  • 9
0

In my case, it was because we introduced Swift packages to organize code. If I tried debugging code inside a Swift package, I would see that message. The solution was to rename config schemes to use the word Development instead of Dev so Xcode could map DEBUG configuration accordingly. https://www.sobyte.net/post/2022-10/spm-in-xcode/

narduk
  • 964
  • 1
  • 9
  • 19