Skip to content

Commit 99c2748

Browse files
committed
update to swift 5
1 parent 18ac4c2 commit 99c2748

File tree

7 files changed

+117
-24
lines changed

7 files changed

+117
-24
lines changed

APIJSON-iOS/APIJSON-Swift/APIJSONTest.xcodeproj/project.pbxproj

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@
193193
TargetAttributes = {
194194
655C4C5A1FCDCACA00EE5EF8 = {
195195
CreatedOnToolsVersion = 7.3.1;
196+
DevelopmentTeam = 7KXVPC4Y3C;
196197
};
197198
655C4C6E1FCDCACA00EE5EF8 = {
198199
CreatedOnToolsVersion = 7.3.1;
@@ -209,6 +210,7 @@
209210
developmentRegion = English;
210211
hasScannedForEncodings = 0;
211212
knownRegions = (
213+
English,
212214
en,
213215
Base,
214216
);
@@ -399,21 +401,25 @@
399401
isa = XCBuildConfiguration;
400402
buildSettings = {
401403
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
404+
DEVELOPMENT_TEAM = 7KXVPC4Y3C;
402405
INFOPLIST_FILE = APIJSONTest/Info.plist;
403406
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
404407
PRODUCT_BUNDLE_IDENTIFIER = org.apijson.APIJSONTest;
405408
PRODUCT_NAME = "$(TARGET_NAME)";
409+
SWIFT_VERSION = 5.0;
406410
};
407411
name = Debug;
408412
};
409413
655C4C851FCDCACA00EE5EF8 /* Release */ = {
410414
isa = XCBuildConfiguration;
411415
buildSettings = {
412416
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
417+
DEVELOPMENT_TEAM = 7KXVPC4Y3C;
413418
INFOPLIST_FILE = APIJSONTest/Info.plist;
414419
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
415420
PRODUCT_BUNDLE_IDENTIFIER = org.apijson.APIJSONTest;
416421
PRODUCT_NAME = "$(TARGET_NAME)";
422+
SWIFT_VERSION = 5.0;
417423
};
418424
name = Release;
419425
};
@@ -482,6 +488,7 @@
482488
655C4C851FCDCACA00EE5EF8 /* Release */,
483489
);
484490
defaultConfigurationIsVisible = 0;
491+
defaultConfigurationName = Release;
485492
};
486493
655C4C861FCDCACA00EE5EF8 /* Build configuration list for PBXNativeTarget "APIJSONTestTests" */ = {
487494
isa = XCConfigurationList;
@@ -490,6 +497,7 @@
490497
655C4C881FCDCACA00EE5EF8 /* Release */,
491498
);
492499
defaultConfigurationIsVisible = 0;
500+
defaultConfigurationName = Release;
493501
};
494502
655C4C891FCDCACA00EE5EF8 /* Build configuration list for PBXNativeTarget "APIJSONTestUITests" */ = {
495503
isa = XCConfigurationList;
@@ -498,6 +506,7 @@
498506
655C4C8B1FCDCACA00EE5EF8 /* Release */,
499507
);
500508
defaultConfigurationIsVisible = 0;
509+
defaultConfigurationName = Release;
501510
};
502511
/* End XCConfigurationList section */
503512
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>IDEDidComputeMac32BitWarning</key>
6+
<true/>
7+
</dict>
8+
</plist>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Bucket
3+
uuid = "F176DF8F-D2E4-4AAD-82C4-B145D3DDB06F"
4+
type = "1"
5+
version = "2.0">
6+
<Breakpoints>
7+
<BreakpointProxy
8+
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
9+
<BreakpointContent
10+
uuid = "0FD1DAD7-3631-4008-A63C-D9F9E5B3C01C"
11+
shouldBeEnabled = "Yes"
12+
ignoreCount = "0"
13+
continueAfterRunningActions = "No"
14+
filePath = "APIJSONTest/ViewController.swift"
15+
startingColumnNumber = "9223372036854775807"
16+
endingColumnNumber = "9223372036854775807"
17+
startingLineNumber = "84"
18+
endingLineNumber = "84"
19+
landmarkName = "test()"
20+
landmarkType = "7">
21+
<Locations>
22+
<Location
23+
uuid = "0FD1DAD7-3631-4008-A63C-D9F9E5B3C01C - 336694360a88cce6"
24+
shouldBeEnabled = "Yes"
25+
ignoreCount = "0"
26+
continueAfterRunningActions = "No"
27+
symbolName = "APIJSONTest.ViewController.test() -&gt; ()"
28+
moduleName = "APIJSONTest"
29+
usesParentBreakpointCondition = "Yes"
30+
urlString = "file:///Users/rogerluo/Downloads/APIJSON-Demo-master/APIJSON-iOS/APIJSON-Swift/APIJSONTest/ViewController.swift"
31+
startingColumnNumber = "9223372036854775807"
32+
endingColumnNumber = "9223372036854775807"
33+
startingLineNumber = "84"
34+
endingLineNumber = "84"
35+
offsetFromSymbolStart = "4060">
36+
</Location>
37+
<Location
38+
uuid = "0FD1DAD7-3631-4008-A63C-D9F9E5B3C01C - 183bbd76298b9b95"
39+
shouldBeEnabled = "Yes"
40+
ignoreCount = "0"
41+
continueAfterRunningActions = "No"
42+
symbolName = "closure #1 (Swift.Optional&lt;Foundation.Data&gt;, Swift.Optional&lt;__C.NSURLResponse&gt;, Swift.Optional&lt;Swift.Error&gt;) -&gt; () in APIJSONTest.ViewController.test() -&gt; ()"
43+
moduleName = "APIJSONTest"
44+
usesParentBreakpointCondition = "Yes"
45+
urlString = "file:///Users/rogerluo/Downloads/APIJSON-Demo-master/APIJSON-iOS/APIJSON-Swift/APIJSONTest/ViewController.swift"
46+
startingColumnNumber = "9223372036854775807"
47+
endingColumnNumber = "9223372036854775807"
48+
startingLineNumber = "86"
49+
endingLineNumber = "86"
50+
offsetFromSymbolStart = "388">
51+
</Location>
52+
</Locations>
53+
</BreakpointContent>
54+
</BreakpointProxy>
55+
</Breakpoints>
56+
</Bucket>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>SchemeUserState</key>
6+
<dict>
7+
<key>APIJSONTest.xcscheme_^#shared#^_</key>
8+
<dict>
9+
<key>orderHint</key>
10+
<integer>0</integer>
11+
</dict>
12+
</dict>
13+
</dict>
14+
</plist>
Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
1-
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2-
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="6211" systemVersion="14A298i" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="BYZ-38-t0r">
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="17506" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
3+
<device id="retina6_1" orientation="portrait" appearance="light"/>
34
<dependencies>
4-
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6204"/>
5+
<deployment identifier="iOS"/>
6+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="17505"/>
7+
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
58
</dependencies>
69
<scenes>
710
<!--View Controller-->
811
<scene sceneID="tne-QT-ifu">
912
<objects>
10-
<viewController id="BYZ-38-t0r" customClass="ViewController" customModuleProvider="target" sceneMemberID="viewController">
13+
<viewController id="BYZ-38-t0r" customClass="ViewController" customModule="APIJSONTest" customModuleProvider="target" sceneMemberID="viewController">
1114
<layoutGuides>
1215
<viewControllerLayoutGuide type="top" id="y3c-jy-aDJ"/>
1316
<viewControllerLayoutGuide type="bottom" id="wfy-db-euE"/>
1417
</layoutGuides>
1518
<view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
16-
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
19+
<rect key="frame" x="0.0" y="0.0" width="414" height="896"/>
1720
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
18-
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
21+
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
1922
</view>
2023
</viewController>
2124
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
2225
</objects>
26+
<point key="canvasLocation" x="24" y="-21"/>
2327
</scene>
2428
</scenes>
2529
</document>

APIJSON-iOS/APIJSON-Swift/APIJSONTest/ViewController.swift

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ class ViewController: UIViewController {
5252
requestLabel.numberOfLines = 6
5353
self.view.addSubview(requestLabel)
5454

55-
let responseLable = UILabel(frame:CGRect(x:20, y:130, width:400, height:600))
56-
responseLable.text = "request..."
57-
responseLable.numberOfLines = 100
58-
self.view.addSubview(responseLable)
55+
let responseLabel = UILabel(frame:CGRect(x:20, y:130, width:400, height:600))
56+
responseLabel.text = "request..."
57+
responseLabel.numberOfLines = 100
58+
self.view.addSubview(responseLabel)
5959

6060
//生成UI >>>>>>>>>>>>>>>>>>>>>
6161

@@ -65,23 +65,23 @@ class ViewController: UIViewController {
6565

6666
//请求URL
6767

68-
let request:NSMutableURLRequest = NSMutableURLRequest(URL: NSURL(string: url)!)
68+
let request = NSMutableURLRequest(url: URL(string: url)!)
6969

70-
request.HTTPMethod = "POST"
70+
request.httpMethod = "POST"
7171
//设置发送的数据格式为JSON
7272
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
7373
do {
74-
request.HTTPBody = try NSJSONSerialization.dataWithJSONObject(json, options: NSJSONWritingOptions.PrettyPrinted)
74+
request.httpBody = try JSONSerialization.data(withJSONObject: json, options: JSONSerialization.WritingOptions.prettyPrinted)
7575
} catch {
7676
print("Something went wrong!")
7777
}
7878

7979
//默认session配置
80-
let config = NSURLSessionConfiguration.defaultSessionConfiguration()
81-
let session = NSURLSession(configuration: config)
80+
let config = URLSessionConfiguration.default
81+
let session = URLSession(configuration: config)
8282

8383
//发起请求
84-
let dataTask = session.dataTaskWithRequest(request) { (data, response, error) in
84+
let dataTask = session.dataTask(with: request as URLRequest) { (data, response, error) in
8585

8686
print("\n\nreceived result!\n\n")
8787

@@ -90,30 +90,32 @@ class ViewController: UIViewController {
9090
print(error)
9191

9292
//数据类型转换
93-
let jsonData:NSDictionary = try! NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as! NSDictionary
93+
let jsonData:NSDictionary = try! JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as! NSDictionary
9494

9595
print(jsonData)
9696

97-
let res:String = self.toJSONString(jsonData);
97+
let res:String = self.toJSONString(jsonData as! [String : Any]);
9898
print("Response = \n" + res)
9999

100100

101101
//显示返回结果
102-
dispatch_async(dispatch_get_main_queue(), {
102+
DispatchQueue.main.async {
103103

104-
responseLable.text = "Response:\n" + res
104+
responseLabel.text = "Response:\n" + res
105105
print("set text end\n\n")
106-
});
106+
}
107107

108108
}
109109

110110
//请求开始
111111
dataTask.resume()
112112
}
113113

114-
func toJSONString(jsonData: NSDictionary!) -> String {
115-
let data : NSData! = try? NSJSONSerialization.dataWithJSONObject(jsonData, options: [NSJSONWritingOptions.PrettyPrinted]) as NSData!
116-
let str = String(data: data, encoding: NSUTF8StringEncoding)
114+
func toJSONString(_ jsonData: [String: Any]!) -> String {
115+
guard let data = try? JSONSerialization.data(withJSONObject: jsonData!, options: [JSONSerialization.WritingOptions.prettyPrinted]) else {
116+
return ""
117+
}
118+
let str = String(data: data, encoding: String.Encoding.utf8)
117119
return str!
118120
}
119121

0 commit comments

Comments
 (0)