The actual project is always inseparable from these two steps, in most cases, the use of a third-party framework to assist the completion, Swift is no exception, because Swift is not mature, the third-party framework written in Swift language is very unstable (Apple update is too fast), fortunately swift and OC can be mixed, So this use of the framework is used in OC. Network requests use Afnetworking,json parsing to use Mjextension.
The following begins the text:
1. Using the OC framework in Swift
The principle is simple and requires an. h header file, which imports the header files of the OC Framework we need to use, and configures this. h file in the project settings
1). We take an error-prone approach to creating the. h file and create a new OC class in the SWIFT project
Note Language selection OC language, after creation, Xcode will have such a hint
Asked whether to create a bridge on the street, this wave is critical, dot create
, after the creation of the project settings there will be configured to configure the file, if you create their own configuration, it is easy to write the path wrong ~
Next Test whether we are configured correctly
Write a method in the test class that you created earlier, and call it in Swift
Import Test.h in the above Bridge Street file (very critical!). )
To invoke the test in Swift
It is usually highlighted and can be automatically prompted for no problem,
2. Introduce a third-party framework and invoke
It's easy here, just copy the code and import the header file.
and import the framework package in the Bridge Street file
#import "AFNetworking.h"#import "MJExtension.h"
As for the use of the framework, I will not repeat it, the official documents written very clearly,
let manager = AFHTTPSessionManager() let"http://op.juhe.cn/onebox/weather/query?cityname=%e6%b7%b1%e5%9c%b3&key=eb08f814be6e473ec5ad9a6bde57e5e5&dtype=json" objectin NSLog("请求成功") objectin NSLog("请求失败%@",object) })
It is also important to note that in the high version of Xcode, HTTP requests are not allowed (HTTPS only), and you need to include such a section in the project's plist file
<key>NSAppTransportSecurity</key><dict> <key>NSAllowsArbitraryLoads</key> <true/></dict>
Test, if the above configuration is correct, is able to print the request successfully ~
3. Data parsing after a successful request
Look at the callback method, the object is the returned data, but he is a anyobject type, is equivalent to the ID type in OC (any type), the measured return is a dictionary type, if you want to print out, you need to first strong into a dictionary (OC can be printed directly)
letobjectas! NSDictionary NSLog("%@", dict)
The framework has helped us turn into a dictionary type, and now we're going to do a dictionary turn model.
4. Complex JSON parsing & model formulation
Above we have taken the dictionary, in fact, we can use the system method to the dictionary to the model, but for the more complex JSON data, the system method still need to let us write a lot of code ~
This time the JSON data used for testing is the weather forecast, the interface document is released, there is a JSON example returned, if you print the above dictionary, you will also see the approximate structure
Model class
classWeatherbean:nsobject {varReason:String!varerror_code:nsnumber!varresult:result!classResult:nsobject {vardata:data! }classData:nsobject {varrealtime:realtime!varlife:life!varWeatherArray<weather2>!varpm25:pm25!varDate:String!varisforeign:nsnumber! }classRealtime:nsobject {varCity_code:String!varCity_name:String!varDateString!varTimeString!varweek:nsnumber!varMoonString!vardatauptime:nsnumber!varweather:weather!varwind:wind! }classWeather:nsobject {varTemperature:String!varHumidity:String!varInfoString!varImg:String! }classWind:nsobject {varDirectString!varPowerString!varOffsetString!varWindspeed:String! }classLife:nsobject {varDataString!varInfo:info! }classInfo:nsobject {varChuanyi:Array<String>!varGanmao:Array<String>!varKongtiao:Array<String>!varWuran:Array<String>!varXiche:Array<String>!varYundong:Array<String>!varZiwaixian:Array<String>! }classWeather2:nsobject {varDateString!varinfo:info2!varWeekString!varNongli:String!classInfo2:nsobject {varDayArray<String>!varNight:Array<String>! } }classPm25:nsobject {varShow_desc:String!varpm25:pm252!varDatetime:String!varKeyString!varCityName:String!classPm252:nsobject {varPM25:String!varCURPM:String!varPM10:String!varLevelString!varQuality:String!varDes:String! } }}
The deepest about 4-5 layers, the count is very complex ~
Java's small partners must have used Gson, in fact, MJ analysis and this very similar, model classes are consistent
manager.GETin let weat = WeatherBean.mj_objectWithKeyValues(object) NSLog("请求成功"+weat.reason+weat.result.data.pm25.cityName) in NSLog("请求失败%@",object) })
So we can parse all the data ~
Hit a breakpoint and you can see more data.
Finish ~
Swift Network request &json resolution