preface
Here’s a handy tool to turn to when slimming projects: Perihery. Support for OC, not only can filter out unused classes, but also struct, protocol, function, property, enum, typeAlias, associatedType and other scenarios, you do not use methods, not used parameters can filter out.
Method of use
- Install Periphery using Homebrew
If homebrew is not installed, use the following statement to install it. If it is installed, skip:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Copy the code
Then install Periphery using Homebrew:
brew install peripheryapp/periphery/periphery
Copy the code
- Introduce the Periphery library into your project
Open your project’s Podfile and add:
pod 'Periphery'
Copy the code
Run pod Install.
- Scanning project
Go to the root directory of the project and execute:
periphery --setup
Copy the code
The first time you do it, just setup. Setup has a few options to select:
Welcome to Periphery!
This guided setup will help you select the appropriate configuration for your project.
* Inspecting project...
Select build targets to analyze:
? Delimit choices with a single space, e.g: 1 2 3, or 'all' to select all options
1 SwiftLoseWeight
> all
? Delimit choices with a single space, e.g: 1 2 3
1 SwiftLoseWeight
> 1
Assume Objective-C accessible declarations are in use?
? Declarations exposed to the Objective-C runtime explicitly with @objc, or implicitly by inheriting NSObject will be assumed to be in use. You may want to choose 'Yes' here if your project contains a mix of Swift & Objective-C.
(Y)es/(N)o > Y
Assume all 'public' declarations are in use?
? You should choose 'Yes' here if your public interfaces are not used by any selected build target, as may be the case for a framework/library project.
(Y)es/(N)o > Y
Save configuration to .periphery.yml?
(Y)es/(N)o > Y
Copy the code
After setup, scan instruction is executed. Here is the output of a Demo I tested:
Swift code:
class ViewController: UIViewController {
override func viewDidLoad(a) {
super.viewDidLoad()
test()
testStruct()
}
}
class TestObject {
func testUsedMethod<T> (_ t: T.name: String) {}
func testUnUsedMethod(a){}}extension TestObject {
func testUnUsedMethodInExtension(a){}}struct TestStruct1 {
var objectInStruct = 0
func structMethod_1(_ object: String) {}
private func structMethod_2(a){}}struct TestStruct2 {
var objectInStruct = 0
private func structMethod_1(_ object: String) {}
private func structMethod_2(a){}}func test(a) {
let object = TestObject()
object.testUsedMethod(1, name: "rui")}func testStruct(a) {
var struct_1 = TestStruct1()
struct_1.objectInStruct = 1
struct_1.structMethod_1("amazing")}Copy the code
Command line output (XXX is the full path to the root of your project) :
xxx/ViewController.swift:19:30: warning: Parameter 't' is unused
xxx/ViewController.swift:19:36: warning: Parameter 'name' is unused
xxx/ViewController.swift:20:10: warning: Function 'testUnUsedMethod()' is unused
xxx/ViewController.swift:24:10: warning: Function 'testUnUsedMethodInExtension()' is unused
xxx/ViewController.swift:28:9: warning: Property 'objectInStruct' is assigned, but never used
xxx/ViewController.swift:29:27: warning: Parameter 'object' is unused
xxx/ViewController.swift:30:18: warning: Function 'structMethod_2()' is unused
xxx/ViewController.swift:33:8: warning: Struct 'TestStruct2' is unused
Copy the code
As you can see, not only didn’t use to approach, even didn’t use to the parameter will be filtered, is defined in the extension, but without being used as well will be filtered, but no matter how, screening out classes or yourself according to the business to have a look at exactly have actually used, the second confirmed to delete again.
If your project runs through.xcworkspace (and it usually does), you’ll need to specify additional parameters, such as:
periphery scan --workspace MyApp.xcworkspace --schemes MyApp --targets MyApp
Copy the code
other
Add Pod Periphery to your Podfile, and execute Pod Install.
CD to the project root directory, run:
periphery scan --workspace MyApp.xcworkspace --schemes NewSS --targets MyApp
Copy the code
If you are using iterm2, you may not be able to view all of the output. You can open the Settings for Iterm2 and check the boxes shown in the following figure to see the complete output.
Please indicate the source of reprint.