论坛首页 移动开发技术论坛

Xcode 自动属性生成器(强力推荐)

浏览 7314 次
精华帖 (0) :: 良好帖 (15) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-09-30  
iOS

一次偶然机会从git上找到,可惜没有添加关注。现在忘记从哪里clone 出来了。

应该是目前最好用的自动补上属性 

 

@property()xx   

@syn

delloc

功能。

 

 

代码 写道
#! /usr/bin/perl -w

# Created by Matt Gallagher on 20/10/08.
# Copyright 2008 Matt Gallagher. All rights reserved.
#
# Permission is given to use this source code file without charge in any
# project, commercial or otherwise, entirely at your risk, with the condition
# that any redistribution (in part or whole) of source code must retain
# this copyright and permission notice. Attribution in compiled projects is
# appreciated but not required.

use strict;

# Get the header file contents from Xcode user scripts
my $headerFileContents = <<'HEADERFILECONTENTS';
%%%{PBXAllText}%%%
HEADERFILECONTENTS

# Get the indices of the selection from Xcode user scripts
my $selectionStartIndex = %%%{PBXSelectionStart}%%%;
my $selectionEndIndex = %%%{PBXSelectionEnd}%%%;

# Get path of the header file
my $implementationFilePath = "%%%{PBXFilePath}%%%";
my $headerFilePath = $implementationFilePath;

# Look for an implemenation file with a ".m" or ".mm" extension
$implementationFilePath =~ s/\.[hm]*$/.m/;
if (!(-e $implementationFilePath))
{
$implementationFilePath =~ s/.m$/.mm/;
}

# Handle subroutine to trime whitespace off both ends of a string
sub trim
{
my $string = shift;
$string =~ s/^\s*(.*?)\s*$/$1/;
return $string;
}

# Get the selection out of the header file
my $selectedText = substr $headerFileContents, $selectionStartIndex, ($selectionEndIndex - $selectionStartIndex);
$selectedText = trim $selectedText;

my $selectedLine;

