Cocoapods optimization level for schemes

To anybody seeing this using cocoapods 0.38.0 or later:

Use "pods_project" instead of "project"

The previous answers use the word "project" (installer.project.build_configurations.each)

Project was deprecated and replaced with pods_project. https://github.com/CocoaPods/CocoaPods/issues/3747

post_install do |installer|
  installer.pods_project.build_configurations.each do |config|
    if config.name.include?("Debug")
      config.build_settings['GCC_OPTIMIZATION_LEVEL'] = '0'
    end
  end
end

I use post_install hook in Podfile. Like this:

post_install do |installer|
    installer.pods_project.build_configurations.each do |config|
        if config.name.include?("Debug")
            config.build_settings['GCC_OPTIMIZATION_LEVEL'] = '0'
            config.build_settings['SWIFT_OPTIMIZATION_LEVEL'] = '-Onone'
        end
    end
end

EDIT GCC_OPTIMIZATION_LEVEL is ignored for Swift Pods. If you're using Swift Pods you also need to set SWIFT_OPTIMIZATION_LEVEL.


If you also want the DEBUG macro added for debugging with assertions enabled, you can use the following script:

post_install do |installer|
  installer.project.build_configurations.each do |config|
    if config.name.include?("Debug")
      # Set optimization level for project
      config.build_settings['GCC_OPTIMIZATION_LEVEL'] = '0'

      # Add DEBUG to custom configurations containing 'Debug'
      config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)']
      if !config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'].include? 'DEBUG=1'
        config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] << 'DEBUG=1'
      end
    end
  end

  installer.project.targets.each do |target|
    target.build_configurations.each do |config|
        if config.name.include?("Debug")
          # Set optimization level for target
          config.build_settings['GCC_OPTIMIZATION_LEVEL'] = '0'

          # Add DEBUG to custom configurations containing 'Debug'
          config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)']
          if !config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'].include? 'DEBUG=1'
            config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] << 'DEBUG=1'
          end

          # Enable assertions for target
          config.build_settings['ENABLE_NS_ASSERTIONS'] = 'YES'

          config.build_settings['OTHER_CFLAGS'] ||= ['$(inherited)']
          if config.build_settings['OTHER_CFLAGS'].include? '-DNS_BLOCK_ASSERTIONS=1'
            config.build_settings['OTHER_CFLAGS'].delete('-DNS_BLOCK_ASSERTIONS=1')
          end
        end
      end
  end
end