ChatGPT解决这个技术问题 Extra ChatGPT

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

https://i.stack.imgur.com/P9eUE.png

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.

https://i.stack.imgur.com/mtoTT.png

UPDATE 2

Strip Debug Symbols Is off as well.

I recall there being some sort of a "Strip Debug Symbols" option. That probably would cause this problem. Is it off?
@NobodyNada Yes Strip Debug Symbols is off.
Weird I just tried to turn Strip Debug Symbol on, and the warning disappeared 8-|
@Mojtaba Hey, did you find out how to solve the problem? I'm stuck with the same problem since updated to Xcode 7
@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 ?

A
Albert Bori

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:

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

https://i.stack.imgur.com/vyc2n.png

Doing this resolved that warning for me.


What with mixed language projects...?
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 ... ?
I
Iulian Onofrei

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.

https://i.stack.imgur.com/6eHnt.png

Select "Edit Scheme..."

https://i.stack.imgur.com/fm6vP.png

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

https://i.stack.imgur.com/855XL.png


I am using debug. and sure optimizer is OFF.
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 ¯_(ツ)_/¯
m
mfaani

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.

https://i.stack.imgur.com/FxLcc.png


Read other answers, they are suggesting the same. but they are all off in my project (aka None [-O0])
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?
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.
For Pods project, change the "PROJECT" optimization level setting to None works. (no changes for any pod target).
This is incorrect. It will get overridden when you pod install again. Change it like this.
v
valvoline

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:

https://i.stack.imgur.com/Zpbfg.jpg

Changing it to none, solved the issue:

https://i.stack.imgur.com/4Yn6B.jpg


I did it. Not worked for me. I m facing this issue in ios build which is genereated by Unity.
w
wils

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.


A
Ashish Ahuja

https://i.stack.imgur.com/OmGQ2.png

Set Debug to None.


I did it. Not worked for me. I m facing this issue in ios build which is genereated by Unity.
A
Ashish Ahuja

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:

https://i.stack.imgur.com/GelYY.png

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:

https://i.stack.imgur.com/JQv8d.png


M
Mojtaba

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

https://i.stack.imgur.com/NeO19.png

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


I did it. Not worked for me. I m facing this issue in ios build which is genereated by Unity.
@Chandni, Here I faced the same problem did you find the solution?
A
Al Priest

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

C
Can Poyrazoğlu

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.


optimization is off for everything. even release build just to make sure
@Mojtba But release mode turns optimizations on!
l
le0diaz

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.


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.
A
Aaron

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


D
Dmitry Serov

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.


N
Nathan Hosselton

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.

https://i.stack.imgur.com/anj6J.png


关注公众号,不定期副业成功案例分享
Follow WeChat

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now