Skip to content

Instantly share code, notes, and snippets.

@PhilCai1993
Created January 5, 2016 10:52
Show Gist options
  • Save PhilCai1993/8635b1db1df13cf6d497 to your computer and use it in GitHub Desktop.
Save PhilCai1993/8635b1db1df13cf6d497 to your computer and use it in GitHub Desktop.
Swift查找项目图片
//
// FileEnum.swift
//
//
// Created by PhilCai on 1/5/16.
//
//
import Foundation
// MARK: Usage
// Change path in line 50, 60, 61, 62, 63
// In terminal, type:
// chmod +x FileEnum.swift
// xcrun - swift FileEnum.swift
// MARK: Extensions
extension String {
var isImagePath: Bool {
return (hasSuffix("png") || hasSuffix("PNG") || hasSuffix("jpg") || hasSuffix("JPG"))
}
}
// MARK: Enumeration
let fileManager = NSFileManager.defaultManager()
var allFilePaths: [String] = []
func enumerateFilePath(path: String) {
let objs = fileManager.enumeratorAtPath(path)!.allObjects
for obj in objs {
var isDir: ObjCBool = false
fileManager.fileExistsAtPath(obj as! String, isDirectory: &isDir)
let newPath = path + "/" + (obj as! String)
if isDir {
enumerateFilePath(newPath)
} else {
allFilePaths.append(newPath)
}
}
}
// MARK: Example
let projectPath = "/Users/Phil/Desktop/Eleme/nevermore"
enumerateFilePath(projectPath)
//print(allFilePaths)
let imagePaths = allFilePaths.filter{$0.isImagePath}
let imagePaths2x = imagePaths.filter{$0.rangeOfString("@2x") != nil}
let imagePaths3x = imagePaths.filter{$0.rangeOfString("@3x") != nil}
let otherImagePaths = Set(imagePaths).subtract(imagePaths2x).subtract(imagePaths3x).sort()
//print(imagePaths)
try imagePaths2x.joinWithSeparator("\n").writeToFile("/Users/Phil/Desktop/Eleme/2xresult", atomically: false, encoding: NSUTF8StringEncoding)
try imagePaths3x.joinWithSeparator("\n").writeToFile("/Users/Phil/Desktop/Eleme/3xresult", atomically: false, encoding: NSUTF8StringEncoding)
try imagePaths.joinWithSeparator("\n").writeToFile("/Users/Phil/Desktop/Eleme/allresult", atomically: false, encoding: NSUTF8StringEncoding)
try otherImagePaths.joinWithSeparator("\n").writeToFile("/Users/Phil/Desktop/Eleme/otherresult", atomically: false, encoding: NSUTF8StringEncoding)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment