Processing Method, Device, Electronic Device and Storage Medium for Swift Code

By obtaining and processing data templates and syntax dictionaries of Swift code files in application engineering projects and generating and replacing target Swift code, the problems of inefficient processing of general code and type mismatch are solved, and the stability and development efficiency of the application are improved.

CN114416050BActive Publication Date: 2025-06-24BEIJING 58 INFORMATION TTECH CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202111631179.7
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-12-28
Publication Date
2025-06-24
Estimated Expiration
2041-12-28

AI Technical Summary

Technical Problem

During the development and maintenance of application engineering projects, the common code processing efficiency in the source code file is low, resulting in increased development and maintenance costs, and it is prone to type mismatch problems that lead to application crashes.

Method used

By obtaining the Swift code file in the initial application project file, obtaining the data template and the data model corresponding to the general Swift code, including several syntax dictionaries, generating the target Swift code and replacing the original general Swift code, the automatic processing of the general Swift code in the application project file is achieved.

Benefits of technology

It effectively reduces the workload of code writing, avoids the type parsing errors caused by hand-written code by developers, and improves the stability and development efficiency of the application.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114416050B_ABST
    Figure CN114416050B_ABST
Patent Text Reader

Abstract

An embodiment of the present invention provides a method, device, electronic device, and storage medium for processing Swift code. The method includes: during code compilation, obtaining a syntax dictionary corresponding to common Swift code from the Swift code files of the application project file, and then converting the common Swift code into specific template code according to a data template, so as to implement the replacement of the common Swift code in the application project file, effectively reducing the workload of code writing, and being able to avoid the type parsing error problem caused by developers writing code manually, ensuring the stability of the application program operation.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of code processing, and in particular, to a method for processing Swift code, a device for processing Swift code, an electronic device, and a computer-readable storage medium. Background Art

[0002] During the development and maintenance of an application project, as the project iterates and functions increase, the source code data corresponding to the project will increase to a large volume over time. Among them, for the general code involved in the project, such as the source code related to model initialization, list model initialization, etc., developers still need to write all of them manually. The work has a high degree of repetition, and during the manual writing process, the application may crash easily due to problems such as type mismatch, which further increases the cost of source code maintenance. Summary of the Invention

[0003] Embodiments of the present invention provide a method, a device, an electronic device, and a computer-readable storage medium for processing Swift code, so as to solve or partially solve the problem in the related art that the general code in the source code file cannot be processed, resulting in low efficiency in source code development and maintenance.

[0004] Embodiments of the present invention disclose a method for processing Swift code, including:

[0005] Obtain an initial application project file, where the initial application project file includes a Swift code file, and the Swift code file includes general Swift code;

[0006] In response to the compilation of the initial application project file, obtain a data template for the Swift code file, and obtain a data model corresponding to the general Swift code from the Swift code file, where the data model includes several syntax dictionaries corresponding to the general Swift code;

[0007] Generate target Swift code for the general Swift code according to the data template and the several syntax dictionaries;

[0008] Replace the general Swift code in the Swift code file with the target Swift code to generate a target application project file.

[0009] Optionally, the obtaining the data model corresponding to the general Swift code from the Swift code file includes:

[0010] Obtain the file identifier of the Swift code file and the code conversion function for the Swift code file;

[0011] Pass the file identifier into the code conversion function, extract the common Swift code in the Swift code file, and convert the common Swift code into a data model corresponding to the Swift code file.

[0012] Optionally, the converting the common Swift code into a data model corresponding to the Swift code file includes:

[0013] Extract data keys and data values corresponding to the data keys from the common Swift code;

[0014] Use each of the data keys and the corresponding data values to construct a data model corresponding to the Swift code file.

[0015] Optionally, the generating target Swift code for the common Swift code according to the data template and the several syntax dictionaries includes:

[0016] If the data model includes a type inheritance relationship and it is determined that the data model is a data structure to be processed, then generate target Swift code for the common Swift code according to the data template and the several syntax dictionaries.

[0017] Optionally, the generating target Swift code for the common Swift code according to the data template and the several syntax dictionaries includes:

[0018] Traverse the data model to obtain variable keys associated with variables;

[0019] Extract the syntax dictionary corresponding to the variable key from the data model, and the syntax dictionary includes variable information corresponding to the variable key;

[0020] Add the variable information of each syntax dictionary to the data template to generate target Swift code for the common Swift code.

[0021] Optionally, the variable information includes at least a field type and a field name, and the adding the variable information of each syntax dictionary to the data template to generate target Swift code for the common Swift code includes:

[0022] Add the field type and field name of each syntax dictionary to the data template to generate target Swift code for the common Swift code.

[0023] Optionally, it further includes:

[0024] Compile the target application project file to generate a machine language file corresponding to the initial application project file.

[0025] An embodiment of the present invention also discloses a processing device for Swift code, including:

[0026] A project file acquisition module, configured to acquire an initial application project file, where the initial application project file includes a Swift code file, and the Swift code file includes general Swift code;

[0027] A data acquisition module, configured to, in response to the compilation of the initial application project file, acquire a data template for the Swift code file, and acquire a data model corresponding to the general Swift code from the Swift code file, where the data model includes several syntax dictionaries corresponding to the general Swift code;

[0028] A code generation module, configured to generate target Swift code for the general Swift code according to the data template and the several syntax dictionaries;

[0029] A project file generation module, configured to replace the general Swift code in the Swift code file with the target Swift code to generate a target application project file.

[0030] Optionally, the data acquisition module includes:

[0031] A function acquisition sub-module, configured to acquire a file identifier of the Swift code file and a code conversion function for the Swift code file;

[0032] A data model generation sub-module, configured to pass the file identifier into the code conversion function, extract the general Swift code in the Swift code file, and convert the general Swift code into a data model corresponding to the Swift code file.

[0033] Optionally, the data model generation sub-module is specifically configured to:

[0034] Extract data keys and data values corresponding to the data keys from the general Swift code;

[0035] Construct a data model corresponding to the Swift code file by using each of the data keys and the corresponding data values.

[0036] Optionally, the code generation module is specifically configured to:

[0037] If the data model includes a type inheritance relationship and it is determined that the data model is a data structure to be processed, then target Swift code for the general Swift code is generated according to the data template and the several syntax dictionaries.

[0038] Optionally, the code generation module includes:

[0039] A variable Key acquisition sub-module, configured to traverse the data model to obtain a variable Key associated with a variable;

[0040] A syntax dictionary extraction sub-module, configured to extract a syntax dictionary corresponding to the variable Key from the data model, where the syntax dictionary includes variable information corresponding to the variable Key;

[0041] A code generation sub-module, configured to add the variable information of each syntax dictionary to the data template to generate target Swift code for the general Swift code.

[0042] Optionally, the variable information includes at least a field type and a field name, and the code generation sub-module is specifically configured to:

[0043] Add the field type and field name of each syntax dictionary to the data template to generate target Swift code for the general Swift code.

[0044] Optionally, it further includes:

[0045] A compilation module, configured to compile the target application project file to generate a machine language file corresponding to the initial application project file.

[0046] An embodiment of the present invention also discloses an electronic device, including a processor, a communication interface, a memory, and a communication bus. Among them, the processor, the communication interface, and the memory complete communication with each other through the communication bus;

[0047] The memory is used to store a computer program;

[0048] When the processor is configured to execute the program stored in the memory, the method described in the embodiment of the present invention is implemented.

[0049] An embodiment of the present invention also discloses a computer-readable storage medium, on which instructions are stored. When executed by one or more processors, the instructions cause the processors to execute the method described in the embodiment of the present invention.

[0050] The embodiments of the present invention include the following advantages:

[0051] In an embodiment of the present invention, during the compilation of an application engineering file, a Swift code file can be obtained from the application engineering file, and during the compilation process, a data template for the Swift code file can be obtained, and a data model corresponding to the general Swift code can be obtained from the Swift code file. The data model may include several syntax dictionaries corresponding to the general Swift code. Then, according to the data template and each syntax dictionary, the target Swift code of the target Swift code file for the Swift code file can be generated. Then, the target Swift code can be used to replace the general Swift code in the Swift code file. By obtaining the syntax dictionary corresponding to the general Swift code from the Swift code file of the application engineering file during the code compilation period, and then converting the general Swift code into specific template code according to the data template, the replacement of the general Swift code in the application engineering file is realized, effectively reducing the workload of code writing, and being able to avoid the type parsing error problem caused by developers writing code manually, ensuring the stability of the application program operation. Brief Description of the Drawings

[0052] Figure 1 is a flowchart of the steps of a method for processing Swift code provided in an embodiment of the present invention;

[0053] Figure 2 is a block diagram of the structure of a device for processing Swift code provided in an embodiment of the present invention;

[0054] Figure 3 is a block diagram of an electronic device provided in an embodiment of the present invention;

