有沒有人有Xcode腳本來爲類中的實例變量生成@property和@synthsize指令?用於生成/合成屬性的Xcode腳本
回答
這是我基於我發現很久以前發現的一個,用Python重寫並改進爲一次可以生成多個屬性等等。
它將使用(複製)作爲屬性爲所有選定的實例變量生成屬性。
在文件中還有一些邊界情況下有多個@interface或@implementation,還有一些邊界情況有異常標識符或星號位置(如* const),但它應該覆蓋大多數典型的編碼風格。如果您修復了這些情況,請隨時編輯/發佈修改。
#!/usr/bin/python
# Takes a header file with one or more instance variables selected
# and creates properties and synthesize directives for the selected properties.
# Accepts google-style instance variables with a tailing underscore and
# creates an appropriately named property without underscore.
# Entire Document
# Home Directory
# Discard Output
# Display in Alert
import os
import re
import subprocess
# AppleScripts for altering contents of files via Xcode
setFileContentsScript = """\
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 \
"""
getFileContentsScript = """\
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 \
"""
# Get variables from Xcode
headerFileText = """%%%{PBXAllText}%%%"""
selectionStartIndex = %%%{PBXSelectionStart}%%%
selectionEndIndex = %%%{PBXSelectionEnd}%%%
selectedText = headerFileText[selectionStartIndex:selectionEndIndex]
headerFilePath = """%%%{PBXFilePath}%%%"""
# Look for an implementation file with .m or .mm extension
implementationFilePath = headerFilePath[:-1] + "m"
if not os.path.exists(implementationFilePath):
implementationFilePath += "m"
instanceVariablesRegex = re.compile(
"""^\s*((?:(?:\w+)\s+)*(?:(?:\w+)))""" + # Identifier(s)
"""([*]?)\\s*""" + # An optional asterisk
"""(\\w+?)(_?);""", # The variable name
re.M)
# Now for each instance variable in the selected section
properties = ""
synthesizes = ""
for lineMatch in instanceVariablesRegex.findall(selectedText):
types = " ".join(lineMatch[0].split()) # Clean up consequtive whitespace
asterisk = lineMatch[1]
variableName = lineMatch[2]
trailingUnderscore = lineMatch[3]
pointerPropertyAttributes = "(copy) " # Attributes if variable is pointer
if not asterisk:
pointerPropertyAttributes = ""
newProperty = "@property %s%s %s%s;\n" % (pointerPropertyAttributes,
types,
asterisk,
variableName)
# If there's a trailing underscore, we need to let the synthesize
# know which backing variable it's using
newSynthesize = "@synthesize %s%s;\n" % (variableName,
trailingUnderscore and
" = %s_" % variableName)
properties += newProperty
synthesizes += newSynthesize
# Check to make sure at least 1 properties was found to generate
if not properties:
os.sys.stderr.writelines("No properties found to generate")
exit(-1)
# We want to insert the new properties either immediately after the last
# existing property or at the end of the instance variable section
findLastPropertyRegex = re.compile("^@interface.*?{.*?}.*?\\n" +
"(?:.*^\\s*@property.*?\\n)?", re.M | re.S)
headerInsertIndex = findLastPropertyRegex.search(headerFileText).end()
# Add new lines on either side if this is the only property in the file
addedNewLine = "\n"
if re.search("^\s*@property", headerFileText, re.M):
# Not the only property, don't add
addedNewLine = ""
newHeaderFileText = "%s%s%s%s" % (headerFileText[:headerInsertIndex],
addedNewLine,
properties,
headerFileText[headerInsertIndex:])
subprocess.call(["osascript",
"-e",
setFileContentsScript,
headerFilePath,
newHeaderFileText])
if not os.path.exists(implementationFilePath):
os.sys.stdout.writelines("No implementation file found")
exit(0)
implementationFileText = subprocess.Popen(
["osascript",
"-e",
getFileContentsScript,
implementationFilePath],
stdout=subprocess.PIPE).communicate()[0]
# We want to insert the synthesizes either immediately after the last existing
# @synthesize or after the @implementation directive
lastSynthesizeRegex = re.compile("^\\s*@implementation.*?\\n" +
"(?:.*^\\s*@synthesize.*?\\n)?", re.M | re.S)
implementationInsertIndex = \
lastSynthesizeRegex.search(implementationFileText).end()
# Add new lines on either side if this is the only synthesize in the file
addedNewLine = "\n"
if re.search("^\s*@synthesize", implementationFileText, re.M):
# Not the only synthesize, don't add
addedNewLine = ""
newImplementationFileText = "%s%s%s%s" % \
(implementationFileText[:implementationInsertIndex],
addedNewLine,
synthesizes,
implementationFileText[implementationInsertIndex:])
subprocess.call(["osascript",
"-e",
setFileContentsScript,
implementationFilePath,
newImplementationFileText])
# Switch Xcode back to header file
subprocess.Popen(["osascript",
"-e",
getFileContentsScript,
headerFilePath],
stdout=subprocess.PIPE).communicate()
呃,是不是「合成」,有兩個e's?我假設這個腳本不能正確工作,拼寫爲「synthsize」。 – n8gray 2009-10-14 00:06:13
是的,謝謝你指出。 – 2009-10-14 16:06:42
下面是我昨天寫過的一篇文章,在幾個小時後遇到此問題之前執行@property指令。這是一個簡單的文本過濾器將是微不足道的把它擴大到@synthesize指令(添加適當的when
條款的case
語句,並作出適當的增加的when block_end
條件),並沒有更多的工作來擴展它來處理多次出現@在一個文件中的接口/ @實現(通過跟蹤他們的名字---它可以通過捕獲正則表達式來完成,因爲一切是在腳本中):
#! /usr/bin/ruby
# -------------- Basic Definitions -----------------------------
doc = "%%%{PBXFilePath}%%%"
# regular expressions
search_exp = /[[:space:]]*([[a-zA-Z0-9]]*)[[:space:]]\*([a-zA-Z0-9]*)/
interface_start = /@interface/
block_end = /^\}/
#initializing variables
properties_list = []
properties_string = ""
reading_interface = 0
#---------------- Start Processing -----------------------------
file = File.open(doc, "r").readlines
file.each do |line|
# capture the regular expression matches only in the
# interface declaration and print out the matching
# property declarations
case line
# start capturing
when interface_start
reading_interface = 1
puts line
# capture and keep in properties_list
when search_exp
if (reading_interface == 1) then
data = Regexp.last_match
properties_list << data
end
puts line
# unpack properties_list and print out the property
# declarations
when block_end
if (reading_interface == 1) then
reading_interface = 0
properties_list.each do |pair|
properties_string << "@property (readwrite, copy) #{pair[0].lstrip};\n"
end
puts line
puts "\n" + properties_string
end
else puts line
end
end
我運行這個使用「無輸入」和「替換文檔內容「作爲用戶腳本編輯器中I/O的選項。
這裏是我目前使用的userscript - 它一次在一個實例變量上工作。它試圖使用正確的保留機制(簡單類型不被保留),並且它還在實現文件中創建了@synthesize語句 - 目前它還沒有爲你創建dealloc語句。
#! /usr/bin/perl -w
#Input: Selection
#Directory: Selection
#Output: Display in Alert
#Errors: Display in Alert
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);
#my $otherText = substr $headerFileContents, $selectionStartIndex;
#my $pulledText = "";
#if (length($otherText) && $otherText =~ /.*$(^.*;).*/)
#{
# $pulledText = $1;
#}
#
#
#print $pulledText;
$selectedText = trim $selectedText;
my $type = "";
my $asterisk = "";
my $name = "";
my $behavior = "";
my $iboutlet = "";
# 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($selectedText) && ($selectedText =~ /([_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;
$name = $3;
$behavior = "";
if (defined($asterisk) && length($asterisk) == 1)
{
$behavior = "(nonatomic, retain) ";
}
else
{
$behavior = "(nonatomic) ";
$asterisk = "";
}
}
else
{
print "Bailing, error in Regex";
exit 1;
}
# special case, see if we need to keep around an IBOUTLET declaration.
if (length($selectedText) && ($selectedText =~ /IBOutlet/))
{
$iboutlet = "IBOutlet ";
}
# 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 proper declaration
my $propertyDeclaration = $leadingNewline . "\@property " . $behavior . $iboutlet . $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 = $leadingNewline . "\@synthesize " . $name . ";\n" . $trailingNewline;
substr($implementationFileContents, $indexAfterMatch, 0) = $synthesizeStatement;
# Use Applescript to replace the contents of the implementation file in Xcode
system 'osascript', '-e', $replaceFileContentsScript, $implementationFilePath, $implementationFileContents;
}
exit 0;
訪問器http://www.kevincallahan.org/software/accessorizer.html做這些東西和更多。它還處理自定義前綴和後綴(後綴)。如果你想要谷歌的下劃線,你知道了。如果您想更改它,請即時更改它 - 無需編輯腳本。此外,還有一個默認表,您可以根據傳入的伊娃類型(複製,保留,只讀,賦值等)來定義默認屬性說明符。它執行IBOutlet檢測並自動插入IBOutlet關鍵字,爲-viewDidUnload刪除視圖,執行幾種dealloc風格。它還爲集合編寫了所有這些毛茸茸的訪問器(NSMutableArray和NSSet)。它可以實現密鑰存檔,各種鎖定方法,它可以對你的屬性進行排序併合成塊,編寫KVO代碼,Singleton代碼,轉換爲選擇器,生成HeaderDoc標籤,NSLog()等......它還有一個靈活的樣式選項卡把括號對換行還是不行,對空間,自定義參數名稱等大部分事情都是通過服務來處理,所以你只需選擇你的伊娃塊,打一兩個按鍵,即可大功告成。如果最小化Accessorizer到碼頭,它的接口不親臨前線,讓你保持專注在Xcode或支持服務的任何其他編輯器。當然,Accessorizer也寫出明確的訪問器(如Objective-C 1.0中的),並允許你重寫屬性 - 所有這些都通過一個簡單的開關切換來完成。您甚至可以根據傳入的類型自定義覆蓋。觀看視頻以查看它的實際使用情況。
這是Xcode的3.2.4產生一個python腳本;接口屬性,實現合成和dealloc's。 要安裝,請複製此腳本,轉到Xcode腳本菜單(從倒數第二個) 「編輯用戶腳本...」 在代碼中添加它,創建一個新的腳本名稱,並粘貼下面的python腳本。
要使用只需選擇@interface下的變量,然後調用此腳本。 然後它將添加所有的@屬性,在實現 和所有@synthesize和dealloc的。 它不會將IBOutlet添加到任何標籤或按鈕,因爲它不知道這一點,但是這個 很容易手動添加。
縮進下面的腳本是至關重要的,所以不要改變它。
#!/usr/bin/python
# Takes a header file with one or more instance variables selected
# and creates properties and synthesize directives for the selected properties.
# Accepts google-style instance variables with a tailing underscore and
# creates an appropriately named property without underscore.
# Xcode script options should be as follows:
# Entire Document
# Home Directory
# Discard Output
# Display in Alert
import os
import re
import subprocess
# AppleScripts for altering contents of files via Xcode
setFileContentsScript = """\
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 \
"""
getFileContentsScript = """\
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 \
"""
# Get variables from Xcode
headerFileText = """%%%{PBXAllText}%%%"""
selectionStartIndex = %%%{PBXSelectionStart}%%%
selectionEndIndex = %%%{PBXSelectionEnd}%%%
selectedText = headerFileText[selectionStartIndex:selectionEndIndex]
headerFilePath = """%%%{PBXFilePath}%%%"""
# Look for an implementation file with .m or .mm extension
implementationFilePath = headerFilePath[:-1] + "m"
if not os.path.exists(implementationFilePath):
implementationFilePath += "m"
instanceVariablesRegex = re.compile(
"""^\s*((?:(?:\\b\w+\\b)\s+)*(?:(?:\\b\\w+\\b)))\\s*""" + # Identifier(s)
"""([*]?)\\s*""" + # An optional asterisk
"""(\\b\\w+?)(_?\\b);""", # The variable name
re.M)
# Now for each instance variable in the selected section
properties = ""
synthesizes = ""
deallocs = ""
for lineMatch in instanceVariablesRegex.findall(selectedText):
types = " ".join(lineMatch[0].split()) # Clean up consequtive whitespace
asterisk = lineMatch[1]
variableName = lineMatch[2]
trailingUnderscore = lineMatch[3]
pointerPropertyAttributes = "(nonatomic, retain) " # Attributes if variable is pointer
if not asterisk:
pointerPropertyAttributes = "(nonatomic, assign) "
newProperty = "@property %s%s %s%s;\n" % (pointerPropertyAttributes,
types,
asterisk,
variableName)
# If there's a trailing underscore, we need to let the synthesize
# know which backing variable it's using
newSynthesize = "@synthesize %s%s;\n" % (variableName,
trailingUnderscore and
" = %s_" % variableName)
# only do the objects
if asterisk:
newDealloc = " [%s%s release];\n" % (variableName,
trailingUnderscore and
" = %s_" % variableName)
properties += newProperty
synthesizes += newSynthesize
# only add if it's an object
if asterisk:
deallocs += newDealloc
# Check to make sure at least 1 properties was found to generate
if not properties:
os.sys.stderr.writelines("No properties found to generate")
exit(-1)
# We want to insert the new properties either immediately after the last
# existing property or at the end of the instance variable section
findLastPropertyRegex = re.compile("^@interface.*?{.*?}.*?\\n" +
"(?:.*^\\s*@property.*?\\n)?", re.M | re.S)
headerInsertIndex = findLastPropertyRegex.search(headerFileText).end()
# Add new lines on either side if this is the only property in the file
addedNewLine = "\n"
if re.search("^\s*@property", headerFileText, re.M):
# Not the only property, don't add
addedNewLine = ""
newHeaderFileText = "%s%s%s%s" % (headerFileText[:headerInsertIndex],
addedNewLine,
properties,
headerFileText[headerInsertIndex:])
subprocess.call(["osascript",
"-e",
setFileContentsScript,
headerFilePath,
newHeaderFileText])
if not os.path.exists(implementationFilePath):
os.sys.stdout.writelines("No implementation file found")
exit(0)
implementationFileText = subprocess.Popen(
["osascript",
"-e",
getFileContentsScript,
implementationFilePath],
stdout=subprocess.PIPE).communicate()[0]
# We want to insert the synthesizes either immediately after the last existing
# @synthesize or after the @implementation directive
lastSynthesizeRegex = re.compile("^\\s*@implementation.*?\\n" +
"(?:.*^\\s*@synthesize.*?\\n)?", re.M | re.S)
implementationInsertIndex = \
lastSynthesizeRegex.search(implementationFileText).end()
# Add new lines on either side if this is the only synthsize in the file
addedNewLine = "\n"
if re.search("^\s*@synthesize", implementationFileText, re.M):
# Not the only synthesize, don't add
addedNewLine = ""
newImplementationFileText = "%s%s%s%s" % \
(implementationFileText[:implementationInsertIndex],
addedNewLine,
synthesizes,
implementationFileText[implementationInsertIndex:])
subprocess.call(["osascript",
"-e",
setFileContentsScript,
implementationFilePath,
newImplementationFileText])
implementationFileText = subprocess.Popen(
["osascript",
"-e",
getFileContentsScript,
implementationFilePath],
stdout=subprocess.PIPE).communicate()[0]
# We want to insert the deallocs either immediately after the last existing
# [* release] or after the [super dealloc]
lastDeallocRegex = re.compile("^\\s+\[super dealloc\];?\\n" +
"(?:.*^\\s+\[\w release\];?\\n)?", re.M | re.S)
deallocInsertIndex = \
lastDeallocRegex.search(implementationFileText).end()
addedNewDeallocLine = "\n"
if re.search("^\s*\[\w release\];?", implementationFileText, re.M):
# Not the only dealloc, don't add
addedNewDeallocLine = ""
newImplementationFileText = "%s%s%s%s" % \
(implementationFileText[:deallocInsertIndex],
addedNewDeallocLine,
deallocs,
implementationFileText[deallocInsertIndex:])
subprocess.call(["osascript",
"-e",
setFileContentsScript,
implementationFilePath,
newImplementationFileText])
# Switch Xcode back to header file
subprocess.Popen(["osascript",
"-e",
getFileContentsScript,
headerFilePath],
stdout=subprocess.PIPE).communicate()
哇,這裏有很多瘋狂的腳本。
作爲的Xcode 4.4(也許之前)...您IVAR
旨意進行自動合成。例如..
@property (assign) BOOL automatically;
@property (strong) NSArray *believeDat;
可以 「accessored」 通過
self.automatically = YES;
和編輯實例變量直接通過自動生成帶領導的下劃線就像..
_believeDat = @["thank you, jesus", @"mary poopins"];
否@synthesize
必要。
至於快速簡單地輸入這樣的@property
...將下列內容一次拖入「代碼片段」庫中,然後您可以指定鍵盤快捷方式以插入這些跳出點以輸入屬性更快。我使用的對象RRR和原語AAA ..但是那只是我..
@property (nonatomic, assign) <#type#> <#name#>;
@property (nonatomic, retain) <#type#> *<#name#>;
最後但並非最不重要的,有的可能說我瘋了 ..但我將下列宏投入我的.pch
以進一步加速,澄清並給這個過程帶來歡迎的簡潔性..所有常見的宏免責聲明都適用...
#define RONLY readonly
#define RDWRT readwrite
#define NATOM nonatomic
#define STRNG strong
#define ASS assign
#define CP copy
#define SET setter
#define GET getter
具有相似結構的#define
S表示蘋果類(#define NSA NSArray \ #define NSS NSString
)一起,這使得事情更容易閱讀,並更快地進入(對我),看起來像......
@property (NATOM, STRNG) NSA* fonts;
@property (NATOM, STRNG) NSS* cachedPath;
- 1. 如何在Xcode 5中禁用屬性自動合成屬性?
- 2. 腳本生成Xcode空項目
- 3. 生成腳本
- 4. 生成XML屬性
- 5. 屬性生成NSManagedObject
- 6. 用bean屬性生成preparedstatement
- 7. 關於「腳本生成」的新手
- 8. 生成等效於Transact SQL的腳本
- 9. 用於生成其他腳本的腳本
- 10. 用於爲表格生成腳本的SQL服務器腳本
- 11. 如何將Xcode Bot集成號添加到生成腳本中
- 12. 生成bash腳本
- 13. slideToggle()腳本生成
- 14. 生成腳本[ORACLE]
- 15. 生成SQL腳本
- 16. 生成SQLite腳本
- 17. GMF腳本生成
- 18. 使用GUI生成腳本
- 19. 基於數據屬性生成鏈接
- 20. cxf wsdl2java生成'protected'屬性,如何生成私有屬性?
- 21. Jade中生成的屬性
- 22. 用於從Xcode中自動生成headerdoc註釋的html輸出的腳本
- 23. 如何使用AutoFixture爲屬於類的屬性生成數據?
- 24. PHP腳本與命名空間生成XML和屬性
- 25. 預生成腳本路徑和預生成腳本參數
- 26. 腳本生成的OpenSSL
- 27. 如何使用obj2xml爲xsd2ruby生成的ruby類生成屬性?
- 28. @合成屬性和KVC
- 29. 屬性和可可合成
- 30. VBA集合成員屬性
感謝您的建議 - 我會看看。 – 2010-01-04 17:45:11