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

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.



Solution 1:[1]

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

Solution 2:[2]

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.

Solution 3:[3]

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.

Solution 4:[4]

This warning only appear when you hit a breakpoint and the source is in a project where optimization is enabled, preventing you from watching real variable values (every object is shown as nil, even if it's not)

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

Solution 5:[5]

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

Solution 6:[6]

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

Set Debug to None.

Solution 7:[7]

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.

Solution 8:[8]

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

Solution 9:[9]

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

Solution 10:[10]

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.

Solution 11:[11]

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.

Solution 12:[12]

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).

Solution 13:[13]

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.

Solution 14:[14]

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.