欢迎加入QQ讨论群258996829
Swift 头像
苹果5袋
5
Swift

对苹果“五仁”编程语言Swift的简单分析

发布时间:2014-07-05 18:27  回复:0  查看:3638   最后回复:2014-07-05 18:27  

在CSDN上看到的文章。转给大家。


苹果在昨天的WWDC上,发布了新的编程语言Swift。这两天开发社区都在讨论这个语言,从语言的特性上大家发现了好多语言的影子,这样的情况可以说是集大成,也可以说是“五仁”。每个人看问题的角度都不同,下面从个人的角度来看看这门语言涉及到的工具链及其对越狱开发的影响。

由于刚刚发布,针对相关工具的介绍几乎没有,那我们就从xcode中寻找。在shell中执行:

1find /Applications/Xcode6-Beta.app/Contents/ -name "*swift*"
可以找到swift相关的文件,简单过滤后,如下的文件比较重要:


1/Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift
2/Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-demangle
3/Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-ide-test
4/Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-stdlib-tool
5/Applications/Xcode6-Beta.app/Contents/SharedFrameworks/LLDB.framework/Versions/A/Resources/repl_swift
6/Applications/Xcode6-Beta.app/Contents//Developer/Toolchains/XcodeDefault.xctoolchain/usr/share/man/man1/swift.1
从找到的文件中我们可以看到包含 man 手册文件,我们首先看看手册的内容:


01swift(1)                                   Swift Documentation                                  swift(1)
02 
03NAME
04       swift - <strong>the amazingly new programming language</strong>
05 
06SYNOPSIS
07       swift [-emit-object|-emit-assembly|-emit-library|-i]
08         [-help]
09         -o output-file
10         input-filenames
11 
12       The full list of supported options is available via "swift -help".
13 
14DESCRIPTION
15       Swift is a new, high performance systems programming language.  It has a clean and modern syntax,
16       and offers seamless access to existing C and Objective-C code and frameworks, and is memory safe
17       (by default).
18 
19       Although inspired by Objective-C and many other languages, Swift is not itself a C-derived
20       language. As a complete and independent language, Swift packages core features like flow control,
21       data structures, and functions, with high-level constructs like objects, protocols, closures, and
22       generics.  Swift embraces modules, eliminating the need for headers and the code duplication they
23       entail.
24 
25                                               2014-05-17                                       swift(1)
从手册中可以得到的信息也非常有限,但是看到参数:-help,我们看看帮助会输出什么:


01OVERVIEW: Swift compiler
02 
03USAGE: swift [options] <inputs>
04 
05MODES:
06  -dump-ast        Parse and type-check input file(s) and dump AST(s)
07  -dump-parse      Parse input file(s) and dump AST(s)
08  -emit-assembly   Emit assembly file(s) (-S)
09  -emit-bc         Emit LLVM BC file(s)
10  -emit-executable Emit a linked executable
11  -emit-ir         Emit LLVM IR file(s)
12  -emit-library    Emit a linked library
13  -emit-object     Emit object file(s) (-c)
14  -emit-silgen     Emit raw SIL file(s)
15  -emit-sil        Emit canonical SIL file(s)
16  -integrated-repl Integrated REPL mode
17  -i               Immediate mode
18  -lldb-repl       LLDB-enhanced REPL mode
19  -parse           Parse input file(s)
20  -print-ast       Parse and type-check input file(s) and pretty print AST(s)
21  -repl            REPL mode
22 
23OPTIONS:
24  -application-extension  Restrict code to those available for App Extensions
25  -arch <arch>            Compile for architecture <arch>
26  -assert-config <value>  Specify the assert_configuration replacement. Possible values are Debug, Release, Replacement.
27  -D <value>              Specifies one or more build configuration options
28  -emit-dependencies      Emit Make-compatible dependencies files
29  -emit-module-path <path>
30                          Emit an importable module to <path>
31  -emit-module            Emit an importable module
32  -emit-objc-header-path <path>
33                          Emit an Objective-C header file to <path>
34  -emit-objc-header       Emit an Objective-C header file
35  -framework <value>      Specifies a framework which should be linked against
36  -F <value>              Add directory to framework search path
37  -g                      Emit debug info
38  -help                   Display available options
39  -import-underlying-module
40                          Implicitly imports the Objective-C half of a module
41  -I <value>              Add directory to the import search path
42  -j <n>                  Number of commands to execute in parallel
43  -L <value>              Add directory to library link search path
44  -l<value>               Specifies a library which should be linked against
45  -module-cache-path <value>
46                          Specifies the Clang module cache path
47  -module-link-name <value>
48                          Library to link against when using this module
49  -module-name <value>    Name of the module to build
50  -nostdimport            Don't search the standard library import path for modules
51  -output-file-map <path> A file which specifies the location of outputs
52  -o <file>               Write output to <file>
53  -parse-as-library       Parse the input file(s) as libraries, not scripts
54  -parse-sil              Parse the input file as SIL code, not Swift source
55  -save-temps             Save intermediate compilation results
56  -sdk <sdk>              Compile against <sdk>
57  -serialize-diagnostics  Serialize diagnostics in a binary format
58  -target-cpu <value>     Generate code for a particular CPU variant
59  -target-feature [+-]<feature-name>
60                          Generate code with a particular CPU feature enabled or disabled
61  -target <value>         Generate code for the given target
62  -version                Print version information and exit
63  -v                      Show commands to run and use verbose output
64  -Xcc <arg>              Pass <arg> to the C/C++/Objective-C compiler
65  -Xfrontend <arg>        Pass <arg> to the Swift frontend
66  -Xlinker <value>        Specifies an option which should be passed to the linker
67  -Xllvm <arg>            Pass <arg> to LLVM.
这么多参数到底怎么用呢?!我们一起来看看 xcode 是怎么使用的。


打开xcode6新建一个空工程,取名“FuckSwift”:



将工程的Deployment Target改成 5.0,然后编译,并在设备上运行。我手上的设备是iPad2-iOS7.0.4,可以正常运行。为什么可以正常运行?swift相关的运行时库怎么处理?我们打开编译好的app目录:



可以看到比以前的程序多了一个目录 Frameworks,内容如下:



因此,在iOS8之下的设备上,程序打包的时候带上了必要的运行库。对于iOS8,目前还没法解开 dyld cache 也就无法知道设备上是否带了这些库,不过应该会带。

 

接上文,我们建立测试工程的目的是为了看看 xcode 如何使用 swift 编译工具,xcoce编译时的相关参数如下:
01/Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift
02-target armv7-apple-ios5.0
03-module-name FuckSwift
04-O0
05-sdk /Applications/Xcode6-Beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS8.0.sdk
06-g
07-module-cache-path /Users/proteas/Library/Developer/Xcode/DerivedData/ModuleCache
08-I /Users/proteas/Library/Developer/Xcode/DerivedData/FuckSwift-czflmbyelvdrnvaxblvpkcrhfbie/Build/Products/Debug-iphoneos
09-F /Users/proteas/Library/Developer/Xcode/DerivedData/FuckSwift-czflmbyelvdrnvaxblvpkcrhfbie/Build/Products/Debug-iphoneos
10-parse-as-library
11-c
12-j8
13/Users/proteas/Desktop/FuckSwift/FuckSwift/AppDelegate.swift
14-output-file-map /Users/proteas/Library/Developer/Xcode/DerivedData/FuckSwift-czflmbyelvdrnvaxblvpkcrhfbie/Build/Intermediates/FuckSwift.build/Debug-iphoneos/FuckSwift.build/Objects-normal/armv7/FuckSwift-OutputFileMap.json
15-serialize-diagnostics
16-emit-dependencies
17-emit-module
18-emit-module-path /Users/proteas/Library/Developer/Xcode/DerivedData/FuckSwift-czflmbyelvdrnvaxblvpkcrhfbie/Build/Intermediates/FuckSwift.build/Debug-iphoneos/FuckSwift.build/Objects-normal/armv7/FuckSwift.swiftmodule
19-Xcc
20-iquote
21-Xcc /Users/proteas/Library/Developer/Xcode/DerivedData/FuckSwift-czflmbyelvdrnvaxblvpkcrhfbie/Build/Intermediates/FuckSwift.build/Debug-iphoneos/FuckSwift.build/FuckSwift-generated-files.hmap
22-Xcc -I/Users/proteas/Library/Developer/Xcode/DerivedData/FuckSwift-czflmbyelvdrnvaxblvpkcrhfbie/Build/Intermediates/FuckSwift.build/Debug-iphoneos/FuckSwift.build/FuckSwift-own-target-headers.hmap
23-Xcc -I/Users/proteas/Library/Developer/Xcode/DerivedData/FuckSwift-czflmbyelvdrnvaxblvpkcrhfbie/Build/Intermediates/FuckSwift.build/Debug-iphoneos/FuckSwift.build/FuckSwift-all-target-headers.hmap
24-Xcc
25-iquote
26-Xcc /Users/proteas/Library/Developer/Xcode/DerivedData/FuckSwift-czflmbyelvdrnvaxblvpkcrhfbie/Build/Intermediates/FuckSwift.build/Debug-iphoneos/FuckSwift.build/FuckSwift-project-headers.hmap -Xcc -I/Users/proteas/Library/Developer/Xcode/DerivedData/FuckSwift-czflmbyelvdrnvaxblvpkcrhfbie/Build/Products/Debug-iphoneos/include
27-Xcc
28-I/Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
29-Xcc -I/Users/proteas/Library/Developer/Xcode/DerivedData/FuckSwift-czflmbyelvdrnvaxblvpkcrhfbie/Build/Intermediates/FuckSwift.build/Debug-iphoneos/FuckSwift.build/DerivedSources/armv7
30-Xcc -I/Users/proteas/Library/Developer/Xcode/DerivedData/FuckSwift-czflmbyelvdrnvaxblvpkcrhfbie/Build/Intermediates/FuckSwift.build/Debug-iphoneos/FuckSwift.build/DerivedSources
31-Xcc
32-DDEBUG=1
33-emit-objc-header
34-emit-objc-header-path /Users/proteas/Library/Developer/Xcode/DerivedData/FuckSwift-czflmbyelvdrnvaxblvpkcrhfbie/Build/Intermediates/FuckSwift.build/Debug-iphoneos/FuckSwift.build/Objects-normal/armv7/FuckSwift-Swift.h

