The Swift Programming Language3

常量和变量的命名(Naming Constants and Variables)

 

你能够使用几乎所有的你所喜欢的字符来命名常量和变量,包括一些特别的Unicode字符

 

常量和变量都不能包含数学符号,箭头,私用的(非法的)Unicode 点,划线,划筐(line- and box-drawing)字符。不能够以数字开头,不过数字可以包含在名字的其他位置

 

只要你声明了一个变量或者常量为一个特定的类型,就不能在重新声明相同的名字的变量或者常量。也不能用它来存储别的数据类型的数据,也不能把一个常量变成一个变量或者反过来把变量变成常量

 

注意

如果你的常量或者变量需要使用与Swift 保留关键字相同的名字,你可以在关键字后面添加(‘)标记,不过除非万不得已,否则还是尽量不要这样用。

 

只要类型能够兼容,你就能够改变相应的变量的值,下面的例子把friendlyWelcome的值从”Hello!”改变为”Bonjour!”:

 

var friendlyWelcome = “Hello!”

friendlyWelcome = “Bonjour!”

//friendlyWelcome is now “Bonjour!”

 

不像变量,常量的值一旦设置就不能改变,如果你企图改变常量的值,将会编译器报错,如下:

let languageName = “Swift”

languageName = “Swift++”

// this is a compile-time error – languageNamecannot be changed

 

常量和变量的打印(Printing Constants andVariables)

 

可以使用println 函数打印常量或者变量的当前的值,如下:

println(friendlyWelcome)

//prints “Bonjour!”

 

println 是用来打印值的一个全局函数,为了合适的输出效果,后面跟有一个换行符。如果你正在使用Xcode工作,println 把内容输出到Xcode的控制台(另一个相关的函数是print,唯一区别是,后面没有跟有换行符)

 

println 函数打印传给它的任何字符串,如下:

println(“This is a string”);

// prints “This is a string”

 

println 函数能够打印更复杂的日志信息,就像Cocoa’s 的NSLog函数一样。打印的内容包括常量和变量的当前的值。

 

 

Swift在比较长的字符串中使用字符串占位符方式来引入常量或者变量的值,Swift会使用当前的常量或者变量的值来替换这个占位符。使用方式是把占位符写在括号里面,前面增加反斜杠,如下:

 

println(“The current value of friendlyWelcome is\(friendlyWelcome)”)

//prints “The current value of friendlyWelcome isBonjour!”

 

所有关于字符串插入的操作在 String Interplation 这部分有描述

 

注释(Comments)

在我们的代码中,使用注释来包括一些不执行的文本信息,给我们自己一个提示或者提醒.当我们的代码被编译时,注释被Swift编译器忽略掉

 

在Swift中的注释非常相似于C语言中的注释,单行注释用两个斜杠开始(//)

 

//this is a Comment

 

我们也能写多行注释,以斜杠星(/*)开始,以星斜杠(*/)结束。如下

/* this is also a comment,

but written over multiple lines */

 

 

不像C(//Java,OC,C++)语言中的多行注释,在Swift 中的多行注释能够进行嵌套,嵌套的注释在父注释块开始后开始,在父注释块结束前结束,如下:

 

/* this is the start of the first multiline comment

/* this is the second ,nested multiline comment */

this is the end of the fitst multiline comment */

 

内嵌的多行注释使我们能够快速,容易地对大段代码块进行注释,即使这段代码块已经包含了多行注释。

 

分号(Semicolons)

 

不像大多数的编程语言,Swift 不需要在每条语句后面写上分号(;),如果想加上分号也是可以的。不过,如果一行有多条语句代码的话,需要使用分号来分割。

如下:


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iOS 9 Swift Programming Cookbook 电子书,英文版 Description: With iOS 9 SDK during WWDC 2015, Apple introduced some very big changes, including the introduction of watchOS 2 which really changed how we interact, as programmers, with the ?Watch. Previously, almost all requests had to go to the companion iOS device from the watch and then served by the iOS app. With watchOS 2, the watch takes a lot more responsibilities and can update its content almost always by itself. Among the new tools in iOS 9, there is a new UI Testing framework and the ability to write UI tests purely in Swift. iOS 9 Swift Programming Cookbook has a whole chapter dedicated to this subject so that you can get started with UI Testing in no time. The philosophy of this edition of the book was to write content that is only relevant to iOS 9. The author did not reuse any material from the previous editions of the book, meaning that this book is entirely about iOS 9 subjects, including but not limited to the followings: Apple watchOS 2: so that you can create complications and standalone ?Watch apps UI Testing: using the new UI testing frameworks in iOS 9 SDK, written in pure Swift Extensions: learn how to create new Safari Content Blockers to block advertisements or obtrusive content Contacts: iOS 9 introduced two new frameworks that allows Swift programmers to use native Swift syntax to interface with the contacts database on the iOS device UI Dynamics: allowing programmers to create some dynamic effects on their UI, including radial gravity fields Multitasking: with this edition of the cookbook, you will learn how to create Picture in Picture functionalities in your app Swift 2, Xcode 7 and Interface Builder: the whole book is based on the awesome new features of Swift 2, Xcode 7 and Interface Builder
Beginning Swift: Master the fundamentals of programming in Swift 4 pdf Master the fundamentals of programming in Swift 4 Key Features Covers theory and practice in equal parts Teaches you how to correctly structure and architect software using Swift Uses real-world examples to connect the theory to a professional setting Imparts expertise in the core Swift standard library Book Description Take your first foray into programming for Apple devices with Swift. Swift is fundamentally different from Objective-C, as it is a protocol-oriented language. While you can still write normal object-oriented code in Swift, it requires a new way of thinking to take advantage of its powerful features and a solid understanding of the basics to become productive. This course helps you develop client-side and server-side applications, as well as web services using Swift. We'll begin with exploring the fundamental Swift programming concepts, language structure, and the Swift programming syntax. Then, we'll learn to create original custom operators with Swift operators, branching, and loops. Moving on, we'll learn how to run application codes and compile errors. Having made progress with it, we'll see how Swift compares to other computer languages and how to transform your thinking. Then, master the usage of key language elements, such as strings and collections. Finally, grasp how Swift supports modern application development using advanced features, such as built-in Unicode support and higher-order functions. This is an introductory course to the Swift programming language with Xcode.After completing this course, students will be well-prepared to begin developing native end-user applications for iOS or macOS, or to develop server-side (back-end) application and web services using Swift on Linux. What you will learn Explore the fundamental Swift programming concepts, language structure, and the Swift programming syntax Learn how Swift compares to other computer languages and how to transform your thinking to leverage new concepts such as optionals and protocols Master how to use key language elements, such as strings and collections Grasp how Swift supports modern application development using advanced features, such as built-in Unicode support and higher-order functions. Who this book is for If you are seeking fundamental Swift programming skills, in preparation for learning to develop native applications for iOS or macOS, this book is the best for you. You don’t need to have any prior Swift knowledge; however, object-oriented programming experience is desired. Lesson 1, Swift Basics, covers the fundamentals of using the Swift programming language. In this lesson, you'll learn basic Swift syntax and program structure. You'll also learn how to use Swift built-in data types and enums, and how to declare and use Swift variables and constants. Lesson 2, Swift Operators and Control Flow, shows you how to use the fundamental flow control structures and language elements that form the building blocks of Swift programs. We will specifically cover operators, branching statements, and loops in this lesson. Lesson 3, Functions, Classes, and Structs, teaches you how to develop fully featured Swift functions, catch unexpected errors, and use asynchronous programming paradigms. You'll learn how to create your own data types, and create object-oriented applications using classes and structs. Lesson 4, Collections, shows you how to work with Swift's collections, such as arrays, sets, and dictionaries. Lesson 5, Strings, covers Swift strings in detail. We will create and use strings and substrings, and see the various common operations available for strings. Lesson 6, Functional Programming and Lazy Operations, ventures at functional programming and explains what lazy operations are. We will end this lesson with an important but often overlooked topic— writing Swifty code.
提供的源码资源涵盖了小程序应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值