[0055] Figure 4 is a schematic diagram of a computer-readable storage medium provided in an embodiment of the present invention. Detailed Embodiments

[0056] To make the above objects, features, and advantages of the present invention more obvious and understandable, the present invention will be further described in detail below with reference to the accompanying drawings and specific embodiments.

[0057] LLVM (Low Level Virtual Machine) is a framework system for building compilers, written in C++. It is used to optimize the compile-time, link-time, run-time, and idle-time of programs written in any programming language. It is open to developers and compatible with existing scripts. During the development of application projects, LLVM can be used as a framework system for building compilers, covering the entire compilation process of IOS source code, including pre-compilation, intermediate language generation, and compilation backends, and finally compiling the source code into executable machine language. Among them, sourcekitd is one of the toolkits provided in LLVM, which can be used for the display and processing of Swift language in the IDE (Integrated Development Environment), such as source code parsing, syntax highlighting, typesetting, auto-completion, etc.

[0058] As an example, during the process of writing code for the project files of an application, developers need to manually write some common Swift code, such as model initialization, list template initialization, etc. These tasks that involve common code cannot be automatically generated, resulting in high work repetition for developers. At the same time, due to subjective errors in manual writing by developers, type mismatches are likely to occur, leading to crashes during the operation of the application and reducing the stability of the application's operation.

[0059] In this regard, one of the core inventive points of the embodiments of the present invention lies in that during the compilation of the application engineering file, the Swift code involving general functions in the Swift code in the file is obtained through the sourcekitd toolset of LLVM, and is converted into specific template code according to the corresponding syntax dictionary and rewritten into the engineering file to implement the processing of the Swift code. Specifically, during the compilation of the application engineering file, the Swift code file can be obtained from the application engineering file, and during the compilation, the data template for the Swift code file is obtained, and the data model corresponding to the general Swift code is obtained from the Swift code file. The data model may include several syntax dictionaries corresponding to the general Swift code. Then, according to the data template and each syntax dictionary, the target Swift code of the target Swift code file for the Swift code file can be generated. Then, the target Swift code can replace the general Swift code in the Swift code file. By obtaining the syntax dictionary corresponding to the general Swift code from the Swift code file of the application engineering file during the code compilation period, and then converting the general Swift code into specific template code according to the data template, the replacement of the general Swift code in the application engineering file is realized, effectively reducing the workload of code writing, and being able to avoid the type parsing error problem caused by developers writing code by hand, ensuring the stability of the application program operation.

[0060] Specifically, referring to Figure 1 , the step flowchart of a method for processing Swift code provided in the embodiments of the present invention is shown, which may specifically include the following steps:

[0061] Step 101, obtain an initial application engineering file, where the initial application engineering file includes a Swift code file, and the Swift code file includes general Swift code;

[0062] For an application engineering file, it can be an engineering project file corresponding to an application program developed by developers in a development tool. For example, it can be an application program developed by developers in LLVM for IOS or macOS, etc. Among them, the application engineering file can include Swift code files, and the relevant general Swift code in the application engineering file can be included in the Swift code files. For the general Swift code, it can be the code related to general functions in the application program, such as the implementation code related to model initialization, list template initialization, etc. For such general Swift code, it is possible that multiple application functions correspond to the same Swift code. If developers write the general Swift code once for each application function, it will undoubtedly seriously increase the workload of developers and cannot guarantee the type matching during the writing process, affecting the stability of the application program operation.

[0063] Optionally, for the processing of Swift code, it can be processed at the Swift source code level based on the SourceKitd toolset of LLVM, including source code parsing, syntax highlighting, typesetting, auto-completion, and cross-language header file generation, etc. That is, in the embodiments of the present invention, taking the processing of Swift code based on the SourceKitd toolset of LLVM as an example for exemplary illustration. It can be understood that the present invention is not limited thereto.

[0064] Step 102, in response to the compilation of the initial application engineering file, obtain a data template for the Swift code file, and obtain a data model corresponding to the general Swift code from the Swift code file. The data model includes several syntax dictionaries corresponding to the general Swift code.

[0065] Optionally, during the development of the application engineering file, the acquisition of business data is involved. For the acquisition of business data, it is often the case that the client sends a corresponding request to the backend and receives the business data returned by the backend. Among them, the business data can be json data, and the client can convert the json data into a corresponding dictionary. Since the data type of the dictionary itself is relatively flexible, when the hierarchical nesting is relatively deep, it is easy to cause the way of obtaining data values to be too complex, affecting the code writing of developers. In this regard, the data template can effectively solve the difficulty of code writing. The data template can be a data structure for a specific business scenario, and the field names and field types suitable for the business scenario can be defined in the data structure, so as to ensure that the business data is more convenient in use and transmission compared with the dictionary, improving the stability of the code.

[0066] In an embodiment of the present invention, during the compilation of an application engineering file through SourceKitd, a data template for a Swift code file can be obtained, and a data model corresponding to the general Swift code can be obtained from the Swift code file, so as to process the general Swift code through the data template and the data model. Among them, the data model can include the content to be added to the data template, which can include a syntax dictionary corresponding to the general Swift code corresponding to different variables. That is, one Swift code file can correspond to one data model, and one data model can include syntax dictionaries corresponding to multiple general Swift codes.

[0067] Optionally, for a dictionary, it can be a container that can store multiple data in a key-value pair manner disorderly. Each value is associated with a unique key, and the data is stored in an unordered list as a set. Then, the data model can include the association relationship between the relevant data values and data keys in the Swift code file, and the syntax dictionary can include the association relationship between the relevant data values and data keys in the general Swift code, etc. Thus, by determining the dictionary corresponding to the Swift code, it is beneficial to extract the content to be added to the data template from the dictionary.

[0068] For the acquisition of the data model, the file identifier of the Swift code file and the code conversion function for the Swift code file can be obtained first. Then, the file identifier is passed into the code conversion function to extract the general Swift code in the Swift code file and convert the general Swift code into a data model corresponding to the Swift code file. Specifically, the file identifier can be the file name of the Swift code file; the code conversion function can be the execution function corresponding to the conversion API (Application Programming Interface) provided by sourceKitd in LLVM. Then, the file name of the Swift code file can be used as a parameter to input the conversion API to output the data model corresponding to the Swift code file through the corresponding conversion function.

[0069] In one example, during the compilation of the application project files by SourceKitd, the Swift code files and the file names of these files can be obtained first. The corresponding API of sourcekitd can be called to obtain the code conversion function that converts the Swift code file into a data model through dlsym(dlopen("sourcekitd.framework / Versions / A / sourcekitd", RTLD_LAZY), "sourcekitd_response_get_value"). Then, the file name of the Swift code file is passed into this function to extract the data keys and the data values corresponding to the data keys from the general Swift code. The data keys and the corresponding data values are used to construct a data model corresponding to the Swift code file, and then the corresponding data model is output to further process the Swift code according to the data template and several syntax dictionaries in the data model to achieve the replacement of the Swift code.

[0070] Step 103, generate target Swift code for the general Swift code according to the data template and the several syntax dictionaries;

[0071] Before processing the Swift code according to the data template and the syntax dictionaries, the data structure of the data model can be judged first to determine whether the data structure of the data model meets the corresponding protocol. Specifically, it can be detected whether there is a type inheritance relationship in the data model. If there is a type inheritance relationship in the data model, it indicates that the data model is a data structure to be processed, and then the target Swift code for the general Swift code can be generated according to the data template and several syntax dictionaries; if there is no type inheritance relationship in the data model, it indicates that the data model is not a data structure to be processed, and the processing of the Swift code is not performed.

[0072] Optionally, for the initialization protocol, it can be a fixed protocol name. By this protocol name, it is beneficial to mark whether the data model is a data structure to be processed. Then, when the protocol name is included in the data model, it indicates that the data model is a data structure to be processed, and the syntax dictionary can be further determined from the data model.

[0073] In a specific implementation, the variable Key associated with the variable can be obtained by traversing the data model. Then, the syntax dictionary corresponding to the variable Key is extracted from the data model. The syntax dictionary includes the variable information corresponding to the variable Key. Subsequently, the variable information of each syntax dictionary is added to the data template to generate the target Swift code for the general Swift code. Among them, the variable information may include the variable name, variable type, etc. of the variable. Then, the field type and field name of each syntax dictionary can be added to the data template to generate the target Swift code for the general Swift code.

