标签:swift
之前最开始了解Swift的时候的一些基本的东西,简单的整理一下。
import UIKit
var names = []
class ViewController: UIViewController,UIAlertViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
var stringValue:String!
// stringValue = "chw"
// let str11 = stringValue.hashValue
// var stringValue:Optional<String>
if let str = stringValue {
let hashValue = str.hashValue
}
let str1 = "swift"
for k in str1 {
println(k)
}
var dic = [0:0, 1:0, 2:0]
var newDic = dic
//Check dic and newDic
dic[0] = 1
println(dic) //[0: 1, 1: 0, 2: 0]
println(newDic)//[0: 0, 1: 0, 2: 0]
var arr11:Array = [0,"lll",0]
var newArr = arr11
arr11[0] = 1
//Check arr and newArr
println(arr11) //[1, 0, 0]
println(newArr) //[0, 0, 0]
// 字符串
let heart = "??"
println(heart)//??
//空字符串
let empty = ""
let anotherEmpty = String()
if empty.isEmpty {
println(" empty ")
} else {
println(" not empty ")
}
//前缀后缀
let str11 = "chengHong wei"
if str11.hasPrefix("ch") {
println("prefix is ch")
}
if str11.hasSuffix("wei") {
println("suffix is wei")
}
//大小写
let up = str11.uppercaseString
println(up)//CHENGHONG WEI
let low = str11.lowercaseString
println(low)//chenghong wei
// 可选值探究
var input:String?
var input1 = "18"
var age = input1.toInt()
if age != nil{
println("your age is " + String(age!))
} else {
println("error for input")
}
// 遍历数组
let arr = ["1","2","3"]
// ...相当于[0,2]一个区间,意思就是 0=< i && i<= 2
for i in 1...arr.count {
println("arr count is \(arr.count)")
println("arr object index \(arr[i-1])")
}
// ..<是半闭半开区间,意思就是[0,arr.count) (之前是..,现在已经改了)
for i in 0..<arr.count {
println(i)
}
for var i=0;i<3;i++ {
println("\(arr)")
}
// array加元素
var arr1 = ["a"]
arr1 += ["b"]
arr1 += ["c","d","e"]
println(arr1)
println("arr1 is \(arr1)")
arr1[0] = "f"
println(arr1)
var arr20 = [1,2,3,4]
arr20[0] = 0
arr20[1...1] = [5,8]//在array[1]这儿插入了5,8两个元素,这个功能仅在区间状态有效,如果a[3] = [5,8] 则会报错
println(arr20) //[0,5,8,3,4]
// 泛型是如何用来定义容器的存储的。还有一个数组的缩写形式,这个更具有可读性,但本质上是一样的。现在你不能往数组里面添加非Int型的元素。这听起来挺糟糕,但它非常有用。再也不需要用API来记录数组里存储了哪些从某个方法返回或者以属性存储的元素。你可以告诉编译器这些信息,编译器在错误检查方面会更加智能,并且可以提早做出优化。
let arr2: [Int] = [1,2,3,4] //数组里面只能是int型
let arr3: [Float] = [1,2,2.3]
println("arr3 is \(arr3)")
let arr4: [String] = ["1","chw","love"]
println("arr4 is \(arr4)")
// var 可变的数组是可以加元素的,let 声明的是不可加的,也就是可变与不可变数组的意思,在 Objective-C 和 Cocoa 中,您通过选择两个不同的类( NSString 和 NSMutableString )来指定该字符串是否可以被修改,Swift中的字符串是否可以修改仅通过定义的是变量还是常量来决定,实现了多种类型可变性操作的统一。
var arr5:Array = [1,2,3,"cheng"]
arr5.append("chw")
println("arr5 is \(arr5)")
// 字典
let dic11 = ["name":"chw","sex":1,"height":175.0]
println("This is a dictionary \(dic11)")
// Swift的=是没有返回值的
var a1 = 5
var a2 = 5
if a1 == a2 {
println("===")
} else {
// println("!==")
}
// 字符串拼接
let cheng = "c"
let hong = "h"
let wei = "w"
let chenghongwei = cheng + hong + wei
println("my name is \(chenghongwei)")
// 取余
println(8%3) // = 2
println(8%2.5) // 8 % 2.5 = 8 - 2.5 * 3 = 0.5 小数也是可以取余的
// swith语句
var i = 14
switch i {
case 0...10:
println("range is 0-10")
case 11...20:
println("range is 11-20")
case 21..<30:
println("range is [21-30)")
default:
break
}
var name = "chw"
switch name {
case "cheng":
println("name is cheng")
case "hong":
println("name is hong")
case "wei":
println("name is wei")
default:
println("someone else")
break
}
// 闭包初探
names = ["Chris", "Alex", "Ewa", "Barry", "Daniella"]
func backwards(s1:String,s2:String)->Bool {
return s1>s2
}
// UI控件
let lab = UILabel(frame: CGRectMake(100, 100, 200, 100))
lab.text = "Hello Chw"
lab.backgroundColor = UIColor.greenColor()
lab.textAlignment = NSTextAlignment.Center
self.view.addSubview(lab)
let btn = UIButton(frame: CGRect(origin: CGPointMake(100,250), size: CGSizeMake(100,50)))
btn.backgroundColor = UIColor.grayColor()
btn.setTitle("Chw btn", forState: UIControlState.Normal)
btn.addTarget(self, action: "btnClick:", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(btn)
}
//TODO:---
func btnClick(sender:UIButton!){
println("chw 111")
var alert = UIAlertView()
alert.title = "CHW"
alert.delegate = self
alert.message = " This is a alert !"
alert.addButtonWithTitle("Cancle")
alert.addButtonWithTitle("OK")
alert.show()
}
//MARK:-
//MARK:--- AlertViewdelegate
func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int){
println("buttonIndex:\(buttonIndex)")
if buttonIndex == 0 {
println("Cancle Btn !")
} else if buttonIndex == 1 {
println("Sure Btn")
}
}
//MARK:-
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:swift
原文地址:http://blog.csdn.net/loveway_/article/details/47124717