XCode中的Code Snippet

Xcode4 引入了一个新 feature: Code Snippets,它是一些代码的模版,对于一些常见的编程模式,Xcode 都将这些代码抽象成模版放到 Code Snippets 中,使用的时候,只需要键入快捷键,就可以把模版的内容填到代码中。

1.怎样定义代码块?

选择 "Editor" --> "Create Code Snippet",如下图所示:

点击之后,打开如下的编辑页面:

  • Title:Code Snippet的名称;
  • Summary:摘要;
  • Platform:平台;
  • Language:适用语言;
  • Completion Shortcut:快捷键(建议选一个自己常用的前缀来命名);
  • Completion Scopes:自定义的常用代码块。

示例如下:

之前定义方式:选中整块代码,拖动到 xcode 右下角的 code snippets 区域中即可。xcode 会自动帮你创建一个新的代码片段。之后你可以单击该代码片段,在弹出的界面中选择 edit,即可为此代码片段设置快捷键等信息。

2.常用代码块

2.1Assign属性

@property (nonatomic, assign) <#type#> <#name#>;  //!<<#注释#>

2.2字符串属性

@property (nonatomic, copy) NSString *<#name#>;  //!<<#注释#>

2.3Strong属性

@property (nonatomic, strong) <#type#> *<#name#>;  //!<<#注释#>

2.4Weak修饰

__weak typeof(self)weakSelf = self;

2.5Strong修饰

__strong __typeof(<#weakSelf#>)strongSelf = <#weakSelf#>;

2.6UITableViewDataSource

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return <#Section Number#>;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return <#Rows Number#>;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:<#reuseIdentifier#> forIndexPath:<#indexPath#>];

    <#statements#>

    return cell;
}

2.7UITableViewDelegate

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    <#statements#>
}

2.8生成单例方法

+ (instancetype)shared<#name#> {
    static <#class#> *_shared<#name#> = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _shared<#name#> = <#initializer#>;
    });

    return _shared<#name#>;
}

2.9定义Pragma

#pragma mark - <#Section#>

2.10类扩展

@interface <#Class Name#> ()

<#Continuation#>

@end

2.11GCD主线程队列

dispatch_async(dispatch_get_main_queue(), ^{
    <#code#>
});

2.12GCD全局队列

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {
    <#code#>
});

2.13Window初始化

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    ViewController *vc = [[ViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
    self.window.rootViewController = nav;
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];

2.14Get方法

- (<#TYPE#> *)<#NAME#> {
    if (nil == _<#NAME#>) {
        _<#NAME#> = [[<#TYPE#> alloc] init];
    }
    return _<#NAME#>;
}

2.15Enum枚举

typedef NS_ENUM(NSUInteger, <#Name#>) {
<#value0#> = 0,
<#value1#>
};

2.16OPTIONS枚举

typedef NS_OPTIONS(NSInteger, <#name#>) {
<#value0#> = 0,
<#value1#> = 1
};

 

posted @ 2019-07-11 15:49  LeeGof  阅读(1003)  评论(0)    收藏  举报