[0074] Among them, in the data model, there may be syntax dictionaries corresponding to different instance variables, and each instance variable can correspond to a syntax dictionary. In this syntax dictionary, the variable name, the length of the variable name, the variable identifier, the string value, the variable type, etc. are stored in the form of key-value pairs. For the variable type, it can be the syntax type of the variable involved in the corresponding syntax dictionary, which may include: Int is an integer type, Double and Float are floating-point types, Bool is a boolean type, and String is a string type; the variable name can be the variable name of the variable involved in the syntax dictionary corresponding to the general Swift code. Thus, when it is determined that the data model meets the initialization protocol, the variable information related to the variable can be extracted from the data model and the variable information is added to the data template to generate the corresponding target Swift code. Thus, through the data template and the data model, the general Swift code in the application project file is converted into specific template code, effectively reducing the workload of developers in writing the general Swift code and avoiding the type mismatch problem caused by manual writing.

[0075] Step 104, replace the general Swift code in the Swift code file with the target Swift code to generate a target application project file.

[0076] After converting the general Swift code related to general functions in the application project file into specific template code (i.e., target Swift code) through the data template and the syntax dictionary corresponding to the Swift code file, the template code can be written into the currently compiled application project file to replace the original general Swift code, obtaining a new application project file. At the same time, the application project file can continue to be compiled to generate the corresponding machine language file. Thus, during the code compilation process of the application project file, the syntax dictionary corresponding to the general Swift code can be obtained from the Swift code file of the application project file, and then the general Swift code can be converted into specific template code according to the data template, realizing the replacement of the Swift code in the application project file. Furthermore, by compiling while performing code conversion, it not only effectively reduces the workload of code writing, avoids the type parsing error problem caused by developers writing code by hand, ensures the stability of the application program running, but also can improve the efficiency of code compilation through the method of "compilation + code conversion".

[0077] It should be noted that the embodiments of the present invention include but are not limited to the above examples. It can be understood that under the guidance of the idea of the embodiments of the present invention, those skilled in the art can also make settings according to actual needs, and the present invention makes no limitation thereto.

[0078] In the embodiments of the present invention, during the compilation process of the application project file, the Swift code file can be obtained from the application project file, and during the compilation process, the data model corresponding to the general Swift code and the data template for the Swift code file can be obtained from the Swift code file. Among them, the data model can include several syntax dictionaries corresponding to the general Swift code. Then, according to the data template and each syntax dictionary, the target Swift code of the target Swift code file for the Swift code file can be generated. Then, the target Swift code can be used to replace the general Swift code in the Swift code file. By obtaining the syntax dictionary corresponding to the general Swift code from the Swift code file of the application project file during the code compilation period, and then converting the general Swift code into specific template code according to the data template, the replacement of the Swift code in the application project file is realized, effectively reducing the workload of code writing, and being able to avoid the type parsing error problem caused by developers writing code by hand, ensuring the stability of the application program running.

[0079] To enable those skilled in the art to better understand the technical solutions of the embodiments of the present invention, the following is an exemplary illustration through an example.

[0080] Developers can develop applications for iOS or macOS in LLVM. When compiling the application project files of relevant applications through the SourceKitd toolset based on LLVM, it is possible to convert the Swift code related to common functions in the project files while compiling, so as to reduce the writing of common Swift code by developers.

[0081] Specifically, assuming HunTianSwiftLint is a data template, its related code can be as follows:

[0082]

[0083] During the compilation process using SourceKitd, the data model corresponding to the Swift code file in the application project file can be obtained. Then, by determining whether the data model follows the WBSwiftyJSONAutoGenertorCode protocol, it can be determined whether the data model is the data structure to be processed. If so, the syntax dictionary related to variables can be extracted from the data model, and the corresponding variable information can be extracted from the syntax dictionary, including the variable types and variable names corresponding to each variable, etc., to construct the corresponding target Swift code. Then, according to the variable information stored in the syntax dictionary, such as the variable type (typename) being ""key.typename":"Bool?"" and the variable name (name) being ""key.name":"three"", the corresponding initialization method can be automatically generated by concatenating the variable information, such as three = serverDictionary["three"] as? Bool, and then adding this initialization method to the data template to generate the corresponding target Swift code.

[0084] For example, assuming that the relevant code can include three variables, one, two, and three, with corresponding variable types of String, Int, and Bool?, respectively, the corresponding target Swift code can be as follows:

[0085]

[0086]

[0087] After the Swift code conversion is completed, the automatically generated code can be written into the currently compiled application project file to replace the original general Swift code, and then continue the compilation. By obtaining the syntax dictionary corresponding to the general Swift code from the Swift code file of the application project file during code compilation, and then converting the general Swift code into specific template code according to the data template, the replacement of the Swift code in the application project file is realized, effectively reducing the workload of code writing, and being able to avoid the type parsing error problem caused by developers writing code by hand, ensuring the stability of the application program operation.

