【极客学院】-iOS学习笔记-5-Swift(变量,循环,控制,函数,面向对象,小项目:呈现网页)

极客学院课程网址:http://www.jikexueyuan.com/course/92_2.html?ss=1


变量,常量:

var a = 1
var b = 2
a = 10
let c = a+b
print(c)

数据类型:

var str="Hello"
var s:String = "World"
var i:Int = 100
var words:String = "jikexueyuan"

print(str)

字符串连接:

var str = "Hello"
var i = 200
str = str + "Alice"
str = str + "100"
str = "\(str),2016 better,\(100)"

print(str)

str = "\(str),2016 better,\(i)"

print(str)

数组:空数组,指定类型的空数组

var arr = ["Happy","New","Year",2016]

print(arr)

var arr1 = []
var arr2 = [String]()

字典: 静态定义,动态添加

var dict = ["name":"Alice","age":"18"]
dict["sex"]="Female"

print(dict)
print(dict["name"])

循环:

var arr = [String]()

for index in 0...100{
    arr.append("Item \(index)")

}

print(arr)

for value in arr{
    print(value)

}

var i = 0

while i<arr.count{
    print(arr[i])
    i++
}

var dict = ["name":"jikexueyuan","age":"16"]

for (key,value) in dict{
    print("\(key),\(value)")
}

条件控制:

for index in 0...100{
    if index%2 == 0 {
            print(index)
    }
    
}

可选变量:可选变量为空时,不执行下面的判断

var myName:String?="jikexueyuan"
myName = nil

if let name=myName{
    print("Hello \(name)")
}

定义函数:

func sayHello(name:String){
    print("Hello \(name)")
}

sayHello("2016")

函数,返回多个结果:默认情况下没有返回追,所以要加上返回的类型,指定类型时和返回结果一样的形式 ->(Int,Int)

func getNums()->(Int,Int){
    return (2,3)
}

let (a,b) = getNums()

函数,可以被当成变量

var fun = sayHello
fun("ZhangSan")

创建类:写方法

class Hi {
    func sayHi(){
        print("Hi 2016")
    }
}

var hi = Hi()
hi.sayHi()

继承

class Hello:Hi{

}

var h = Hello()
h.sayHi()

重写

class Hello:Hi{
    
    override func sayHi() {
        print("Hello 2016")
    }
}

var h = Hello()
h.sayHi()

构造方法

class Hello:Hi{
    
    
    var _name:String
    
    //构造方法,传递进来一个name属性
    init(name:String) {
        
        self._name = name
        //内部记录name
    }
    
    override func sayHi() {
        print("Hello \(self._name)")
    }
}

//创建方法时,需要传递参数,还有标签name
var h=Hello(name:"Alice")
h.sayHi()

使用Swift编程语言开发iOS项目

呈现网页:

1.Main.storyboard→把 web view 拖到 view controller scene

2.在ViewController.swift里写一个变量 wv

为了把变量和用户界面绑定,需要写:@IBOutlet var wv:UIWebView

3.Main.storyboard→选中UIWebView→ReferencingOutletCollectiions→拖出一条线,连接到ViewController.swift的与变量wv绑定起来

4.再override viewDidLoad方法里

wv.loadRequest 通过loadRequest 方法呈现一个页面

loadRequest 的传入参数




Swift 中 问号和叹号的意思:

简单明了

http://blog.csdn.net/wmqi10/article/details/37562071

很多例子

http://www.cnblogs.com/royi123/archive/2014/07/10/3834909.html

专业级

http://letsswift.com/2014/06/swift-questionmark-exclamatorymark/


代码运行成功后,报错:“NSURLSession/NSURLConnection HTTP load failed”

解决方案:

open your info.plist as a source code
Add following:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>

参考自:http://swiftdeveloperblog.com/nsurlsessionnsurlconnection-http-load-failed/

http://stackoverflow.com/questions/30739473/nsurlsession-nsurlconnection-http-load-failed-on-ios-9



你可能感兴趣的:(ios,极客学院)