Xcode script for generating/synthesizing properties

前端 未结 8 1888
南旧
南旧 2020-12-16 03:03

Does anybody have an Xcode script for generating @property and @synthsize directives for instance variables in a class?

8条回答
  •  忘掉有多难
    2020-12-16 03:53

    Here's one I wrote up yesterday to do the @property directives before coming across this question a few hours later. It's a simple text filter and would be trivial to extend it to @synthesize directives (add an appropriate when clause to the case statement and make appropriate additions to the when block_end condition), and not much more work to extend it to handle multiple occurrences of @interface/@implementation in one file (by tracking their names --- it can be done through regexp captures, as everything else is in the script):

    #! /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 run this using "no input" and "replace document contents" as the options for I/O in the User Scripts editor.

提交回复
热议问题