foreach $selectedLine (split(/\n+/, $selectedText)) {
my $type = "";
my $asterisk = "";
my $name = "";
my $ivarName = "";
my $behavior = "";
my $isPointer = 0;

# Test that the selection is:
# At series of identifiers (the type name and access specifiers)
# Possibly an asterisk
# Another identifier (the variable name)
# A semi-colon
if (length($selectedLine) && ($selectedLine =~ /([_A-Za-z][_A-Za-z0-9]*\s*)+([\s\*]+)([_A-Za-z][_A-Za-z0-9]*);/))
{
$type = $1;
$type = trim $type;
$asterisk = $2;
$asterisk = trim $asterisk;
$ivarName = $3;
if ($ivarName =~ /^_(.*)/) {
$name = $1;
}
else {
$name = $ivarName;
}
$behavior = "";
if (defined($asterisk) && length($asterisk) == 1)
{
$isPointer = 1;
if ($type eq "NSArray" || $type eq "NSString" || $type eq "NSDictionary" || $type eq "NSSet") {
$behavior = "(nonatomic, copy) ";
}
else
{
$behavior = "(nonatomic, retain) ";
}
}
else
{
$isPointer = 0;
$behavior = "(nonatomic, assign) ";
$asterisk = "";
}
}
else
{
next;
}

# Find the closing brace (end of the class variables section)
my $remainderOfHeader = substr $headerFileContents, $selectionEndIndex;
my $indexAfterClosingBrace = $selectionEndIndex + index($remainderOfHeader, "\n}\n") + 3;
if ($indexAfterClosingBrace == -1)
{
exit 1;
}

# Determine if we need to add a newline in front of the property declaration
my $leadingNewline = "\n";
if (substr($headerFileContents, $indexAfterClosingBrace, 1) eq "\n")
{
$indexAfterClosingBrace += 1;
$leadingNewline = "";
}

# Determine if we need to add a newline after the property declaration
my $trailingNewline = "\n";
if (substr($headerFileContents, $indexAfterClosingBrace, 9) eq "\@property")
{
$trailingNewline = "";
}

# Create and insert the propert declaration
my $propertyDeclaration = $leadingNewline . "\@property " . $behavior . $type . " " . $asterisk . $name . ";\n" . $trailingNewline;
substr($headerFileContents, $indexAfterClosingBrace, 0) = $propertyDeclaration;

my $replaceFileContentsScript = <<'REPLACEFILESCRIPT';
on run argv
set fileAlias to POSIX file (item 1 of argv)
set newDocText to (item 2 of argv)
tell application "Xcode"
set doc to open fileAlias
set text of doc to newDocText
end tell
end run
REPLACEFILESCRIPT

# Use Applescript to replace the contents of the header file
# (I could have used the "Output" of the Xcode user script instead)
system 'osascript', '-e', $replaceFileContentsScript, $headerFilePath, $headerFileContents;

# Stop now if the implementation file can't be found
if (!(-e $implementationFilePath))
{
exit 1;
}

my $getFileContentsScript = <<'GETFILESCRIPT';
on run argv
set fileAlias to POSIX file (item 1 of argv)
tell application "Xcode"
set doc to open fileAlias
set docText to text of doc
end tell
return docText
end run
GETFILESCRIPT

# Get the contents of the implmentation file
open(SCRIPTFILE, '-|') || exec 'osascript', '-e', $getFileContentsScript, $implementationFilePath;
my $implementationFileContents = do {local $/; <SCRIPTFILE>};
close(SCRIPTFILE);

# Look for the class implementation statement
if (length($implementationFileContents) && ($implementationFileContents =~ /(\@implementation [_A-Za-z][_A-Za-z0-9]*\n)/))
{
my $matchString = $1;
my $indexAfterMatch = index($implementationFileContents, $matchString) + length($matchString);

# Determine if we want a newline before the synthesize statement
$leadingNewline = "\n";
if (substr($implementationFileContents, $indexAfterMatch, 1) eq "\n")
{
$indexAfterMatch += 1;
$leadingNewline = "";
}

# Determine if we want a newline after the synthesize statement
$trailingNewline = "\n";
if (substr($implementationFileContents, $indexAfterMatch, 11) eq "\@synthesize")
{
$trailingNewline = "";
}

# Create and insert the synthesize statement
my $synthesizeStatement;
if ($ivarName ne $name) {
$synthesizeStatement = $leadingNewline . "\@synthesize " . $name . " = " . $ivarName . ";\n" . $trailingNewline;
}
else {
$synthesizeStatement = $leadingNewline . "\@synthesize " . $name . ";\n" . $trailingNewline;
}
substr($implementationFileContents, $indexAfterMatch, 0) = $synthesizeStatement;

if ($isPointer) {
if ($implementationFileContents !~ s#(\(void\)\s*dealloc\s*\{\s*\n)(\s*)#$1$2\[$ivarName release\], $ivarName = nil;\n$2#s) {
$implementationFileContents =~ s#(\@end)#\n- (void)dealloc {\n\t[$ivarName release], $ivarName = nil;\n\t[super dealloc];\n}\n$1#s;
}
}

# Use Applescript to replace the contents of the implementation file in Xcode
system 'osascript', '-e', $replaceFileContentsScript, $implementationFilePath, $implementationFileContents;
}
}
exit 0;

 

配置

 

1.添加到自定义脚本中。

2.INPUT: ENTIRE DOC
3.DIRECTORY:HOME
4.OUTPUT:DISCARD
5.ERRORS:IGNORE

 

使用:

选中需要生成属性的内容,运行脚本。

enjoy!

   发表时间:2009-10-01  
这个还挺有趣的!
0 请登录后投票
   发表时间:2009-10-04   最后修改:2009-10-04
snippet功能还属textmate最强
有个iphone tmbundle的github
0 请登录后投票
   发表时间:2009-10-04  
gakaki 写道
snippet功能还属textmate最强
有个iphone tmbundle的github


找到了。看看好用否。
0 请登录后投票
   发表时间:2009-10-08  
gakaki 写道
snippet功能还属textmate最强
有个iphone tmbundle的github


完全没这个功能。
这个bundle 只提供了对 iPhone class 的高亮 和代码提示。
0 请登录后投票
   发表时间:2009-10-09  
我是说你可以自己编写
0 请登录后投票
   发表时间:2009-10-10  
gakaki 写道
我是说你可以自己编写


对你回复真无语。
我发帖子是干嘛的?

0 请登录后投票
论坛首页 移动开发技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics