Swift教程 2014苹果首发

所需积分/C币:6 2014-06-04 3.67MB PDF
评分

WWDC Worldwide Developers Conference 2014苹果开发者大会 北京时间今天1点 3点 苹果带来了全新的编程语言 Swift 不像此前语言那样 有多种复杂的符号和表达式 将比之前的Object C更加快速 便利 高效 安全 除此之外 新的Swift语言依旧和C与Object C兼容 支持实时的代码效果预览 大大降低开发难度
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 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 dont understand something-every thing introduced in this tour is explained in detail in the rest of this book NOTE 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 var myVariable =42 2 myVariable=50 3 let my Constant =42 a constant or variable must have the same ty pe as the value you want to assign to it. However, you don t always have to write the ty pe explicitly. Providing a value when you create a constant or variable lets the compiler infer its ty pe. In the example above, the compiler infers that my Variab le is an integer because its initial value is a integer. If the initial value doesnt provide enough information(or if there is no initial value), specity the ty pe by writing it after the variable, separated by a colon. 1 let implicitInteger =70 2 Let implicitDouble =70.0 3 let explicitDouble: Double =70 EXPERIMENT 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 let label = "The width is 2 let width 94 3 let widthLabel= label String(width) EXPERIMENT 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 let apples =3 2 let oranges =5 3 let app Le Summary ="I have \(apples)apples 4 let fruitSummary ="I have \(apples oranges) pieces of fruit. EXPERIMENT Use \ () to include a floating- point calcu lation in a string and to include someone s name in a greeting Create arrays and dictionaries us ing brackets([]), and access their elements by writing the index or key in brackets 1 var shoppingList =["catfish",water",tulips","blue paint"] 2 shoppingList [1] ="bottle of water 4 var occupations Malcolm" :"Captain", Kay lee:"Mechanic", 8 occupations ["Jayne"]="Public Relations To create an empty array or dictionary, use the initializer syntax let emptyArray String[]() 2 Let emptyDictionary Dictionary<String, Float>() If type information can be inferred, you can write an empty array as [I and an empty dictionary as [ ]-for example, when you set a new value for a variable or pass an argument to a function shoppingList =[]// Went shopping and bought everything Control flow Use if and switch to make conditionals, and use for-in, for, while, and do-while to make loops. Parentheses around the condition or loop variable are optional. Braces around the body are required 1 Let individualScores =[75, 43, 103,87, 121 2 var teamScore =0 3 for score in individualScores i 4 if score >50 f 5 teamscore +=3 6 f else teamscore +=1 10 teamScore In an if statement, the conditional must be a Boolean expression -this means that code such as if score t t is an error, not an implicit comparison to zero You can use if and let together to work with values that might be mis s ing these values are represented as optionals. An optional value either contains a value or contains nil to indicate that the value is missing. Write a question mark(? )after the type of a value to mark the value as optional 1 var optionalString: String?="Hellot 2 optionalString = nil 4 var optiona LName: String? ="John Appleseed" 5 var greeting ="Hello! if let name optiona lName t 678 greeting =Hello, \(name) EXPERIMENT Change optiona IName to nil. What greeting do you get? Add an else clause that sets a d ifferent greeting if optiona UName is niL. If the optional value is nil, the conditional is false and the code in braces is skipped. otherwise, the optional value is unwrapped and assigned to the constant after let, which makes the unwrapped value available inside the block of code Switches support any kind of data and a wide variety of comparison operations-they arent limited to integers and tests for equality 1 let vegetable ="red pepper switch vegetable t 3 case" ce lery let vegetableComment ="Add some raisins and make ants on a log 5 case " cucumber","watercress": 6 let vegetab leComment ="That would make a good tea sandwich 7 case let x where x has Suffix("pepper"): 8 let vegetableComment ="Is it a spicy \(x)?" 9 default 10 let vegetab leComment =Everything tastes good in soup EXPERIMENT Try removing the default case. What error do you get? After executing the code inside the switch case that matched, the program ex its from the switch statement Execution doesnt continue to the next case, so there is no need to explicitly break out of the switch at the end of each case's code You use for-in to iterate over items in a dictionary by providing a pair of names to use for each key-value pair 1 let interestingNumbers = " Prime":[2,3,5,7,11,13] 2345 " Fibonacci":[1,1,2,3,5,81, Square":[1,4,9,16,25], 6 var largest =0 for (kind, numbers) in interestingNumbers t for number in numbers f if number Largest t 10 largest number 11 } 12 13} 14 Largest EⅩPER|MENT Add another variable to keep track of which kind of number was the largest, as well as what that largest number was Use while to repeat a block of code until a condition changes. the condition of a loop can be at the end instead ensuring that the loop is run at least once 1 var n=2 2 while n 100 t n=n*2 23456789 var m 8 do i 10 while m 100 11m You can keep an index in a loop-either by using. to make a range of indexes or by writing an explicit initialization condition, and increment. These two loops do the same thing 1 var firstForLoop =0 2 for i in 0..3 i 3 firstForLoop + 5 firstForLoop 6 7 var second ForLoop =0 8 for var i=0; 1<3; ++i i 9 second ForLoop +=1 10 11 secondForLoop Use. to make a range that omits its upper value and use .. to make a range that includes both values Functions and closures Use func to declare a function. Call a function by following its name with a list of arguments in parentheses. Use to separate the parameter names and types from the functions return type 1 func greet(name: String, day: String)-> String t return Hello \(name), today is \(day) 4 greet("Bob","Tuesday") EXPERIMENT Remove the day parameter. Add a parameter to include today's lunch special in the greeting Use a tuple to return multiple values from a function 1 func getGasPrices()->(Double, Double, Double)i return(3.59,3,69,3,79 234 getGasPrices() Functions can also take a variable number of arguments, collecting them into an array 1 func sumOf(numbers: Int.)- Int I var sum三 3 for number in numbers i sum + number 5 return sum 8 sumOf() 9sumf(42,597,12) EⅩPER|MENT Write a function that calculates the average of its arguments Functions can be nested nested functions have access to variables that were declared in the outer function you can use nested functions to organize the code in a function that is long or complex 1 func returnFifteen()-> Int i var y= 10 func addo i 4 6 add() return 8} 9 returnFifteen() Functions are a first-class type. this means that a function can return another function as its value 1 func makeIncrementer()->(Int - Int)i func addOne (number: Int)-> Int t return 1+ number 456 return addone 7 var increment makeIncrementer() 8 increment(7) A function can take another function as one of its arguments func hasAnyMatches(list: Int[, condition: Int - Bool)-> Bool t for item in list i if condition(item)i return true return false 9 func less ThanTen(number: Int) 10 return number 10 11} 12 var numbers=[20,19,7,12] 13 hasAnyMatches(numbers, lessThanTen Functions are actually a special case of closures. You can write a closure without a name by surrounding code with braces ((). Use in to separate the arguments and return ty pe from the body 1 numbers, map(i (number: Int)-> Int in let result =3 x number return result }) EXPERIMENT Rewrite the closure to return zero for all odd numbers You have several options for writing closures more concisely. When a closure's type is already known, such as the callback for a delegate, you can omit the ty pe of its parameters, its return type, or both Single statement closures implicitly return the value of their only statement numbers. map(i number in 3 number You can refer to parameters by number instead of by name-this approach is especially useful in very short closures. a closure passed as the last argument to a function can appear immediately after the parentheses sort([1,5,3,12,21){$0>$1} Objects and classes Use class followed by the classs name to create a class. a property declaration in a class is written the same way as a constant or variable declaration, except that it is in the context of a class. Likewise, method and function declarations are written the same way class Shape t 1234 var numberofSides 0 func simp leDescription ()-> String i return" A shape with \(numberofSides) sides EXPERIMENT Add a constant property with let, and add another method that takes an argument Create an instance of a class by putting parentheses after the class name. Use dot syntax to access the properties and methods of the instance 1 var shape Shape()

...展开详情
立即下载 最低0.43元/次 身份认证VIP会员低至7折
举报 举报 收藏 收藏
分享
124B
Swift入门视频教程

教程名称: Swift入门视频教程0.Swift简介 开始学习 01.Swift基本语法 开始学习 02.Swift数据类型 开始学习 03.Swift变量 开始学习 04.Swift可选类型 开始学习 05.Swift常量 运算符 开始学习 06.Swift流程控制语句 开始学习 07.Swift字符串操作 开始学习 08.Swift数组 开始学习 09.Swift集合 开始 资源太大,传百度网盘了,链接在附件中,有需要的同学自取。

2019-07-23
125B
Swift教程大合集(文档视频)

资源名称:Swift教程大合集(文档 视频)资源目录:【】00Swift开发入门【】Swift教程-代码【】The Swift Programming Language中文完整精校版【】TheSwiftProgrammingLanguage【】【Swift教程】01Swift简介(文件缺失)【】【Swift教程】02HelloWorld、 资源太大,传百度网盘了,链接在附件中,有需要的同学自取。

2019-07-23
4.86MB
Swift中文教程 PDF

Swift 是一种新的编程语言,用于编写 iOS 和 OS X 应用。Swift 结合了 C 和 Objective-C 的优点并且不受 C 兼容性的限制。 Swift 采用安全的编程模式并添加了很多新特性,这将使编程更简单,更灵活,也更有趣。Swift 是基于成熟而且倍受喜爱的 Cocoa 和 Cocoa Touch 框架,它的降临将重新定义软件开发。 Swift 的开发从很久之前就开始了。为了给 Swift 打好基础,苹果公司改进了编译器,调试器和框架结构。我们使用自动引用 计数(Automatic Reference Counting, ARC)来简化内存管理。我们在 Foundatio

2014-06-23
67B
黑马Swift视频教程.txt

黑马Swift视频教程,我认为讲的最好的,虽然不是最新的,但差别不大。

2019-08-31
1.11MB
Swift语言官方教程【最新中文版】

Swift语言官方教程【最新中文版】,最新版!!非常不错,建议大家下载看看!

2018-09-08
130KB
Swift视频教程配套代码

[全国首发]Swift视频教程 -(视频持续更新) http://blog.csdn.net/jaywon/article/details/29355253

2014-06-12
48B
Swift语言精讲视频教程全套39G

C语言与OC语言,想要学习Swift语言的程序猿们赶紧看过来吧! 这套Swift语言视频教程是针对有Objective-C/iOS基础,想在iOS开发行业发展的互联网爱好者,或者想从事Swift开发的程序猿们而录制的,视频中从Swift编程语言简介、语法、结构体、面向对象、继承等,到Swift作为iOS开发的UI,地图,网络进行一系列讲解。39G的资料亮瞎你眼!

2015-06-10
264KB
Swift 5.1 官方教程.pdf

根据Swift官网上的官方教程翻译,加上了一些个人理解和一些注释。目前版本为5.1。陆续即将推出其他官网文档翻译版。

2020-02-12
123B
千锋教育《Swift学习语言》视频教程

    本专题由千锋资深iOS金牌讲师张老师倾情录制,注重Swfit基础语法的讲解,即使你没有iOS开发的基础、只有其它编程语言的基础,同样可以打开Swift语言编程iOS的大门。本视频主要讲解的内容有:变量和常量、基本数据类型、类型推断和类型安全、类型转换、Bool类型、元组(tuples)、可选值、隐式解析可选等内容。教程目录:【】Swift语言简介【】千锋iOS培训Swi 资源太大,传百度网盘了,链接在附件中,有需要的同学自取。

2019-07-23
2.61MB
Swift官方教程中文版

最新官方文档中文翻译,带书签,针对新手,或是英文不好的童鞋,绝对物有所值

2018-10-19
204KB
Swift4.0 视频教程(2018-raywenderlich)

2018年国外知名网站raywenderlich最新Swift4视频教程(想要完整版可以留言邮箱)

2018-01-29
6.76MB
swift项目开发基础教程

swift项目开发基础教程整本书代码,及课件学习资源。里面分别有蛇棋游戏,2048小游戏项目完整源码。

2018-06-17
1.8MB
swift 3d 入门教程

为了让大家快速了解SWIFT 3D的一些概念,我们先做一个简单的文字转到效果,我将一步步教你怎么做,请跟着做...............

2011-07-30
46B
最新Swift语言精讲视频教程全套.txt

最新Swift语言精讲视频教程全套,百度网盘地址

2015-06-11
10.96MB
精通 Swift 4 (Mastering Swift 4.0)

本书 精通 Swift 4 (Mastering Swift 4.0)是 非扫描版PDF 作者是 Jon Hoffman

2017-10-17
9.3MB
Swift 4入门教程Swift Apprentice

Beginning programming with Swift 4. This is a book for complete beginners to Apple’s brand new programming language — Swift 4.

2018-03-02
6.5MB
Swift5编程指南.pdf

《Swift 编程语言》是苹果官方对 Swift 语言做的权威指南,很遗憾苹果公司并没有进行多语言支持。该文档为Swift5 编程语言官方文档中文翻译版本。

2019-10-16
177.78MB
swift ios开发最新教程

swift ios开发最新教程,里面包括了ios11\ios10两本教程,需要一定的swift语言基础,没有swift语言基础的同学,可以在我的资源中下载swift语言学习教程资料

2018-05-18
71B
小码哥Swift5.0视频教程(非加密)

是百度网盘地址,1-22节课 1. 基本运算、流程控制、函数.mp4 2. 字符、字符串、枚举、集合、闭包】.mp4 3. 结构体、类、继承、属性、方法.mp4 4. init、deinit、extension.mp4 5. protocol、访问权限控制、反射.mp4 ... 21. 面向协议编程、响应式编程.mp4 22. 标准库源码分析、项目实战.mp4

2020-04-16
11.43MB
果官方swift教程

swift官方教程(CocoaChina翻译)

2015-11-08
img
喜狼狼9527

关注 私信 TA的资源

上传资源赚积分,得勋章
相关内容推荐