2019独角兽企业重金招聘Python工程师标准>>>
1 import Foundation
2
3 //加载网络数据,查找数据的字符串
4 let dataurl = "http://api.k780.com:88/?app=weather.city&&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json"
5 var url = NSURL(string: dataurl)
6 var httpData = NSData(contentsOfURL: url!)
7 //println(httpData) //输出来都是数字 字节码
8 var httpstr = NSString(data: httpData!, encoding: NSUTF8StringEncoding)
9 //println(httpstr) //转为NSString就是 可观的数据了。
10 let subRange = httpstr?.rangeOfString("玉山")
11 println("\(subRange!.location) 和 \(subRange!.length)")
12
13 var findRange = NSMakeRange(subRange!.location-33, 76)
14 var findStr = httpstr!.substringWithRange(findRange)
15 println(findStr)
16
17 //读取本地文件
18 let path = "/Users/HeYang/Desktop/file.txt"
19 var findfileStr = String(contentsOfFile: path, encoding: NSUTF8StringEncoding, error: nil)
20 var nsfindfileStr = NSString(string: findfileStr!)//转为NSString类型
21 println(nsfindfileStr)
运行结果: