NSHipster 本周的主题是 《Xcode Snippets》 ,并将他们常用的 Snippets 放在了GitHub ,又引来很多粉丝疯狂的 Star,我也过去观望了一下,其实内容不多,而且也没有什么太多的亮点,我觉得只要你平时注意积累和整理自己的 Snippets,效果一定比从别人那里 Fork 来用要好得多。什么?你还不知道 Snippets 是什么?那你弄清楚了再来看!
其实我平时也不太注意 Sinppets 的积累和整理,所以决定从现在起开始。在积累自己的 Sinppets 前,我先大致看了一下 Xcode 预置的 Sinppets,发现不少我曾经千百次敲的代码 Sinppets 里面都有,所以觉得有比较写成文章记录一下。
现在就按顺序将 Xcode 预置的 Sinppets 过一遍。
Shortcut: typedofBlock
Code:
typedef <#return type#>(^<#block name#>)(<#arguments#>);
Shortcut: inlineBlock
Code:
<#Return Type#>(^<#Block Name#>)(<#Parameter Types#>) = ^(<#Parameters#>) { <#Code#> };
Shortcut: typedef
Code:
typedef <#existing#> <#new#>;
Shortcut: classdef
Code:
class <#class name#> { <#instance variables#> public: <#member functions#> };
Shortcut: templateclass
Code:
template <<#template parameters#>> class <#class name#> { <#instance variables#> public: <#member functions#> };
Shortcut: templatefunction
Code:
template <<#template parameters#>> <#return type#> <#function name#>(<#function parameters#>) { <#statements#> }
Shortcut: namespace
Code:
namespace <#namespace name#> { <#declarations#> }
Shortcut: try
Code:
try { <#statements#> } catch (<#catch parameter#>) { <#statements#> }
Shortcut: using namespace
Code:
using namespace <#namespace name#>
Shortcut:
Code:
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; NSEntityDescription *entity = [NSEntityDescription entityForName:@"<#Entity name#>" inManagedObjectContext:<#context#>]; [fetchRequest setEntity:entity]; NSError *error = nil; NSArray *fetchedObjects = [<#context#> executeFetchRequest:fetchRequest error:&error]; if (fetchedObjects == nil) { <#Error handling code#> } [fetchRequest release];
比较奇怪的是所有 Core Data 相关的 Snippets 都没有提供 Shortcut,有知道的朋友麻烦留言告诉我。
Shortcut:
Code:
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; NSEntityDescription *entity = [NSEntityDescription entityForName:@"<#Entity name#>" inManagedObjectContext:<#context#>]; [fetchRequest setEntity:entity]; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"<#Predicate string#>", <#Predicate arguments#>]; [fetchRequest setPredicate:predicate]; NSError *error = nil; NSArray *fetchedObjects = [<#context#> executeFetchRequest:fetchRequest error:&error]; if (fetchedObjects == nil) { <#Error handling code#> } [fetchRequest release];
Shortcut:
Code:
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; NSEntityDescription *entity = [NSEntityDescription entityForName:@"<#Entity name#>" inManagedObjectContext:<#context#>]; [fetchRequest setEntity:entity]; NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"<#Sort key#>" ascending:YES]; NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil]; [fetchRequest setSortDescriptors:sortDescriptors]; NSError *error = nil; NSArray *fetchedObjects = [<#context#> executeFetchRequest:fetchRequest error:&error]; if (fetchedObjects == nil) { // Handle the error } [fetchRequest release]; [sortDescriptor release]; [sortDescriptors release];
Shortcut:
Code:
- (<#Property type#> *)<#Property name#> { [self willAccessValueForKey:@"<#Property name#>"]; <#Property type#> *tmpValue = [self primitiveValueForKey:@"<#Property name#>"]; [self didAccessValueForKey:@"<#Property name#>"]; return tmpValue; } - (void)set<#Capitalized property name#>:(<#Property type#> *)value { [self willChangeValueForKey:@"<#Property name#>"]; [self setPrimitiveValue:value forKey:@"<#Property name#>"]; [self didChangeValueForKey:@"<#Property name#>"]; }
Shortcut:
Code:
- (BOOL)validate<#Capitalized property name#>:(id *)valueRef error:(NSError **)outError { BOOL validationResult = YES; <#Validation code#> return validationResult; }
Shortcut:
Code:
- (<#Property type#>)<#Property name#> { [self willAccessValueForKey:@"<#Property name#>"]; <#Property type#> *tmpValue = <#Property name#>; [self didAccessValueForKey:@"<#Property name#>"]; return tmpValue; } - (void)set<#Capitalized property name#>:(<#Property type#>)value { [self willChangeValueForKey:@"<#Property name#>"]; <#Property name#> = value; [self didChangeValueForKey:@"<#Property name#>"]; }
Shortcut:
Code:
- (void)add<#Capitalized relationship name#>Object:(<#Relationship destination class#> *)value { NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1]; [self willChangeValueForKey:@"<#Relationship name#>" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects]; [[self primitiveValueForKey:@"<#Relationship name#>"] addObject:value]; [self didChangeValueForKey:@"<#Relationship name#>" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects]; [changedObjects release]; } - (void)remove<#Capitalized relationship name#>Object:(<#Relationship destination class#> *)value { NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1]; [self willChangeValueForKey:@"<#Relationship name#>" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects]; [[self primitiveValueForKey:@"<#Relationship name#>"] removeObject:value]; [self didChangeValueForKey:@"<#Relationship name#>" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects]; [changedObjects release]; } - (void)add<#Capitalized relationship name#>:(NSSet *)value { [self willChangeValueForKey:@"<#Relationship name#>" withSetMutation:NSKeyValueUnionSetMutation usingObjects:value]; [[self primitiveValueForKey:@"<#Relationship name#>"] unionSet:value]; [self didChangeValueForKey:@"<#Relationship name#>" withSetMutation:NSKeyValueUnionSetMutation usingObjects:value]; } - (void)remove<#Capitalized relationship name#>:(NSSet *)value { [self willChangeValueForKey:@"<#Relationship name#>" withSetMutation:NSKeyValueMinusSetMutation usingObjects:value]; [[self primitiveValueForKey:@"<#Relationship name#>"] minusSet:value]; [self didChangeValueForKey:@"<#Relationship name#>" withSetMutation:NSKeyValueMinusSetMutation usingObjects:value]; }
Shortcut: dowhile
Code:
do { <#statements#> } while (<#condition#>);
Shortcut:
Code:
NSUInteger index = [<#index set#> firstIndex]; while (index != NSNotFound) { // Do something with index index = [<#index set#> indexGreaterThanIndex:index]; }
Shortcut:
Code:
NSUInteger index = [<#index set#> lastIndex]; while (index != NSNotFound) { // Do something with index. index = [<#index set#> indexLessThanIndex:index]; }
同样,已上两个也没有提供 Shortcut。
Shortcut: enumdef
Code:
enum <#enumeration name#> { <#enumerator1#> = <#value1#>, <#enumerator2#> = <#value2#> };
Shortcut: for
Code:
for (<#initialization#>; <#condition#>; <#increment#>) { <#statements#> }
Shortcut: dispatch_after
Code:
double delayInSeconds = 2.0; dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC)); dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ <#code to be executed on the main queue after delay#> });
Shortcut: dispatch_once
Code:
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
<#code to be executed once#> });
Shortcut: if
Code:
if (<#condition#>) { <#statements#> }
Shortcut: ifelse
Code:
if (<#condition#>) { <#statements-if-true#> } else { <#statements-if-false#> }
Shortcut: @autoreleasepool
Code:
@autoreleasepool {
<#statements#> }
Shortcut: @catch
Code:
@catch (<#exception#>) { <#handler#> }
Shortcut: @interface-category
Code:
@interface <#class name#> (<#category name#>) @end
Shortcut: @implementation-category
Code:
@implementation <#class#> (<#category name#>) <#methods#> @end
Shortcut: @interface
Code:
@interface <#class name#> : <#superclass#> @end
Shortcut: @interface-extension
Code:
@interface <#class name#> () @end
Shortcut: @implementation
Code:
@implementation <#class#> <#methods#> @end
Shortcut: dealloc
Code:
- (void)dealloc { <#deallocations#> }
Shortcut: forin
Code:
for (<#type *object#> in <#collection#>) { <#statements#> }
Shortcut: @finally
Code:
@finally { <#handler#> }
Shortcut: init
Code:
- (id)init { self = [super init]; if (self) { <#initializations#> } return self; }
Shortcut: observeValueForKeyPath
Code:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if (context == <#context#>) { <#code to be executed upon observing keypath#> } else { [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; } }
Shortcut: keyPathsForValuesAffecting
Code:
+ (NSSet *)keyPathsForValuesAffecting<#Dependent Key#> { return [NSSet setWithObjects:@"<#key1#>", nil]; }
Shortcut: @protocol
Code:
@protocol <#protocol name#> <NSObject> <#methods#> @end
Shortcut: @try
Code:
@try { <#statements#> } @catch (NSException *exception) { <#handler#> } @finally { <#statements#> }
Shortcut: structdef
Code:
struct <#struct name#> { <#instance variables#> };
Shortcut: switch
Code:
switch (<#expression#>) { case <#constant#>: <#statements#> break; default: break; }
Shortcut: test
Code:
- (void) test<#Name#> { <#statements#> }
Shortcut: uniondef
Code:
union <#union name#> { <#instance variables#> };
Shortcut: while
Code:
while (<#condition#>) { <#statements#> }
Shortcut: initWithCoder
Code:
- (id)initWithCoder:(NSCoder *)coder { self = [super initWithCoder:coder]; if (self) { <#initializations#> } return self; }
Shortcut: initWithFrame
Code:
- (id)initWithFrame:(NSRect)frame { self = [super initWithFrame:frame]; if (self) { <#initializations#> } return self; }
复制粘贴真累,上面贴出来这么长,其实很多根本就不用记,比如: for
,typedof
等,可以当成 Xcode 代码补齐功能 , 但有的使用可能需要注意一些条件,如: init
, dealloc
,如果你习惯于一写方法就先写 -(id)
或 -(void)
,那么再打 init
, dealloc
就千呼万唤使不出来了,我就躺枪了,你呢? 还有像 observeValueForKeyPath
和 keyPathsForValuesAffecting
确实不知道,不知道有没有常用 KVO 的将这两个方法自己又加一遍 Snippets?
当然 Snippets 在添加时可以设置如支持的平台(OS X or iOS),语言环境以及使用的位置(Completion Scopes), 用系统预置的 Snippets 就不用管那么多了,反正苹果会将这些设置为最合理的就是了,我们自己添加 Snippets 时就得多考虑一下这些设置的参数了。
Posted by TracyYih - 9月 6 2013
如需转载,请注明: 本文来自 Esoft Mobile
原文 http://esoftmobile.com/2013/09/06/xcode预置sinppets知多少/