[0088] It should be noted that for the method embodiments, for the sake of simple description, they are all expressed as a series of action combinations. However, those skilled in the art should know that the embodiments of the present invention are not limited by the described action sequence, because according to the embodiments of the present invention, certain steps can be carried out in other sequences or simultaneously. Secondly, those skilled in the art should also know that the embodiments described in the specification are all preferred embodiments, and the actions involved are not necessarily essential for the embodiments of the present invention.

[0089] Refer to Figure 2 , which shows the structural block diagram of a processing device for Swift code provided in the embodiments of the present invention, and specifically may include the following modules:

[0090] The project file acquisition module 201 is used to acquire an initial application project file, where the initial application project file includes a Swift code file, and the Swift code file includes general Swift code;

[0091] The data acquisition module 202 is used to, in response to the compilation of the initial application project file, acquire a data template for the Swift code file, and acquire a data model corresponding to the general Swift code from the Swift code file, where the data model includes several syntax dictionaries corresponding to the general Swift code;

[0092] The code generation module 203 is used to generate target Swift code for the general Swift code according to the data template and the several syntax dictionaries;

[0093] The project file generation module 204 is used to replace the general Swift code in the Swift code file with the target Swift code to generate a target application project file.

[0094] In an alternative embodiment, the data acquisition module 202 includes:

[0095] The function acquisition sub-module is used to obtain the file identifier of the Swift code file and the code conversion function for the Swift code file;

[0096] The data model generation sub-module is used to pass the file identifier into the code conversion function, extract the common Swift code in the Swift code file, and convert the common Swift code into a data model corresponding to the Swift code file.

[0097] In an alternative embodiment, the data model generation sub-module is specifically configured to:

[0098] Extract data keys and data values corresponding to the data keys from the common Swift code;

[0099] Construct a data model corresponding to the Swift code file by using each of the data keys and the corresponding data values.

[0100] In an alternative embodiment, the code generation module 203 is specifically configured to:

[0101] If the data model includes a type inheritance relationship, determine that the data model is a data structure to be processed, and generate target Swift code for the common Swift code according to the data template and the several syntax dictionaries.

[0102] In an alternative embodiment, the code generation module 203 includes:

[0103] The variable Key acquisition sub-module is used to traverse the data model and obtain the variable Key associated with the variable;

[0104] The syntax dictionary extraction sub-module is used to extract the syntax dictionary corresponding to the variable Key from the data model, and the syntax dictionary includes variable information corresponding to the variable Key;

[0105] The code generation sub-module is used to add the variable information of each syntax dictionary to the data template to generate target Swift code for the common Swift code.

[0106] In an alternative embodiment, the variable information includes at least a field type and a field name, and the code generation sub-module is specifically configured to:

[0107] Add the field type and field name of each syntax dictionary to the data template to generate target Swift code for the common Swift code.

[0108] In an alternative embodiment, it further includes:

[0109] A compilation module for compiling the target application project file to generate a machine language file corresponding to the initial application project file.

[0110] For the device embodiments, since they are basically similar to the method embodiments, the description is relatively simple. For related parts, please refer to the corresponding descriptions in the method embodiments.

[0111] In addition, an embodiment of the present invention also provides an electronic device, as Figure 3 shown, including a processor 301, a communication interface 302, a memory 303, and a communication bus 304. Among them, the processor 301, the communication interface 302, and the memory 303 complete mutual communication through the communication bus 304.

[0112] The memory 303 is used to store computer programs.

[0113] When the processor 301 is used to execute the program stored on the memory 303, the following steps are implemented:

[0114] Obtain an initial application project file, where the initial application project file includes Swift code files, and the Swift code files include general Swift code.

[0115] In response to the compilation of the initial application project file, obtain a data template for the Swift code file, and obtain a data model corresponding to the general Swift code from the Swift code file. The data model includes several syntax dictionaries corresponding to the general Swift code.

[0116] Generate target Swift code for the general Swift code according to the data template and the several syntax dictionaries.

[0117] Replace the general Swift code in the Swift code file with the target Swift code to generate a target application project file.

[0118] In an alternative embodiment, the obtaining the data model corresponding to the general Swift code from the Swift code file includes:

[0119] Obtain the file identifier of the Swift code file and the code conversion function for the Swift code file.

[0120] Pass the file identifier into the code conversion function, extract the general Swift code in the Swift code file, and convert the general Swift code into a data model corresponding to the Swift code file.

