[转帖] Xcode 自动属性生成器(强力推荐)_Android, Python及开发编程讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  Android, Python及开发编程讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 2878 | 回复: 0   主题: [转帖] Xcode 自动属性生成器(强力推荐)        下一篇 
zhang.chen
注册用户
等级:少校
经验:1145
发帖:69
精华:1
注册:2013-10-31
状态:离线
发送短消息息给zhang.chen 加好友    发送短消息息给zhang.chen 发消息
发表于: IP:您无权察看 2013-11-1 15:30:51 | [全部帖] [楼主帖] 楼主

一次偶然机会从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;

复制代码




赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论