参数还是太多,继续精简,把去掉绝对路径:

01swift   -target armv7-apple-ios5.0
02        -module-name FuckSwift
03        -O0
04        -sdk iPhoneOS8.0.sdk
05        -g
06        -module-cache-path ModuleCache
07        -I Debug-iphoneos
08        -F Debug-iphoneos
09        -parse-as-library
10        -c
11        -j8
12        AppDelegate.swift
13        -output-file-map FuckSwift-OutputFileMap.json
14        -serialize-diagnostics
15        -emit-dependencies
16        -emit-module
17        -emit-module-path FuckSwift.swiftmodule
18        -Xcc -iquote
19        -Xcc FuckSwift-generated-files.hmap
20        -Xcc -IFuckSwift-own-target-headers.hmap
21        -Xcc -IFuckSwift-all-target-headers.hmap
22        -Xcc -iquote
23        -Xcc FuckSwift-project-headers.hmap
24        -Xcc -Iinclude
25        -Xcc -I/usr/include
26        -Xcc -Iarmv7
27        -Xcc -IDerivedSources
28        -Xcc -DDEBUG=1
29        <strong>-emit-objc-header
30        -emit-objc-header-path FuckSwift-Swift.h</strong>

这样就相对清晰了。这里面的参数大家可以对照上述的帮助查看,这里我们一起看两点:

1、Xcc <arg>:Pass <arg> to the C/C++/Objective-C compiler,这里我们可以得到:swift代码很可能最终被转变成C/C++/Objective-C代码进行静态编译


2、会生成FuckSwift-Swift.h头文件,对比下这个文件与swift文件。

01@UIApplicationMain
02class AppDelegate: UIResponder, UIApplicationDelegate {
03    var window: UIWindow?
04    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
05        self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
06        self.window!.backgroundColor = UIColor.whiteColor()
07        self.window!.makeKeyAndVisible()
08        return true
09    }
10    func applicationWillResignActive(application: UIApplication) {
11    }
12    func applicationDidEnterBackground(application: UIApplication) {
13    }
14    func applicationWillEnterForeground(application: UIApplication) {
15    }
16    func applicationDidBecomeActive(application: UIApplication) {
17    }
18    func applicationWillTerminate(application: UIApplication) {
19    }
20}

