In the course of fixing the Oclint error, the Podfile was modified as follows

post_install do |installer| installer.pods_project.targets.each do |target| target.build_configurations.each do |config|  config.build_settings['COMPILER_INDEX_STORE_ENABLE'] = "NO" end end endCopy the code

But what does this script mean? Familiarize yourself with Ruby syntax

Iterators in Ruby

Eg, here is a Ruby iterator.

collection.each do |variable|
   code
end
Copy the code

so

target.build_configurations.each do |config|
    config.build_settings['COMPILER_INDEX_STORE_ENABLE'] = "NO"
end
Copy the code

Gets the build_configurations of the target object (everything object in Ruby) and iterates through it, fetching the build_setting from the config, which is a hash similar to a dictionary. Set COMPILER_INDEX_STORE_ENABLE to NO

Get familiar with Ruby syntax

Methods in Ruby

Define the simplest method

// Define a method def test puts "test" end // call method testCopy the code

Define a method that takes parameters

Def sum(a,b) c = a+b puts c endCopy the code

Define a method with default parameters


def sum(a=1,b=2)
	c = a+b
	puts c
end

sum
Copy the code

In ruby block

Define the simplest block

def testblock
	yield
end

testblock {puts 'hello world'}
Copy the code

You can see that we are passing in a block of code that can be invoked using the yield keyword

We can also add parameters to blocks

Def testblock yield "zhang SAN" end testblock {| name | puts "# {name}, hello!" } // Output zhang SAN, hello!Copy the code

We can also change the curly braces to do end

Def testblock yield "bill" end testblock do | name | puts "# {name}, hello!" Hello, Li Si!Copy the code

Let’s tweak what the code looks like

Testblock do | name | puts "# {name}, hello!" endCopy the code

At this point, we’ve got the code in our podfile.

post_install do |installer| installer.pods_project.targets.each do |target| target.build_configurations.each do |config|  config.build_settings['COMPILER_INDEX_STORE_ENABLE'] = "NO" end end endCopy the code
  • Called the methodpost_installAnd passes a block as an argument
  • methodspost_installThe internal will call the incomingblockAnd to theblockIncoming parametersinstallerobject
  • The code inside the Block is pretty straightforward, we know it’s an iterator, iterates through all the targets, gets the build configuration under target, getsbuild_settingsAnd modify the configurationCOMPILER_INDEX_STORE_ENABLE The value is NO

Hook functions

In addition to post_install, cocoapods also has a hook function, pre_install, that allows you to do something in the pod library before it is installed. The post_install hook function allows us to do something before the project is written to the hard disk.

Hashes in Ruby

Similar to creating a dictionary hash

H = Hash [100, "a" = > "b" : 200] puts "# {H [' a ']}" / / output 100Copy the code

extended

Let’s go back to the contents of our podfile again to make it clearer

source 'https://github.com/CocoaPods/Specs.git' platform :ios, '9.0' target 'testPrivatePod' do pod 'MBProgressHUD' pod 'TZImagePickerController' pod 'SDWebImage' pod 'PonyDebugger' :source => 'https://github.com/CocoaPods/Specs.git' post_install do |installer| installer.pods_project.targets.each do |target| target.build_configurations.each do |config| config.build_settings['COMPILER_INDEX_STORE_ENABLE'] = "NO" end end end endCopy the code

Feels like it’s all called methods?