我们每次新建文件的时候,Xcode总是默认帮我们生成一些代码。比如我们继承UIViewController创建了一个MyViewController的类文件,.m文件如下:
//
// MyViewController.m
// TemplateDemo
//
// Created by herui on 2018/4/10.
// Copyright © 2018年 herui. All rights reserved.
//
#import "MyViewController.h"
@interface MyViewController ()
@end
@implementation MyViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
可以看见有些代码并非是我们需要的:
- 当项目中并没有用到Storyboard时,prepareForSegue:sender:方法就不需要了;
- didReceiveMemoryWarning方法一般情况下也不会做特殊处理;
这些代码每次都得手动移除,能不能不让Xcode生成这些代码呢????
答案是:可以。(哈哈哈。。 当然可以,不然你就看不见这篇文章啦~~)
找到并拷贝此文件:
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/File Templates/Source/Cocoa Touch Class.xctemplate/UIViewControllerObjective-C/___FILEBASENAME___.m
随便找个地方粘贴下,然后双击打开。。。看看这些代码看起来是不是很眼熟??咱们熟悉的viewDidLoad方法、didReceiveMemoryWarning方法都在这儿了。
//___FILEHEADER___
#import "___FILEBASENAME___.h"
@interface ___FILEBASENAMEASIDENTIFIER___ ()
@end
@implementation ___FILEBASENAMEASIDENTIFIER___
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
这就是Xcode创建文件时默认的代码生成模版文件。删掉不需要的代码并保存。
重启Xcode->新建文件,看看那些无用的代码是不是都没啦?!
//
// MyViewController.m
// TemplateDemo
//
// Created by herui on 2018/4/10.
// Copyright © 2018年 herui. All rights reserved.
//
#import "MyViewController.h"
@interface MyViewController ()
@end
@implementation MyViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
@end
如果发现前边的改动并没有生效,先别着急,看一下模版文件的目录:
目录截图- 如果你的项目语言是Swift,就应该修改UIViewControllerSwift目录下的文件;
- 如果你习惯在创建VC类文件的时候勾上Xib,那么就应该修改UIViewControllerXIBObjective-C目录下的文件;
- 同理,则应该修改UIViewControllerXIBSwift目录下的文件;
以上,是不是解决了你的问题了呢?(掌声在哪里~)
网友评论