01SWIFT_CLASS("_TtC9FuckSwift11AppDelegate")
02@interface AppDelegate : UIResponder <UIApplicationDelegate>
03@property (nonatomic) UIWindow * window;
04- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
05- (void)applicationWillResignActive:(UIApplication *)application;
06- (void)applicationDidEnterBackground:(UIApplication *)application;
07- (void)applicationWillEnterForeground:(UIApplication *)application;
08- (void)applicationDidBecomeActive:(UIApplication *)application;
09- (void)applicationWillTerminate:(UIApplication *)application;
10- (instancetype)init OBJC_DESIGNATED_INITIALIZER;
11@end

可以看到swift程序是被转换了的,但是是不是转换成了Objective-C程序呢?还需要进一步分析。

打开IDA,加载测试程序的MachO:


我们重点看下函数窗口:


从函数名上我们可以得到如下结论:


1、可以看到函数名被“改编”了。


2、swift代码并不是被简单得编译成了ObjC代码


3、swift代码最终被编译成了纯C代码,相对于ObjC来说省去了运行时查表进行函数调用的开销,执行效率应该比ObjC要高。


4、从文档上看swfit相对于ObjC来说是更加动态的语言,开发效率也应该比ObjC高


5、对于越狱开发来说,我们只要像Hook C函数那样来Hook swift 函数就可以了,因此MobileSubstrate还是可以工作的。


6、生成的代码还是要遵循 ARM 的ABI标准


 

就像C++的名称改编一样,改编后的名称虽然可以手工还原但是太麻烦,最好找到工具来做这一件事。前文,我们在查找swift相关的文件时看到一个工具:swift-demangle,下面我们就用它来demangle改编后的名称,看看可以得到什么。

首先,我们切换命令行到xcode6的命令行工具:

1sudo xcode-select --switch /Applications/Xcode6-Beta.app/Contents/Developer
然后,我们看看swift-demangle的命令行参数:

1USAGE: swift-demangle [options] [mangled name...]
2 
3OPTIONS:
4  -compact   - Compact mode (only emit the demangled names)
5  -expand    - Expand mode (show node structure of the demangling)
6  -help      - Display available options (-help-hidden for more)
7  -no-sugar  - No sugar mode (disable common language idioms such as ? and [] from the output)
8  -tree-only - Tree-only mode (do not show the demangled string)
9  -version   - Display the version of this program
从IDA中复制一个函数名,如:

1__TToFC9FuckSwift11AppDelegate26applicationDidBecomeActivefS0_FCSo13UIApplicationT_
我们执行如下命令:

1xcrun swift-demangle "__TToFC9FuckSwift11AppDelegate26applicationDidBecomeActivefS0_FCSo13UIApplicationT_"
得到输出:

1_TToFC9FuckSwift11AppDelegate26applicationDidBecomeActivefS0_FCSo13UIApplicationT_ ---> @objc FuckSwift.AppDelegate.applicationDidBecomeActive (FuckSwift.AppDelegate)(ObjectiveC.UIApplication) -> ()

可以看到函数被还原。

到这里我们停下,看看swift对越狱开发的影响:


1、目前无法使用class-dump得到ObjC的头文件。


2、MobileSubstrate应该还可以正常工作。


3、可以使用demangle工具还原函数名,后期这部分工作IDA可能会自动支持。

总之,对越狱开发的流程会稍微有影响,针对应用的越狱开发还可以存在。


下面我们看看命令行工具swift-ide-test的参数:

