极客学院课程网址: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")
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()
呈现网页:
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