简短而有用的Objective-C代码段?

问题描述 投票:25回答:9

自XCode 4起,现在有一个代码段部分,该段在键入时通过自动完成功能提供代码段。我会对大家都存储在其中的摘要非常感兴趣。哪些摘要可为您节省最多的时间(为什么)?

请仅发布实际代码段(意味着不要发表尖刻的“不需要不需要臭皮的代码段”,也不要“我喜欢执行的代码段”),并且仅包含简短而甜美的代码段(即最多不超过20行...)。如果摘要没有明显用处,还请说明您认为为什么有用。 ;)

objective-c xcode code-snippets xcode4
9个回答
10
投票

我不知道这是否有用,但是每当我在任何ViewController中添加UITableView时,我总是使用此代码段。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if(cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
                                      reuseIdentifier:cellIdentifier];
            // Do something here......................
    }
    // Do something here too .........................
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return ;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return ;
}

如果不使用UITableViewController显示表内容,这将非常方便。


9
投票

在给定的秒数后,当前队列上的调度块:

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, <#seconds#>*1e9),
    dispatch_get_current_queue(), <#block#>);

7
投票

这是我的两个评论片段。我经常使用它们。

标题评论:

// ----------------------------------------------------------------------------------------------------------------
# pragma mark -
# pragma mark <#comment#>
# pragma mark -
// ----------------------------------------------------------------------------------------------------------------

子评论:

// ----------------------------------------------------------------------------------------------------------------
//  <#comment#>
// ----------------------------------------------------------------------------------------------------------------

6
投票

我经常添加带有类扩展名的私有类接口:

@interface <#ClassName#> ()
@end

这是为了使公共接口完全摆脱内部的束缚,尤其是现在我们可以具有纯合成的属性(example gist)。


5
投票

这里有几个收藏夹:

https://github.com/mneorr/snippie/tree/master/backup

和这里:

https://github.com/jad/xcode-code-snippets

您可以粘贴在此文件夹中:

~/Library/Developer/Xcode/UserData/CodeSnippets

5
投票

虽然调试此代码段非常有用。它让您知道类名,函数名,还可以添加注释。

NSLog(@"%s [Line %d] %@ ", __PRETTY_FUNCTION__, __LINE__,<#comment#>);

4
投票

工厂片段之间似乎没有类类别:

@interface <#ClassName#> (<#CategoryName#>)
@end

4
投票

这是我出于相同目的而创建的博客...

http://ios-funda.blogspot.in/


0
投票

我的摘要中也有标准的视图生命周期方法(每天都会使用):

我使用键盘快捷键v w a用于

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear: animated];


}

v d l for viewDidLoad

© www.soinside.com 2019 - 2024. All rights reserved.