01OVERVIEW: Swift IDE Test
02 
03USAGE: swift-ide-test [options] [input files...]
04 
05OPTIONS:
06  -D=<string>                              - Build configurations
07  -F=<string>                              - add a directory to the framework search path
08  -I=<string>                              - add a directory to the import search path
09  -annotate-print                          - Annotate AST printing
10  -code-completion-diagnostics             - Print compiler diagnostics while doing code completion
11  -code-completion-token=<string>          - Code completion token name
12  -comments-xml-schema=<string>            - Filename of the RelaxNG schema for documentation comments
13  -enable-objc-factory-method-constructors - Implicitly import Objective-C factory methods as initializers
14  -enable-objc-implicit-properties         - Implicitly import Objective-C getter/setter pairs as properties
15  -explode-pattern-binding-decls           - Separate pattern binding decls into individual var decls
16  -fatal-assembler-warnings                - Consider warnings as error
17  -fully-qualified-types                   - Print fully qualified types
18  -fully-qualified-types-if-ambiguous      - Print types fully-qualified if they would be ambiguous otherwise
19  -function-definitions                    - Print function bodies
20  -help                                    - Display available options (-help-hidden for more)
21  -implicit-objc-with                      - Make the "with" implicit in initializers
22  -import-objc-header=<string>             - header to implicitly import
23  -module-cache-path=<string>              - Clang module cache path
24  -module-print-hidden                     - Print non-exported imported or submodules
25  -module-print-skip-overlay               - Skip Swift overlay modules
26  -module-print-submodules                 - Recursively print submodules
27  -module-to-print=<string>                - Name of the module to print
28  -objc-bridge-dictionary                  - Bridge Dictionary<K, V> to NSDictionary
29  -prefer-type-repr                        - When printing types, prefer printing TypeReprs
30  -print-after-all                         - Print IR after each pass
31  -print-before-all                        - Print IR before each pass
32  Mode:
33    -code-completion                       - Perform code completion
34    -repl-code-completion                  - Perform REPL-style code completion
35    -syntax-coloring                       - Perform syntax coloring
36    -structure                             - Perform document structure annotation
37    -annotate                              - Perform semantic annotation
38    -test-input-complete                   - Check if input source is complete
39    -print-ast-not-typechecked             - Print the non-typechecked AST
40    -print-ast-typechecked                 - Print the typechecked AST
41    -print-module                          - Print visible declarations in a module
42    -print-types                           - Print types of all subexpressions and declarations in the AST
43    -print-comments                        - Print documentation comments attached to decls
44    -print-module-comments                 - Given a module, print documentation comments attached to decls
45    -print-module-imports                  - Recursively print all imports visible from a particular module
46    -print-usrs                            - Print USRs for all decls
47    -parse-rest                            - Parse a ReST file
48  -print-implicit-attrs                    - Print implicit attributes
49  -print-regular-comments                  - Print regular comments from clang module headers
50  -print-stats                             - Print statistics
51  -sdk=<string>                            - path to the SDK to build against
52  -skip-private-stdlib-decls               - Don't print declarations that start with '_'
53  -skip-unavailable                        - Don't print unavailable declarations
54  -source-filename=<string>                - Name of the source file
55  -split-objc-selectors                    - Split Objective-C selectors
56  -stats                                   - Enable statistics output from program (available with Asserts)
57  -synthesize-sugar-on-types               - Always print Array and Optional with sugar
58  -target=<string>                         - target triple
59  -terminal                                - Use terminal color for source annotations
60  -time-passes                             - Time each pass, printing elapsed time for each on exit
61  -typecheck                               - Type check the AST
62  -version                                 - Display the version of this program


从参数上看,这很可能是xcode的内部工具,用来做代码完成与检查的。这里不具体分析,感兴趣的兄弟可以自己试试。

 

下面我们继续回到 swift 工具上,首先看看parse的输出,执行如下命令:

1xcrun swift AppDelegate.swift -dump-parse
得到如下输出:

