arc和mrc混编
Build Phases->Compile Sources 中找到需要使用mrc的文件,在Compiler Flags中填上 -fno-objc-arc
Build Phases->Compile Sources 中找到需要使用mrc的文件,在Compiler Flags中填上 -fno-objc-arc
//
// ViewController.swift
// 数据解析-json
//
// Created by admin on 16/2/25.
// Copyright © 2016年 jin. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.plistDecodeC()
// Do any additional setup after loading the view, typically from a nib.
}
func plistDecodeC()
{
// url
let url = NSURL(string: "http://test.com/plist")
// 请求
let request = NSURLRequest(URL: url!)
// 发送请求
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue()) { (responce:NSURLResponse?, data:NSData?, error:NSError?) -> Void in
// 转换后使用
let plist:AnyObject? = NSPropertyListSerialization.propertyListFromData(data!, mutabilityOption: NSPropertyListMutabilityOptions.MutableContainers, format: nil, errorDescription: nil)
// 使用
print(plist)
print(plist?.valueForKey("CFBundleDevelopmentRegion"))
print(plist?.valueForKey("NSAppTransportSecurity"))
}
}
/**
{
"statuses": [
"3382905382185354",
"3382905252160340",
"3382905235630562",
],
"previous_cursor": 0,
"next_cursor": 0,
"total_number": 16
}
*/
func jsonDecodeB()
{
// url
let url = NSURL(string: "http://test.com/jsonA")
// 请求
let request = NSURLRequest(URL: url!)
// 发送请求
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue()) { (responce:NSURLResponse?, data:NSData?, error:NSError?) -> Void in
// 转换后使用
let json:AnyObject? = JSONDecoder().objectWithData(data!)
// 全部都是nil。。。。
print(json)
print(json?.valueForKey("statuses"))
print(json?.valueForKey("next_cursor"))
}
}
func jsonDecodeA()
{
// url
let url = NSURL(string: "http://test.com/jsonA")
// 请求
let request = NSURLRequest(URL: url!)
// 发送请求
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue()) { (responce:NSURLResponse?, data:NSData?, error:NSError?) -> Void in
// 转换后使用
let json:AnyObject? = try? NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers)
// 取出使用,都是可选类型,有点蛋疼啊
print(json?.valueForKey("statuses"))
print(json?.valueForKey("next_cursor"))
}
}
}
//
// ViewController.swift
// 网络
//
// Created by admin on 16/2/25.
// Copyright © 2016年 jin. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
let webView = UIWebView(frame: UIScreen.mainScreen().bounds)
override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(self.webView)
self.requestB()
}
// 发送请求,NSURLConnection,同步
func requestC()
{
// 确定url
let url = NSURL(string: "https://m.baidu.com")
// 设置请求
let request = NSURLRequest(URL: url!)
var responce:NSURLResponse?
// 发送请求,NSURLConnection会出警告,所以使用NSURLSession
let responceData:NSData? = try! NSURLConnection.sendSynchronousRequest(request, returningResponse: &responce)
self.webView.loadHTMLString(String(data: responceData!, encoding: NSUTF8StringEncoding)!, baseURL: nil)
}
// 发送请求,NSURLConnection会出警告,所以使用NSURLSession,异步的
func requestA()
{
// 确定url
let url = NSURL(string: "https://m.baidu.com")
// 设置请求
/**
参数:url:资源路径
cachePolicy:缓存策略
NSURLRequestUseProtocolCachePolicy = 0, // 默认缓存策略,会自动缓存
NSURLRequestReload(刷新)Ignoring(忽略)Local(本地)CacheData(缓存数据) = 1, 每次都从服务器去加载数据。忽略本地缓存
**** 底下两个 一般来离线访问。一般配合Reachability(苹果提供的检测网络连接的框架)使用
// 如果用户使用的wifi,就使用这个策略
NSURLRequestReturnCacheDataElseLoad = 2, // 如果有缓存,就用缓存。没有就上网加载
// 如果用户使用的3G,就使用下面的策略
NSURLRequestReturnCacheDataDontLoad = 3, // 如没有缓存,就用缓存。没有返回空,死活不上网
timeoutInterval:超时时间,默认是60秒,一般设置15~20秒。超过这个时间以后,如果服务器还没有相应,就不继续等待了
SDWebImage超时时长设置的是-->15秒
AFN超时时长 60秒
*/
let request = NSURLRequest(URL: url!)
// 发送请求
/**
参数:Request 请求
queue: 队列,这个队列,是完成以后,回调block执行的队列、
Asynchronous:只要是异步,肯定会开新的线程
// 使用场景:如果下载的是压缩包,解压缩也是耗时操作。也需要放在子线程
// 如果回调block里面只需要更新UI,那么就可以只开始指定queue为主队列
Handler:网络访问完成以后执行的代码块
response: 服务器的响应(包含响应行/响应头...下载的时候才去关心这个)
data: 返回的二进制数据
connectionError:在连接服务器的时候的错误,只要是能正常连接到服务器,就不会有错。
只要有网络候出现的错访问,就会可能有错误
*/
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) { [weak self] (response:NSURLResponse?, data:NSData?, error:NSError?) -> Void in
self?.webView.loadHTMLString(String(data: data!, encoding: NSUTF8StringEncoding)!, baseURL: nil)
}
}
// NSURLSession发送请求
func requestB()
{
// 确定url
let url = NSURL(string: "https://m.baidu.com")
// 设置请求
let request = NSURLRequest(URL: url!)
// 发送请求,NSURLConnection会出警告,所以使用NSURLSession
let session = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration())
let task = session.dataTaskWithRequest(request) { [weak self] (data:NSData?, response:NSURLResponse?, error:NSError?) -> Void in
dispatch_async(dispatch_get_main_queue(), { [weak self] () -> Void in
self!.webView.loadHTMLString(String(data: data!, encoding: NSUTF8StringEncoding)!, baseURL: nil)
})
}
task.resume()
}
}
//
// ViewController.swift
// 位移枚举
//
// Created by zhang on 16/2/24.
// Copyright © 2016年 jin. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
// 需要下载这个类
let reach = Reachability(hostName: "baidu.com")
override func viewDidLoad() {
super.viewDidLoad()
self.showStatus()
// 监听联网状态的改变
NSNotificationCenter.defaultCenter().addObserver(self, selector: "showStatus", name: kReachabilityChangedNotification, object: nil)
// Do any additional setup after loading the view, typically from a nib.
}
deinit
{
// 移除整个控制器里所有的监听
NSNotificationCenter.defaultCenter().removeObserver(self)
// 移除监听
NSNotificationCenter.defaultCenter().removeObserver(self, name: kReachabilityChangedNotification, object: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func showStatus()
{
switch reach.currentReachabilityStatus().rawValue
{
case 0:
print("没有联网")
break
case 1:
print("wifi")
break
case 2:
print("流量")
break
default:
break
}
}
}
//
// ViewController.swift
// 位移枚举
//
// Created by zhang on 16/2/24.
// Copyright © 2016年 jin. All rights reserved.
//
struct Jin:OptionSetType {
let rawValue:Int
static let Left = Jin(rawValue: 1<<0)
static let Right = Jin(rawValue: 1<<1)
static let Top = Jin(rawValue: 1<<2)
static let Bottom = Jin(rawValue: 1<<3)
}
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let jin:Jin = [.Bottom,.Left,.Top]
self.test(jin)
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func test(value:Jin)
{
if value.contains(.Left){
print("left")
}
if value.contains(.Right){
print("Right")
}
if value.contains(.Top){
print("Top")
}
if value.contains(.Bottom){
print("Bottom")
}
}
}