當增加新的屬性上課,我發現自己在Xcode中一遍又一遍地輸入相同的事情:如何在Objective-C中自動添加屬性?
add TYPE *NAME;
(在.H接口)add @property (nonatomic, retain) TYPE *NAME;
(在.H)add @synthesize NAME;
(以.M)add [NAME release];
(在.M的dealloc)
(我在非垃圾收集環境。)
我該如何自動執行此操作?
當增加新的屬性上課,我發現自己在Xcode中一遍又一遍地輸入相同的事情:如何在Objective-C中自動添加屬性?
add TYPE *NAME;
(在.H接口)add @property (nonatomic, retain) TYPE *NAME;
(在.H)add @synthesize NAME;
(以.M)add [NAME release];
(在.M的dealloc)(我在非垃圾收集環境。)
我該如何自動執行此操作?
這聽起來是正確的。 IIRC,Objective-C 2.0文檔說你可能會排除第一步,但否則我不知道任何捷徑。
您可能可以在Xcode中編寫用戶腳本。見http://www.mactech.com/articles/mactech/Vol.23/23.01/2301XCode/index.html。
According to the Developer Documentation在64位運行時,你可以離開了步驟1
你可以看看安德魯·彭氏RMModelObject - 我還沒有使用它,但它充當簡化模型創建一個對象的基類。
我還沒有使用它,但這裏的一些什麼自述強調:
- 無需聲明實例變量,
- 沒有必要寫存取方法,
- 免費NSCopying協議支持(
-copyWithZone:
),- 免費NSCoding協議支持(
-initWithCoder:
,-encodeWithCoder:
),- 免費
-isEqual:
和-hash`執行,- 大多數情況下不需要編寫
-dealloc
。
這裏是另一種解決辦法是我從 this article修改(見the initial article)
在博客版本的變量聲明塊之外尋找變量和太匹配方法名。我已經做了一個粗略的修復,只在第一個'}'之前搜索變量。如果頭文件中有多個接口聲明,這會中斷。
我設置輸出爲「替換文檔Conents」並輸入爲「整個文檔」 ....
#!/usr/bin/python
thisfile = '''%%%{PBXFilePath}%%%'''
code = '''%%%{PBXAllText}%%%'''
selmark = '''%%%{PBXSelection}%%%'''
import re
if thisfile.endswith('.h'):
variableEnd = code.find('\n', code.find('}'))
properties = []
memre = re.compile('\s+(?:IBOutlet)?\s+([^\[email protected]].*? \*?.*?;)')
for match in memre.finditer(code[:variableEnd]):
member = match.group(1)
retain = member.find('*') != -1 and ', retain' or ''
property = '@property (nonatomic%s) %s' % (retain,member)
if code.find(property) == -1:
properties.append(property)
if properties:
print '%s\n\n%s%s%s%s' % (code[:variableEnd],selmark,'\n'.join(properties),selmark,code[variableEnd:])
elif thisfile.endswith('.m'):
headerfile = thisfile.replace('.m','.h')
properties = []
retains = []
propre = re.compile('@property\s\((.*?)\)\s.*?\s\*?(.*?);')
header = open(headerfile).read()
for match in propre.finditer(header):
if match.group(1).find('retain') != -1:
retains.append(match.group(2))
property = '@synthesize %s;' % match.group(2)
if code.find(property) == -1:
properties.append(property)
pindex = code.find('\n', code.find('@implementation'))
if properties and pindex != -1:
output = '%s\n\n%s%s%s' % (code[:pindex],selmark,'\n'.join(properties),selmark)
if retains:
dindex = code.find('\n', code.find('(void)dealloc'))
output += code[pindex:dindex]
retainsstr = '\n\t'.join(['[%s release];' % retain for retain in retains])
output += '\n\t%s' % retainsstr
pindex = dindex
output += code[pindex:]
print output
有凱文·卡拉漢的Accessorizer。從網頁:
Accessorizer基於伊娃類型 選擇適當的 財產符 - 也可以產生明確的訪問器(1.0),自動的...但 Accessorizer確實很多,很多......