001(source_file
002  (import_decl UIKit')
003  (class_decl "AppDelegate" type='<null type>' inherits: <null>, <null>
004    (pattern_binding_decl
005      (pattern_typed
006        (pattern_named 'window')
007))
008    (var_decl "window" type='<null type>' storage_kind='stored')
009    (func_decl "application(_:didFinishLaunchingWithOptions:)" type='<null type>'
010      (body_params
011        (pattern_typed implicit
012          (pattern_named implicit 'self'))
013        (pattern_tuple
014          (pattern_typed
015            (pattern_named 'application')
016            (type_ident
017              (component id='UIApplication' bind=none)))
018          (pattern_typed
019            (pattern_named 'launchOptions')
020)))
021      (result
022        (type_ident
023          (component id='Bool' bind=none)))
024      (brace_stmt
025        (sequence_expr type='<null>'
026          (unresolved_dot_expr type='<null>' field 'window'
027            (declref_expr type='<null>' decl=AppDelegate.(file).AppDelegate.func decl.self@AppDelegate.swift:17:10 specialized=yes))
028          (assign_expr
029            (**NULL EXPRESSION**)
030            (**NULL EXPRESSION**))
031          (call_expr type='<null>'
032            (unresolved_decl_ref_expr type='<null>' name=UIWindow specialized=no)
033            (tuple_expr type='<null>' names=frame
034              (unresolved_dot_expr type='<null>' field 'bounds'
035                (call_expr type='<null>'
036                  (unresolved_dot_expr type='<null>' field 'mainScreen'
037                    (unresolved_decl_ref_expr type='<null>' name=UIScreen specialized=no))
038                  (tuple_expr type='<null>'))))))
039        (sequence_expr type='<null>'
040          (unresolved_dot_expr type='<null>' field 'backgroundColor'
041            (force_value_expr type='<null>'
042              (unresolved_dot_expr type='<null>' field 'window'
043                (declref_expr type='<null>' decl=AppDelegate.(file).AppDelegate.func decl.self@AppDelegate.swift:17:10 specialized=yes))))
044          (assign_expr
045            (**NULL EXPRESSION**)
046            (**NULL EXPRESSION**))
047          (call_expr type='<null>'
048            (unresolved_dot_expr type='<null>' field 'whiteColor'
049              (unresolved_decl_ref_expr type='<null>' name=UIColor specialized=no))
050            (tuple_expr type='<null>')))
051        (call_expr type='<null>'
052          (unresolved_dot_expr type='<null>' field 'makeKeyAndVisible'
053            (force_value_expr type='<null>'
054              (unresolved_dot_expr type='<null>' field 'window'
055                (declref_expr type='<null>' decl=AppDelegate.(file).AppDelegate.func decl.self@AppDelegate.swift:17:10 specialized=yes))))
056          (tuple_expr type='<null>'))
057        (return_stmt
058          (unresolved_decl_ref_expr type='<null>' name=true specialized=no))))
059    (func_decl "applicationWillResignActive(_:)" type='<null type>'
060      (body_params
061        (pattern_typed implicit
062          (pattern_named implicit 'self'))
063        (pattern_tuple
064          (pattern_typed
065            (pattern_named 'application')
066            (type_ident
067              (component id='UIApplication' bind=none)))))
068      (brace_stmt))
069    (func_decl "applicationDidEnterBackground(_:)" type='<null type>'
070      (body_params
071        (pattern_typed implicit
072          (pattern_named implicit 'self'))
073        (pattern_tuple
074          (pattern_typed
075            (pattern_named 'application')
076            (type_ident
077              (component id='UIApplication' bind=none)))))
078      (brace_stmt))
079    (func_decl "applicationWillEnterForeground(_:)" type='<null type>'
080      (body_params
081        (pattern_typed implicit
082          (pattern_named implicit 'self'))
083        (pattern_tuple
084          (pattern_typed
085            (pattern_named 'application')
086            (type_ident
087              (component id='UIApplication' bind=none)))))
088      (brace_stmt))
089    (func_decl "applicationDidBecomeActive(_:)" type='<null type>'
090      (body_params
091        (pattern_typed implicit
092          (pattern_named implicit 'self'))
093        (pattern_tuple
094          (pattern_typed
095            (pattern_named 'application')
096            (type_ident
097              (component id='UIApplication' bind=none)))))
098      (brace_stmt))
099    (func_decl "applicationWillTerminate(_:)" type='<null type>'
100      (body_params
101        (pattern_typed implicit
102          (pattern_named implicit 'self'))
103        (pattern_tuple
104          (pattern_typed
105            (pattern_named 'application')
106            (type_ident
107              (component id='UIApplication' bind=none)))))
108      (brace_stmt))))
由于不了解编译器相关的知识,无法解析上述输出,但是看起来很像common lisp 的“点对”数据结构。



另外,我们会注意到另一个比较重要的命令行参数:-repl,我们一起玩玩,看看这个工具能力怎么样。

在命令行执行如下命令:

1xcrun swift -repl
可以得到一个脚本解析器的执行环境:


我们参考“The Swift Programming Language”写一个Hello World,看看效果:

可以使用:Ctrl + D退出解析器。

这里可以得到:

1、swift既可以被编译执行,也可以被解析执行,是真正的动态语言。

2、大家可以用这个工具来学习语言特性。

 

就分析到这里,希望对大家有帮助。


原文地址http://blog.csdn.net/proteas/article/details/28439601


您还未登录,请先登录

热门帖子

最新帖子