没有合适的资源?快使用搜索试试~ 我知道了~
The Swift Programming Language - Apple Inc
需积分: 9 0 下载量 150 浏览量
2014-06-15
14:42:09
上传
评论
收藏 4.67MB PDF 举报
温馨提示
swift语言,英文版 Swift,一种编译式编程语言,由苹果公司推出,用来撰写OS X和iOS应用程序。2014年,在AppleWWDC所发布,设计Swift时,苹果公司有意让Swift与Objective-C共存在苹果公司的操作系统上。 Swift取消了Objective C的指针及其他不安全访问的使用,并舍弃Objective C早期套用Smalltalk之语法,全面改为句点表示法(dot-notation)。同许多script语言一样,Swift可以推断变量类型(var, variant)。同时,它提供了类似Java的命名空间(namespace)、泛型(generic)、运算对象重载(operator overloading)。Swift被简单的形容为 “没有C的Objective-C”(Objective-C without the C)
资源推荐
资源详情
资源评论
Welcome to Swift
2
About Swift
Swift is a new programming language for iOS and OS X apps that builds on the best of C
and Objective-C, without the constraints of C compatibility. Swift adopts safe programming
patterns and adds modern features to make programming easier, more flexible, and more
fun. Swift’s clean slate, backed by the mature and much-loved Cocoa and Cocoa Touch
frameworks, is an opportunity to reimagine how software development works.
Swift has been years in the making. Apple laid the foundation for Swift by advancing our
existing compiler, debugger, and framework infrastructure. We simplified memory
management with Automatic Reference Counting (ARC). Our framework stack, built on the
solid base of Foundation and Cocoa, has been modernized and standardized throughout.
Objective-C itself has evolved to support blocks, collection literals, and modules, enabling
framework adoption of modern language technologies without disruption. Thanks to this
groundwork, we can now introduce a new language for the future of Apple software
development.
Swift feels familiar to Objective-C developers. It adopts the readability of Objective-C’s
named parameters and the power of Objective-C’s dynamic object model. It provides
seamless access to existing Cocoa frameworks and mix-and-match interoperability with
Objective-C code. Building from this common ground, Swift introduces many new features
and unifies the procedural and object-oriented portions of the language.
Swift is friendly to new programmers. It is the first industrial-quality systems programming
language that is as expressive and enjoyable as a scripting language. It supports
playgrounds, an innovative feature that allows programmers to experiment with Swift code
and see the results immediately, without the overhead of building and running an app.
Swift combines the best in modern language thinking with wisdom from the wider Apple
engineering culture. The compiler is optimized for performance, and the language is
optimized for development, without compromising on either. It’s designed to scale from
“hello, world” to an entire operating system. All this makes Swift a sound future investment
for developers and for Apple.
Swift is a fantastic way to write iOS and OS X apps, and will continue to evolve with new
features and capabilities. Our goals for Swift are ambitious. We can’t wait to see what you
create with it.
3
A Swift Tour
Tradition suggests that the first program in a new language should print the words “Hello,
world” on the screen. In Swift, this can be done in a single line:
1 println("Hello,world")
If you have written code in C or Objective-C, this syntax looks familiar to you—in Swift, this
line of code is a complete program. You don’t need to import a separate library for
functionality like input/output or string handling. Code written at global scope is used as the
entry point for the program, so you don’t need a main function. You also don’t need to write
semicolons at the end of every statement.
This tour gives you enough information to start writing code in Swift by showing you how to
accomplish a variety of programming tasks. Don’t worry if you don’t understand something
—everything introduced in this tour is explained in detail in the rest of this book.
N O T E
For the best experience, open this chapter as a playground in Xcode. Playgrounds allow you to edit the
code listings and see the result immediately.
Simple Values
Use let to make a constant and var to make a variable. The value of a constant doesn’t
need to be known at compile time, but you must assign it a value exactly once. This means
you can use constants to name a value that you determine once but use in many places.
1 varmyVariable=42
2 myVariable=50
3 letmyConstant=42
A constant or variable must have the same type as the value you want to assign to it.
However, you don’t always have to write the type explicitly. Providing a value when you
create a constant or variable lets the compiler infer its type. In the example above, the
compiler infers that myVariable is an integer because its initial value is a integer.
If the initial value doesn’t provide enough information (or if there is no initial value), specify
4
the type by writing it after the variable, separated by a colon.
1 letimplicitInteger=70
2 letimplicitDouble=70.0
3 letexplicitDouble:Double=70
E X P E R I M E N T
Create a constant with an explicit type of Float and a value of 4.
Values are never implicitly converted to another type. If you need to convert a value to a
different type, explicitly make an instance of the desired type.
1 letlabel="Thewidthis"
2 letwidth=94
3 letwidthLabel=label+String(width)
E X P E R I M E N T
Try removing the conversion to String from the last line. What error do you get?
There’s an even simpler way to include values in strings: Write the value in parentheses,
and write a backslash (\) before the parentheses. For example:
1 letapples=3
2 letoranges=5
3 letappleSummary="Ihave\(apples)apples."
4 letfruitSummary="Ihave\(apples+oranges)piecesoffruit."
E X P E R I M E N T
Use \() to include a floating-point calculation in a string and to include someone’s name in a greeting.
5
剩余447页未读,继续阅读
资源评论
like969570377
- 粉丝: 1
- 资源: 37
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- js-leetcode题解之158-read-n-characters-given-read4-ii-call
- js-leetcode题解之157-read-n-characters-given-read4.js
- js-leetcode题解之156-binary-tree-upside-down.js
- js-leetcode题解之155-min-stack.js
- js-leetcode题解之154-find-minimum-in-rotated-sorted-array-ii.js
- js-leetcode题解之153-find-minimum-in-rotated-sorted-array.js
- js-leetcode题解之152-maximum-product-subarray.js
- js-leetcode题解之151-reverse-words-in-a-string.js
- js-leetcode题解之150-evaluate-reverse-polish-notation.js
- js-leetcode题解之149-max-points-on-a-line.js
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功