博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Swift Basic
阅读量:7015 次
发布时间:2019-06-28

本文共 2711 字,大约阅读时间需要 9 分钟。

  hot3.png

推荐使用Xcode6 playground的功能来测试,可以很方便看到输出

不需要分号

println("Hello, world!")

简单的赋值方式

使用 let 标记一个常量   var 标记一个变量

var myVariable = 42myVariable = 50let myConstant = 42

追加声明数据类型

let implicitInteger = 70let implicitDouble = 70.0let explicitDouble: Double = 70

变量之间可以方便的转换类型

let label = "The width is "let width = 94let widthLabel = label + String(width)

以 \(变量名) 的形式可以很方便的无视类型转换到String输出

let apples = 3let oranges = 5let appleSummary = "I have \(apples) apples."let fruitSummary = "I have \(apples + oranges) pieces of fruit."

创建数组,字典

var shoppingList = ["catfish", "water", "tulips", "blue paint"]shoppingList[1] = "bottle of water" var occupations = [    "Malcolm": "Captain",    "Kaylee": "Mechanic",]occupations["Jayne"] = "Public Relations"

初始化数组,字典

let emptyArray = [String]()let emptyDictionary = [String: Float]()

流程控制 

if , switch 做流程选择 

for-in, for, while, do-while 做循环控制

let individualScores = [75, 43, 103, 87, 12]var teamScore = 0for score in individualScores {    if score > 50 {        teamScore += 3    } else {        teamScore += 1    }}teamScore

好来分析一下下面的代码

//使用if和let来处理可能不存在的值//在数据类型后面加?来标记可能不存在的值var optionalString: String? = "Hello"optionalString == nil//如果optinalName 为nil 则执行else var optionalName: String? = "John Appleseed"var greeting = "Hello!"if let name = optionalName {    greeting = "Hello, \(name)"}else{greeting="The value is nil"}
//定义字典let interestingNumbers = [    "Prime": [2, 3, 5, 7, 11, 13],    "Fibonacci": [1, 1, 2, 3, 5, 8],    "Square": [1, 4, 9, 16, 25],]var largest = 0for (kind, numbers) in interestingNumbers {    //取出字典里面的数字    for number in numbers {        //挑选出最大的值        if number > largest {            largest = number        }    }}largest         //结果为25
  • for i in 0..<4 

  • for var i = 0; i < 4; ++i 

上面两个语句是等效的

------------------------------------------------

函数

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 function’s return type.

使用func定义一个函数

使用->设置函数返回数据类型

//传入参数2个String,返回一个Stringfunc greet(name: String, day: String) -> String {    return "Hello \(name), today is \(day)."}//调用函数greet("Li", "SUNDAY")
//传入参数是一个Int数组,函数功能算出这个数组的最大 最小 和值 func calculateStatistics(scores: [Int]) -> (min: Int, max: Int, sum: Int) {        //初始化,将最小值,最大值定义到Int数组第一个值    var min = scores[0]    var max = scores[0]    var sum = 0      //遍历数组    for score in scores {        if score > max {            max = score        } else if score < min {            min = score        }        sum += score    }       return (min, max, sum)}//调用函数  let statistics = calculateStatistics([5, 3, 100, 3, 9])statistics.sumstatistics.2

转载于:https://my.oschina.net/arunu/blog/310011

你可能感兴趣的文章
分布式搜索Elasticsearch——QueryBuilders.matchPhrasePrefixQuery
查看>>
课程2:《黑马程序员_Java基础视频-深入浅出精华版》-视频列表-
查看>>
TP4056大电流1A使用注意事项
查看>>
java代理模式之静态代理
查看>>
ASP.NET MVC5+EF6+EasyUI 后台管理系统(80)-自由桌面
查看>>
Java常考面试题(四)
查看>>
前端学数据库之记录操作
查看>>
学习Javascript闭包(Closure)
查看>>
【Todo】git的fast forward & git命令学习 & no-ff
查看>>
如何远程关闭一个ASP.NET Core应用?
查看>>
共享内存mmap学习 及与 shmxxx操作的区别
查看>>
你性格那么软,总是经常改变想法
查看>>
Summary of Critical and Exploitable iOS Vulnerabilities in 2016
查看>>
NeHe OpenGL教程 第十七课:2D图像文字
查看>>
查看Linux系统版本与位数
查看>>
更换CentOS7的下载源为阿里云
查看>>
退出Android程序时清除所有activity的实现方法
查看>>
eclipse注释模板设置(未整理)
查看>>
Hibernate框架 主配置文件(Hibernate.cfg.xml)基本
查看>>
学习SpringMVC——从HelloWorld开始
查看>>