oc一键复制类
有些时候视图不太一样,又有些类似,需要手动复制类,但是过于繁琐,可以使用这个脚本来复制。
import os
import shutil
def copy_and_rename_class():
# 获取用户输入的原始文件路径和新类名
original_file_path = input("请输入原始文件路径: ")
new_class_name = input("请输入新类名: ")
# 提取文件名和扩展名
original_file_name = os.path.basename(original_file_path)
original_file_base, original_file_ext = os.path.splitext(original_file_name)
# 新文件名
new_header = new_class_name + ".h"
new_implementation = new_class_name + ".m"
new_xib = new_class_name + ".xib"
# 新文件路径
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)
# 寻找对应的.h文件
original_header_path = original_file_path.replace(original_file_ext, ".h")
if not os.path.exists(original_header_path):
print(f"未找到对应的.h文件: {original_header_path}")
return
# 复制.h文件
shutil.copy(original_header_path, new_header_path)
# 打开并替换.h文件中的类名
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("已复制并重命名.h文件!")
# 复制.m文件
shutil.copy(original_file_path, new_implementation_path)
# 打开并替换.m文件中的类名
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("已复制并重命名.m文件!")
# 复制.xib文件
if original_file_ext.lower() == '.xib' and os.path.exists(original_file_path):
shutil.copy(original_file_path, new_xib_path)
# 打开并替换.xib文件中的类名
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("已复制并重命名.xib文件!")
elif original_file_ext.lower() != '.h' and original_file_ext.lower() != '.m':
print("未找到.xib文件!")
print("类复制并重命名成功!")
# 调用函数复制并重命名类
copy_and_rename_class()
发表回复