[0121] In an alternative embodiment, the conversion of the general Swift code into a data model corresponding to the Swift code file includes:

[0122] Extracting data keys and data values corresponding to the data keys from the general Swift code;

[0123] Constructing a data model corresponding to the Swift code file by using each of the data keys and the corresponding data values.

[0124] In an alternative embodiment, the generation of the target Swift code for the general Swift code according to the data template and the several syntax dictionaries includes:

[0125] If the data model includes a type inheritance relationship and it is determined that the data model is a data structure to be processed, then generating the target Swift code for the general Swift code according to the data template and the several syntax dictionaries.

[0126] In an alternative embodiment, the generation of the target Swift code for the general Swift code according to the data template and the several syntax dictionaries includes:

[0127] Traversing the data model to obtain variable keys associated with variables;

[0128] Extracting the syntax dictionaries corresponding to the variable keys from the data model, where the syntax dictionaries include variable information corresponding to the variable keys;

[0129] Adding the variable information of each of the syntax dictionaries to the data template to generate the target Swift code for the general Swift code.

[0130] In an alternative embodiment, the variable information includes at least a field type and a field name, and the adding the variable information of each of the syntax dictionaries to the data template to generate the target Swift code for the general Swift code includes:

[0131] Adding the field types and field names of each of the syntax dictionaries to the data template to generate the target Swift code for the general Swift code.

[0132] In an alternative embodiment, it further includes:

[0133] Compiling the target application project file to generate a machine language file corresponding to the initial application project file.

[0134] The communication bus mentioned in the above terminal may be a Peripheral Component Interconnect (PCI) bus, an Extended Industry Standard Architecture (EISA) bus, or the like. This communication bus can be divided into an address bus, a data bus, a control bus, etc. For the sake of simplicity, only a thick line is used to represent it in the figure, but it does not mean that there is only one bus or one type of bus.

[0135] The communication interface is used for communication between the above terminal and other devices.

[0136] The memory may include a Random Access Memory (RAM), or may also include a non-volatile memory, such as at least one disk memory. Optionally, the memory may also be at least one storage device located far from the aforementioned processor.

[0137] The above-mentioned processor may be a general-purpose processor, including a Central Processing Unit (CPU), a Network Processor (NP), etc.; it may also be a Digital Signal Processor (DSP), an Application Specific Integrated Circuit (ASIC), a Field-Programmable Gate Array (FPGA), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components.

[0138] As Figure 4 shown, in another embodiment provided by the present invention, there is also provided a computer-readable storage medium 401. Instructions are stored in this computer-readable storage medium. When it runs on a computer, it causes the computer to execute the processing method of the Swift code described in the above embodiment.

[0139] In another embodiment provided by the present invention, there is also provided a computer program product containing instructions. When it runs on a computer, it causes the computer to execute the processing method of the Swift code described in the above embodiment.

[0140] In the above embodiments, it can be implemented in whole or in part by software, hardware, firmware, or any combination thereof. When implemented using software, it can be implemented in whole or in part in the form of a computer program product. The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, the processes or functions described in the embodiments of the present invention are generated in whole or in part. The computer can be a general-purpose computer, a special-purpose computer, a computer network, or other programmable devices. The computer instructions can be stored in a computer-readable storage medium, or transmitted from one computer-readable storage medium to another. For example, the computer instructions can be transmitted from one website, computer, server, or data center to another website, computer, server, or data center via wired (such as coaxial cable, optical fiber, digital subscriber line (DSL)) or wireless (such as infrared, wireless, microwave, etc.) means. The computer-readable storage medium can be any available medium that can be accessed by a computer, or a data storage device such as a server or data center that includes one or more integrated available media. The available medium can be a magnetic medium (such as a floppy disk, hard disk, magnetic tape), an optical medium (such as a DVD), or a semiconductor medium (such as a solid state disk (SSD)).

[0141] It should be noted that in this document, relational terms such as first and second are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any actual relationship or order between these entities or operations. Moreover, the term "comprising", "including", or any other variation thereof is intended to cover non-exclusive inclusion, so that a process, method, article, or device that includes a series of elements includes not only those elements but also other elements that are not explicitly listed, or also includes elements that are inherent to such process, method, article, or device. Without further limitation, an element defined by the statement "including one..." does not exclude the existence of additional identical elements in the process, method, article, or device that includes the element.

