IOS8 开发之Swift - 自学之路(第二天)

1.1Converting value

Swift 不会自动类型转换,要想类型转换必须用Int(),Double,String() etc.

var quantity = 42
var unitPrice = 34.55
println("The amount is \(Double(quantity)*unitPrice)")
__________________________________

1.2Basic if statements

Swift在条件判断中不需要小括号,只判断true还是false

1.3switch syntax

switch没有case穿透现象,在每个case中没有break关键字。在default中可以有break。

1)Must be exhaustive

2)Can provide ranges of values

3)No implicit fallthrough - code required in all cases

let windSpeed = 5
switch windSpeed {
    case 0...3:
         println("It's very cala.")
    case 4...6:
         println("A little windy.")
    case 7...9:
         println("Blowing a gale!")
    case 10...12:
         println("Batten down the hatches!")
    default:
         break;
}
__________________________________
 

1.4 loops in Swift

...closed range operator

0...100    36...99

..< half-open range operator

var total = 0
for index in 1...100 {
    total = total + index
}
total

//using for-in loops with Stings
var name = "Bob"

for eachChar in name {
    println(eachChar)
}

1.5Simple Swift function

:means "is a type of"

传入的参数是常量

func myFunction(name : String = "Jason Wang") {
    println("Hello,\(name)")
}
myFunction("Jane") //ERROR - this will no longer work

myFunction() //OK - use default value
myFunction(name:"Jane") //OK - provide named argument
_______________________________

fuc add(a : Int = 10, b : Int = 50) {
    println("The result is \(a*b)")
}

add(99) // ?compile error
add(a:99) //OK
add(b:200) //OK
add(a:99,b:200) //OK

1.6Arrays in Swift

1)Arrays are zero-based

2)Arrays are typed

3)Mutable when created with var,  inMutable when created with let

var flavors : [String]

//adding to the end of an array
flavors.append("Neaplolitan")
flavors += ["Wintergreen"]

//insert at specific position
flavors.insert("Coconut",atIndex: 3)

//removing items
flavors.removeLast()
flavors.removeAtIndex(3) 

//.count for number of items
println("The array has \(daysInMonth.count) items")

if daysInMonth.isEmpty {
    println("There's nothing in the array.")
}

1.7Dictionaries in Swift

1)AKA Associative Array, Map, Hashtable

var states = ["AZ":"Arizona"]
//Declare dictionary of Int keys and String values
var products : [Int:String]

//Accessing dictionary values
println(states["AZ"])
//Updating or inserting
states["FL"] = "Florida"//will change OR insert
states.updateValue("Nevada", forKey:"NV")
//this return any existing value before updating it
<pre name="code" class="objc">states.updateValue("Nevada", forKey:"AZ")

//to delete key/value pair

states["FL"] = nil

states.removeValueForKey("AZ")

println("There are \(states.count) states left.")

for(abbrev, fullname) in states {

println("\(abbrev) is shor for \(fullname)")

}


1.8tuples in Swift

1)A collectio of elements

var str = "Hello"
let num = 1000

var myTuple = (str, num)
var myOtherTuple = (str, num, 12345, "Some text")

var status = ["AZ":"Arizona","CA":"California"] 

______________________________________________

//returning a tuple
func getCurrentSongAndDuration() -> (name:String, length:Int) {
    return("NoonLight in Vernont",210)
}

//call funcation
let result = getCurrentSongAndDuration()
//decomposing - option 1
println("The song is \(result.name) and it's \(result.length) seconds long")
_____________________________________________________________
<pre name="code" class="objc">func getCurrentSongAndDuration() -> (String, Int) {
    return("NoonLight in Vernont",210)
}

//option2let(name, length) = getCurrentSongAndDuration()
println("The song is \(name) and it's \(length) seconds long") 

时间: 2024-10-12 17:27:59

IOS8 开发之Swift - 自学之路(第二天)的相关文章

IOS8 开发之Swift - 自学之路(第一天)

1.1What is Swift "Swift is an innovative new programming language for Cocoa and Cocoa Touch. Writing code is interactive and fun, the syntax is concise yet expressive, and apps run lightning-fast. Swift is ready for your next iOS and OS X project - o

IOS8 开发之Swift - 自学之路(第三天)

字符串类型 countElements()计算字符串长度. 比较字符串相等, ==. 字符串判空.isEmpty 字符串转换成int.toInt() 可选类型 int? ! nil 数组类型 shoppingList.insert("abc",atIndex:0) shoppingList.append("abc","bac") shoppingList.removeAtIndex(0) //返回删除值 shoppingList.count//数

ios开发之Swift标签栏按钮UITabBarItem样式修改(图标文字尺寸,颜色等)

UITabBarItem在开发中非常的常用了它可以用来设置字体颜色及图片的设置等等,下面我们来看一篇关于ios开发之Swift标签栏按钮UITabBarItem样式修改(图标文字尺寸,颜色等)的例子,具体的细节如下所示. 1,tabBarItem图片的推荐尺寸和最大支持尺寸 下面是标签栏(UITabBar)中tab按钮图标分别在1x.2x.3x下不会压缩变形的尺寸: @1x : 推荐 25 x 25   (最大: 48 x 32)@2x : 推荐 50 x 50   (最大: 96 x 64)@

ios开发之Swift新手入门

1.关于swift和调试,swift在ios7.0才支持,ios8.3系统的真机必需要xcode6.3才干调试.安装xcode6.3需要os x 10.10以上 2.应用程序由Main.storyboard启动和设置,定义先启动哪个ViewController 3.组件有两种方式实例化.一种是直接在代码里面new出来.第二种是代码里面定义.然后在storyboard视图界面把组件加入进去.注意组件加入进去之后要进行关联. 4.注意在Build Phasses里面不能缺失Main.storyboa

iOS8开发之iOS8的UIAlertController

在iOS8之前用UIActionSheet和UIAlertView来提供按钮选择和提示性信息,比如UIActionSheet可以这样写: UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"title,nil时不显示" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherB

iOS开发之Swift调用Objective-C代码

本文写于2014.09.25 最近iOS开发之新编程语言Swift在iOS开发圈内反响比较大,国内外都有很多教程或小示例. 虽然Swift这门语言仍然在不断的进化之中,而且变动还是比较大,苹果公司也不承诺目前所写代码会在将来兼容,但仍挡不住iOS开发者的热情. 为什么要使用Swift调用Objective-C代码 目前Swift语言所编写的应用才刚刚可以使用Xcode 6 GM版本提交,而Objective-C作为苹果的主开发语言存在了很多年了.目前尚无成熟的Swift库可用,所以当前编写应用可

IOS开发之SWIFT进阶部分

概述 上一篇文章<iOS开发系列--Swift语言> 中对Swift的语法特点以及它和C.ObjC等其他语言的用法区别进行了介绍.当然,这只是Swift的入门基础,但是仅仅了解这些对于使用Swift 进行iOS开发还是不够的.在这篇文章中将继续介绍一些Swift开发中一些不常关注但是又必备的知识点,以便对Swift有进一步的了解. 访问控制 Swift命名空间 Swift和ObjC互相调用 Swift和ObjC映射关系 Swift调用ObjC ObjC调用Swift 扩展—Swift调用C 反

Swift开发之CALayer---- Modifying the Layer&#39;s Appearance

CALayer的属性一堆,这些都是??,下面就来测试一下各个属性都什么效果- 先建一个基本的洋红色方块 func testLayerProperty(){ myLayer = CALayer() myLayer.frame = CGRectMake(100, 100, 100, 100) myLayer.backgroundColor = UIColor.magentaColor().CGColor self.view.layer.addSublayer(myLayer) } Swift开发之C

物联网的全栈开发之路

本文由嵌入式企鹅圈原创团队成员-华南师范大学物联网创新中心黄鑫执笔. 两年前我还一直专注在android系统定制领域的研究,对物联网关注比较少,尽管我个人对智能家居的概念和场景体验也比较熟悉.真正引起我注意的是有一次在车上,听到电视广播说打开蓝牙,用手机摇一摇就可以摇到红包,我就觉得这个场景很棒,然后就知道了ibeacon和微信摇一摇周边,知道了微信硬件平台和物联网的发展已经深入渗透到我们的生活了.尽管我觉得自己进入物联网不算早,但是考虑到传统电子行业都有升级到物联的需求,加上自身在嵌入式和AP