Xcode默认只能编辑用户自建的代码片段,而系统自带的代码片段在Xcode中是不支持直接编辑的。但是可以通过以下方法进行编辑修改
- 先找到文件存放位置,Xcode自带代码片段存放位置:
Xcode 9/Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Versions/A/Resources/SystemCodeSnippets.codesnippets
Xcode 10/Applications/Xcode.app/Contents/PlugIns/IDESourceEditor.framework/Versions/A/Resources/SystemCodeSnippets.codesnippets
- 将该文件复制出来将后缀改为plist,打开编辑该文件即可
例:由于加@符Xcode不会有代码提示,可以将@protocol、@interface、@implementation中的@符去掉,这样Xcode自带的代码片段就可以起作用,不用自己写了。
附:(一些常用的Xcode自带OC代码片段)
initialize
+ (void)initialize
{
if (self == [<#ClassName#> class]) {
<#statements#>
}
}
initWithFrame
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
<#statements#>
}
return self;
}
init
- (instancetype)init
{
self = [super init];
if (self) {
<#statements#>
}
return self;
}
forin
for (<#type *object#> in <#collection#>) {
<#statements#>
}
delloc
- (void)dealloc
{
<#statements#>
}
description
- (NSString *)description
{
return [NSString stringWithFormat:@"<#format string#>", <#arguments#>];
}
debugDescription
- (NSString *)debugDescription
{
return [NSString stringWithFormat:@"<%@: %p> <#additional format string#>", [self class], self, <#additional arguments#>];
}
encodeWithCoder
- (void)encodeWithCoder:(NSCoder *)coder
{
[super encodeWithCoder:coder];
<#statements#>
}
initWithCoder
- (instancetype)initWithCoder:(NSCoder *)coder
{
self = [super initWithCoder:coder];
if (self) {
<#statements#>
}
return self;
}
isEqual
- (BOOL)isEqual:(id)other
{
if (other == self) {
return YES;
} else if (![super isEqual:other]) {
return NO;
} else {
return <#comparison expression#>;
}
}
- (NSUInteger)hash
{
return <#hash expression#>;
}
typedefBlock
typedef <#returnType#>(^<#name#>)(<#arguments#>);
typedef
typedef <#existing#> <#new#>;
dispatch_once snippet
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
<#code to be executed once#>
});
dispatch_source timer
dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, <#dispatchQueue#>);
dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, <#intervalInSeconds#> * NSEC_PER_SEC, <#leewayInSeconds#> * NSEC_PER_SEC);
dispatch_source_set_event_handler(timer, ^{
<#code to be executed when timer fires#>
});
dispatch_resume(timer);
dispatch_after snippet
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(<#delayInSeconds#> * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
<#code to be executed after a specified delay#>
});
inlineBlock
<#returnType#>(^<#blockName#>)(<#parameterTypes#>) = ^(<#parameters#>) {
<#statements#>
};
enumdef
typedef enum : NSUInteger {
<#MyEnumValueA#>,
<#MyEnumValueB#>,
<#MyEnumValueC#>,
} <#MyEnum#>;
nsenum
typedef NS_ENUM(NSUInteger, <#MyEnum#>) {
<#MyEnumValueA#>,
<#MyEnumValueB#>,
<#MyEnumValueC#>,
};
nsoptions
typedef NS_OPTIONS(NSUInteger, <#MyEnum#>) {
<#MyEnumValueA#> = 1 << 0,
<#MyEnumValueB#> = 1 << 1,
<#MyEnumValueC#> = 1 << 2,
};
structdef
struct <#struct name#> {
<#struct fields#>
};
if
if (<#condition#>) {
<#statements#>
}
ifelse
if (<#condition#>) {
<#statements#>
} else {
<#statements#>
}
switch
switch (<#expression#>) {
case <#constant#>:
<#statements#>
break;
default:
break;
}
while
while (<#condition#>) {
<#statements#>
}
dowhile
do {
<#statements#>
} while (<#condition#>);
for
for (<#initialization#>; <#condition#>; <#increment#>) {
<#statements#>
}
test
- (void)test<#Name#> {
<#statements#>
}
observeValueForKeyPath
- (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];
}
}
带@符号的
@protocol
@protocol <#protocol name#> <NSObject>
<#methods#>
@end
@interface
@interface <#class name#> : <#superclass#>
@end
@interface-extension
@interface <#class name#> ()
@end
@interface-category
@interface <#class name#> (<#category name#>)
@end
@implementation
@implementation <#class#>
<#methods#>
@end
@implementation-category
@implementation <#class#> (<#category name#>)
<#methods#>
@end
@try
@try {
<#Code that can potentially throw an exception#>
} @catch (NSException *exception) {
<#Handle an exception thrown in the @try block#>
} @finally {
<#Code that gets executed whether or not an exception is thrown#>
}
@autoreleasepool
@autoreleasepool {
<#statements#>
}
@synchronized
@synchronized (<#token#>) {
<#statements#>
}
网友评论