[0142] Each embodiment in this specification is described in a related manner. The same or similar parts between the embodiments can be referred to each other, and the differences between each embodiment and other embodiments are emphasized. In particular, for the system embodiment, since it is basically similar to the method embodiment, the description is relatively simple, and the relevant parts can be referred to the description of the method embodiment.

[0143] The above are only the preferred embodiments of the present invention and are not intended to limit the protection scope of the present invention. Any modifications, equivalent replacements, improvements, etc. made within the spirit and principle of the present invention are all included within the protection scope of the present invention.

Claims

1. A method for processing Swift code, characterized in that, Including: Obtain an initial application project file, where the initial application project file includes Swift code files, and the Swift code files include general Swift code, and the general Swift code is code related to general functions in the application; In response to the compilation of the initial application project file, obtain a data template for the Swift code file, and obtain a data model corresponding to the general Swift code from the Swift code file. The data model includes several syntax dictionaries corresponding to the general Swift code. The data model includes content that needs to be added to the data template. The syntax dictionary includes the association relationship between the data values and data keys in the general Swift code; Generate target Swift code for the general Swift code according to the data template and the several syntax dictionaries; Replace the general Swift code in the Swift code file with the target Swift code to generate a target application project file; The obtaining the data model corresponding to the general Swift code from the Swift code file includes: Obtain the file identifier of the Swift code file and the code conversion function for the Swift code file; Pass the file identifier into the code conversion function, extract the general Swift code in the Swift code file, extract the data keys and the data values corresponding to the data keys from the general Swift code, and use each of the data keys and the corresponding data values to construct a data model corresponding to the Swift code file.

2. The method according to claim 1, characterized in that The generating the target Swift code for the general Swift code according to the data template and the several syntax dictionaries includes: If the data model includes a type inheritance relationship, determine that the data model is a data structure to be processed, and then generate the target Swift code for the general Swift code according to the data template and the several syntax dictionaries.

3. The method according to claim 1, wherein The generating the target Swift code for the general Swift code according to the data template and the several syntax dictionaries includes: Traverse the data model to obtain variable Keys associated with variables; Extract the syntax dictionary corresponding to the variable Key from the data model, and the syntax dictionary includes variable information corresponding to the variable Key; Add the variable information of each syntax dictionary to the data template to generate the target Swift code for the general Swift code.

4. The method according to claim 3, characterized in that The variable information includes at least a field type and a field name. The adding the variable information of each syntax dictionary to the data template to generate the target Swift code for the general Swift code includes: Add the field type and field name of each syntax dictionary to the data template to generate the target Swift code for the general Swift code.

5. The method according to claim 1, characterized in that Also including: Compile the target application engineering file to generate a machine language file corresponding to the initial application engineering file.

6. A processing device for Swift code, characterized in that, Including: An engineering file acquisition module for acquiring an initial application engineering file, where the initial application engineering file includes Swift code files, and the Swift code files include general Swift code, and the general Swift code is code related to general functions in the application program; A data acquisition module for, in response to the compilation of the initial application engineering file, acquiring a data template for the Swift code file, and obtaining a data model corresponding to the general Swift code from the Swift code file, where the data model includes a number of syntax dictionaries corresponding to the general Swift code, the data model includes content that needs to be added to the data template, and the syntax dictionary includes the association relationship between the data value and the data key in the general Swift code; A code generation module for generating target Swift code for the general Swift code according to the data template and the number of syntax dictionaries; An engineering file generation module for replacing the general Swift code in the Swift code file with the target Swift code to generate a target application engineering file; The data acquisition module includes: A function acquisition sub-module for acquiring the file identifier of the Swift code file and the code conversion function for the Swift code file; A data model generation sub-module for passing the file identifier into the code conversion function, extracting the general Swift code in the Swift code file, and converting the general Swift code into a data model corresponding to the Swift code file; The data model generation sub-module is specifically used for: extracting data keys and data values corresponding to the data keys from the general Swift code; constructing a data model corresponding to the Swift code file by using each of the data keys and the corresponding data values.

7. An electronic device, characterized in that, Including a processor, a communication interface, a memory, and a communication bus, where the processor, the communication interface, and the memory complete communication with each other through the communication bus; The memory is used for storing a computer program; When the processor is used to execute the program stored on the memory, it implements the method according to any one of claims 1-5.

8. A computer-readable storage medium, on which instructions are stored, and when executed by one or more processors, cause the processors to execute the method according to any one of claims 1-5.

Citation Information

Patent Citations

  • Method and device for generating a program code file, and an electronic device

    CN109491652A

  • File conversion method and device and storage medium

    CN112416365A