当添加新的性能上课,我发现自己的打字同样的事情过于模式:

  1. add TYPE *NAME; (。h口)
  2. add @property (nonatomic, retain) TYPE *NAME; (。h)
  3. add @synthesize NAME; (。m)
  4. add [NAME release]; (。m dealloc)

(我在一个非垃圾收集环境。)

我怎样才能做到这一自动?

有帮助吗?

解决方案

这听起来不错。 IIRC,Objective-C 2.0文档说你可以省略步骤#1,但我不知道有任何捷径。

您可以在Xcode中编写用户脚本。请参阅 http://www.mactech.com/articles /mactech/Vol.23/23.01/2301XCode/index.html

其他提示

根据64位开发人员文档运行时你可以省略第1步。

你可以来看看安德鲁*庞的 RMModelObject -我没有用过它,但是它作为一个目的基类,从而简化模型的创造。

我没有用过,但这是一些什么强调在自述:

  • 没有必要声明实例的变量,
  • 不需要写访问方式,
  • 免费NSCopying议定书》的支持(-copyWithZone:),
  • 免费NSCoding议定书》的支持(-initWithCoder:, -encodeWithCoder:),
  • 免费的 -isEqual: 和哈希`的执行,
  • 不需要写 -dealloc 在大多数情况下。

这是我修改过的另一个解决方案 这篇文章(另见最初的文章

博客中的版本是在变量声明块之外搜索变量,并且也匹配方法名称。我做了一个粗略的修复,只在第一个'}'之前搜索变量。如果头文件中有多个接口声明,则会中断。

我将输出设置为“Replace Document 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+([^\-+@].*? \*?.*?;)')
    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

Kevin Callahan的配置工具。从网页:

  

Accessorizer选择合适的   基于ivar类型的属性说明符    - 并且还可以自动生成显式访问器(1.0)......但是   Accessorizer做得更多,更多......

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top