swift 学习笔记(1)--- 运行环境
1. 下载xcode6 beta版
链接: http://pan.baidu.com/s/1o6kFu0U 密码: i7mm
大家不要怕,xcode 5 和xcode 6 是可以共存的。
2. 创建共存
- Choose File > New > Project > (iOS or OS X) > Application > your template of choice.
此处选择 Single view Application。
2. Click the Language pop-up menu and choose Swift
3. 上代码: 一样的用法,只不过语法不一样
4. 运行okay
class XHNewsTableViewController : UITableViewController {
var dataSource = []
var thumbQueue = NSOperationQueue()
let hackerNewsApiUrl = "http://qingbin.sinaapp.com/api/lists?ntype=%E5%9B%BE%E7%89%87&pageNo=1&pagePer=10&list.htm"
override func awakeFromNib() {
super.awakeFromNib()
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let refreshControl = UIRefreshControl()
refreshControl.attributedTitle = NSAttributedString(string: "下拉刷新")
refreshControl.addTarget(self, action: "loadDataSource", forControlEvents: UIControlEvents.ValueChanged)
self.refreshControl = refreshControl
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
loadDataSource();
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func loadDataSource() {
self.refreshControl.beginRefreshing()
var loadURL = NSURL.URLWithString(hackerNewsApiUrl)
var request = NSURLRequest(URL: loadURL)
var loadDataSourceQueue = NSOperationQueue();
NSURLConnection.sendAsynchronousRequest(request, queue: loadDataSourceQueue, completionHandler: { response, data, error in
if error {
println(error)
dispatch_async(dispatch_get_main_queue(), {
self.refreshControl.endRefreshing()
})
} else {
let json = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary
let newsDataSource = json["item"] as NSArray
dispatch_async(dispatch_get_main_queue(), {
self.dataSource = newsDataSource
self.tableView.reloadData()
self.refreshControl.endRefreshing()
})
}
})
}
// #pragma mark - Segues
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showWebDetail" {
// let indexPath = self.tableView.indexPathForSelectedRow()
// let object = dataSource[indexPath.row] as NSDictionary
// (segue.destinationViewController as XHWebViewDetailController).detailID = object["id"] as NSInteger
}
}
// #pragma mark - Table View
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataSource.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("XHNewsCell", forIndexPath: indexPath) as UITableViewCell
let object = dataSource[indexPath.row] as NSDictionary
cell.textLabel.text = object["title"] as String
cell.detailTextLabel.text = object["id"] as String
cell.imageView.image = UIImage(named :"cell_photo_default_small")
cell.imageView.contentMode = UIViewContentMode.ScaleAspectFit
let request = NSURLRequest(URL :NSURL.URLWithString(object["thumb"] as String))
NSURLConnection.sendAsynchronousRequest(request, queue: thumbQueue, completionHandler: { response, data, error in
if error {
println(error)
} else {
let image = UIImage.init(data :data)
dispatch_async(dispatch_get_main_queue(), {
cell.imageView.image = image
})
}
})
return cell
}
override func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath!) -> CGFloat {
return 80
}
}

浙公网安备 33010602011771号