Oc One click Copy Class

  • content
  • comment
  • relevant

Sometimes the views are different or similar, and the classes need to be copied manually, but it is too cumbersome. You can use this script to copy.

 import os import shutil def copy_and_rename_class(): #Get the original file path and new class name entered by the user Original_file_path=input ("Please enter the original file path:") New_class_name=input ("Please enter a new class name:") #Extract file name and extension original_file_name = os.path.basename(original_file_path) original_file_base, original_file_ext = os.path.splitext(original_file_name) #New File Name new_header = new_class_name + ".h" new_implementation = new_class_name + ".m" new_xib = new_class_name + ".xib" #New File Path new_header_path = os.path.join(os.path.dirname(original_file_path), new_header) new_implementation_path = os.path.join(os.path.dirname(original_file_path), new_implementation) new_xib_path = os.path.join(os.path.dirname(original_file_path), new_xib) #Find the corresponding. h file original_header_path = original_file_path.replace(original_file_ext, ".h") if not os.path.exists(original_header_path): Print (f "corresponding. h file not found: {original_header_path}") return #Copy. h file shutil.copy(original_header_path, new_header_path) #Open and replace the class name in the. h file with open(new_header_path, 'r') as header_file: header_content = header_file.read() header_content = header_content.replace(original_file_base, new_class_name) with open(new_header_path, 'w') as header_file: header_file.write(header_content) Print ("The. h file has been copied and renamed!") #Copy. m file shutil.copy(original_file_path,  new_implementation_path) #Open and replace the class name in the. m file with open(new_implementation_path, 'r') as implementation_file: implementation_content = implementation_file.read() implementation_content = implementation_content.replace(original_file_base, new_class_name) with open(new_implementation_path, 'w') as implementation_file: implementation_file.write(implementation_content) Print ("The. m file has been copied and renamed!") #Copy. xib file if original_file_ext.lower() == '.xib' and os.path.exists(original_file_path): shutil.copy(original_file_path, new_xib_path) #Open and replace the class name in the. xib file with open(new_xib_path, 'r') as xib_file: xib_content = xib_file.read() xib_content = xib_content.replace(original_file_base, new_class_name) with open(new_xib_path, 'w') as xib_file: xib_file.write(xib_content) Print ("The. xib file was copied and renamed!") elif original_file_ext.lower() != '. h' and original_file_ext.lower() != '. m': Print (". xib file not found!") Print ("Class copied and renamed successfully!") #Call the function to copy and rename the class copy_and_rename_class()

comment

zero Comments

Post reply

Your email address will not be disclosed. Required items have been used * tagging