Skip to content

Instantly share code, notes, and snippets.

@subdigital
Created January 5, 2013 21:59
Show Gist options
  • Star 42 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save subdigital/4463913 to your computer and use it in GitHub Desktop.
Save subdigital/4463913 to your computer and use it in GitHub Desktop.
What are your favorite Xcode Snippets? Add them in the comments.
// Property Nonatomic Strong
// Platform: All
// Completion Scopes: ClassInterfaceMethods
@property (nonatomic, strong) <# class_name #> *<# variable_name #>;
@subdigital
Copy link
Author

Also useful is propstring:

@property (nonatomic, copy) NSString *<# variable #>;

@jwilling
Copy link

jwilling commented Jan 5, 2013

I just have this one listed with a shortcut of init.

self = [super <#init function#>];
if (self == nil) return nil;

<#initializations#>

return self;

@subdigital
Copy link
Author

I also use tvds to get a quick UITableViewDelegate implementation

- (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];

    }

    return cell;
}

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

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

@RuiAAPeres
Copy link

Been using this a lot:

 dispatch_queue_t workingQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(workingQueue,
                   ^{
                     // Heavy task

                       dispatch_async(dispatch_get_main_queue(),
                                      ^{
                                        // Update UI
                                      }
                                      );
                   });

@jnjosh
Copy link

jnjosh commented Jan 5, 2013

I actually have a whole series of those property snippets:

'pnc' - Copy Property
@Property (nonatomic, copy) <#Class name#> *<#Property Name#>;

'pns' - Strong Property
@Property (nonatomic, strong) <#Class name#> *<#Property Name#>;

'pnw' - Weak Property
@Property (nonatomic, weak) <#Class name#> *<#Property Name#>;

'pna' - Assign Property
@Property (nonatomic, assign) <#Type#> <#Property Name#>;

I also regularly use:

'mark' - create a pragma mark

pragma mark - <#Note#>

'todo' - A todo marked with my initials
// TODO (JNJ): <#Note#>

@subdigital
Copy link
Author

swf

[NSString stringWithFormat:@"<# format_string #>", <# args #>];

@jnjosh
Copy link

jnjosh commented Jan 5, 2013

Also, @mattt has a repo of several: https://github.com/mattt/Xcode-Snippets

@waynehartman
Copy link

UIWebViewDelegate

#pragma mark - UIWebViewDelegate

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    if (error.code != NSURLErrorCancelled) {

    }
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {

}

- (void)webViewDidStartLoad:(UIWebView *)webView {

}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    return YES;
}

@jasongregori
Copy link

weakself

__weak __typeof(self)weakself = self;

@gbasile
Copy link

gbasile commented Jan 7, 2013

These are some of the snippet I use with Dash, I normally use two empty spaces at the end of each snippet to avoid disambiguations

enum

typedef NS_ENUM(__type__, __name__) {
    @cursor
};

ifnotkind

if (!__variable__ || ![__variable__ isKindOfClass:[__class__ class]]) {
    @cursor
}

log

NSLog(@"@cursor");

errorDomain

#define __NAMEERRORDOMAIN__ @"__NAMEERRORDOMAIN__"
typedef NS_ENUM(__TYPEERRORDOMAIN__, __NAMEERRORCODE__) {
    __ERROR1__,
    __ERROR2__
};

s

@""

doc

//! __Description__
//! @param __param1Name__   __param1Description__
//! @return __returnDescription__   

safeselfblock

__block typeof(self) blockSafeSelf = self;

@aijaz
Copy link

aijaz commented Jan 11, 2013

These are great ideas. My favorite ones:

nss

NSString 

nsf

[NSString stringWithFormat:@"<#format#>", <#values#>]

nsl

NSLocalizedString(@"<#key#>", @"<#comment#>")

@djibouti33
Copy link

Quick log (qlog)

NSLog(@"<#message#>");

Log file and method name (method):

NSLog(@"%s", __PRETTY_FUNCTION__);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment