码迷,mamicode.com
首页 > 编程语言 > 详细

Swift(1) 基本语法

时间:2014-10-21 21:23:41      阅读:358      评论:0      收藏:0      [点我收藏+]

标签:des   blog   http   io   ar   for   sp   数据   div   

【适合对象】有一定编程基础的同学

【情景】边看边练,多练多学,自然成长。

【声明】此文章属于原创作品,未经授权,如非法转载,予以追究法律责任。 原文链接:http://www.cnblogs.com/codeAnimal/p/4041545.html

 

练习笔记:

1.var变量
var str = "Hello, playground"
println(str)

 2.if语句

var a = 20
if a > 10 {
    println("if 语句,判断条件 无括号");
}

 3.三元运算符

var sy = a > 80 ? "真":"假"
println(sy)

 4.指定数据类型

var a1:Int = 10
println(a1)

 5.注释符号

//

 6.数据类型

var inta:Int32 = 12 ; println(inta)
var floata:Float32 = 10.0 ; println(floata)

 7.控制流 for-in

let indi = [10,201,30]
var teamScore  = 0
for score in indi {
    if score > 50{
        println(score)
    }
}
打印结果:201

   //练习1:特殊字符://..表示 范围


  var first = 0
  for i in 0..3{
  first += i
  }
  println(first)

//练习2:计算出 字典中,数字最大值  结果:456

let nums = ["prime":[45,456,1,3],"squ":[12,10,32] ]
var largest = 0
for(k,n) in nums{
    for num in n{
        if num > largest{
            largest = num
        }
    }
}
println(largest)

 8.switch语句 (注意:default,必须有)

let vege = "red peper"
var title = "default msg"
switch vege{
    case "red":
        title = "red"
    case "red","peper":
        title = "2";
case let y where y.hasSuffix("peper")://特殊用法
        println(y)
        title = "3"
default:
    title = "ssss"   
}
println(title)

 9.while

var n = 2;
while n < 50 {
    n = n*3
}
println(n)

 10.计算字符串的长度 countElements

var s = "lljkjij "
var l = countElements(s)
println(l)

 11.字符串相等 == 

var str1 = "i am liuqinghua"
var str2 = "i am liuqinghua"
if str1 == str2 {
    println("result > "+"==")
}else{
    println("result >"+"!=")
}

 12.数据类型转换

let num1 = 3
let num2 = 0.1212
var pi = num1 + Int(num2)
println(pi)

打印结果:3

 13. Int? 可选类型

var strint = "123"
let convertedNum:Int? = strint.toInt()  //不确定 strint是纯数字,还是字符串
println("\(convertedNum) result \(convertedNum!)")

// 为可选类型的变量设置nil值,表示没有任何值
var serverResponse:Int? = 404
serverResponse = nil

 14.元组

//形式1.
let httpError = (404,"fail")
println(httpError.1)

//形式2.
let httpError2 = (status:404,descrip:"fail")
println(httpError2.status)

 15.数组

var shopList:String[] = ["Egg","Milk"]

//追加(append)
shopList.append("Flour")
println(shopList.count) //数组元素个数

//插入(insert)
shopList.insert("Maple",atIndex:shopList.count)
println(shopList)

//删除(removeAtIndex)
shopList.removeAtIndex(0)
println(shopList)

//数组的长度
shopList.count

//遍历1
for item in shopList
{
    println(item)
}

//遍历2
for (index,value) in enumerate(shopList)
{
    println("Item\(index+1):\(value)")
}

打印结果: index,下标(从0开始)

 16.字典

var air:Dictionary<String,String> = ["A":"a","B":"b"]

//字典追加
air["C"] = "c"
println(air)

//字典删除元素 removeValueForKey
let isDel = air.removeValueForKey("A")
println(isDel) //"a" (被删除的值 )
println(air)

//字典长度 count属性
air.count

//字典遍历1
for (k,v) in air
{
    println(k+":" + v)
}

//字典遍历2:分别遍历keys和values
for key in air.keys
{
    println(key)
}
for value in air.values
{
    println(value)
}

//把键转换成数组
let airKey = Array(air.keys)
println(airKey)

 17.函数

func sumOf(nums:Int...) -> Int{
    var sum = 0
    var j = 0
    for n in nums{
        sum += n
        j++
    }
    return sum/j
    
}

//调用方法

var code = sumOf(20,20,20)
println(code)

 18.类

class Test{

    //声明变量
    var num:Int = 0
    var name:String
    
   //构造函数
    init(name:String){
        self.name = name;
    }
    
    //自定义方法,返回String
    func simple() -> String{
            return name;
    }

}

var test = Test(name:"liuqinghua")
var name = test.simple()

println(name)

打印结果:liuqinghua

  

Swift(1) 基本语法

标签:des   blog   http   io   ar   for   sp   数据   div   

原文地址:http://www.cnblogs.com/codeAnimal/p/4041545.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!