Skip to content

Commit 7a16f89

Browse files
author
HaRi
committed
2017.08.16 今日新增: UIKit-读取json文件&重载;
1 parent 7916774 commit 7a16f89

35 files changed

+1398
-2
lines changed

README.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,14 @@ RxSwift使用教程
101101
- 5-RxSwift资源释放
102102
- 6-RxSwift的UITableVIew使用
103103

104+
105+
UIKit实战补充
106+
===
107+
- 读取json文件&重载
108+
104109
### 今日更新
105-
- 2017.08.10 更新
106-
- 元组最佳实践
110+
- 2017.08.16 更新
111+
- 读取json文件&重载
107112

108113
## 更新中...
109114
###### Tips: UIKit 和 UIFoundation 部分接下来会通过 直播实战项目逐步学习!

UIKit/读取json文件&重载/SwiftJsonTest/SwiftJsonTest.xcodeproj/project.pbxproj

+646
Large diffs are not rendered by default.

UIKit/读取json文件&重载/SwiftJsonTest/SwiftJsonTest.xcodeproj/project.xcworkspace/contents.xcworkspacedata

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//
2+
// AppDelegate.swift
3+
// SwiftJsonTest
4+
//
5+
// Created by 韩俊强 on 2017/8/14.
6+
// Copyright © 2017年 HaRi. All rights reserved.
7+
//
8+
9+
import UIKit
10+
11+
@UIApplicationMain
12+
class AppDelegate: UIResponder, UIApplicationDelegate {
13+
14+
var window: UIWindow?
15+
16+
17+
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
18+
19+
let tabBarVC = MyTabBarController()
20+
self.window?.rootViewController = tabBarVC
21+
22+
return true
23+
}
24+
25+
func applicationWillResignActive(_ application: UIApplication) {
26+
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
27+
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
28+
}
29+
30+
func applicationDidEnterBackground(_ application: UIApplication) {
31+
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
32+
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
33+
}
34+
35+
func applicationWillEnterForeground(_ application: UIApplication) {
36+
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
37+
}
38+
39+
func applicationDidBecomeActive(_ application: UIApplication) {
40+
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
41+
}
42+
43+
func applicationWillTerminate(_ application: UIApplication) {
44+
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
45+
}
46+
47+
48+
}
49+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
{
2+
"images" : [
3+
{
4+
"idiom" : "iphone",
5+
"size" : "29x29",
6+
"scale" : "2x"
7+
},
8+
{
9+
"idiom" : "iphone",
10+
"size" : "29x29",
11+
"scale" : "3x"
12+
},
13+
{
14+
"idiom" : "iphone",
15+
"size" : "40x40",
16+
"scale" : "2x"
17+
},
18+
{
19+
"idiom" : "iphone",
20+
"size" : "40x40",
21+
"scale" : "3x"
22+
},
23+
{
24+
"idiom" : "iphone",
25+
"size" : "60x60",
26+
"scale" : "2x"
27+
},
28+
{
29+
"idiom" : "iphone",
30+
"size" : "60x60",
31+
"scale" : "3x"
32+
},
33+
{
34+
"idiom" : "ipad",
35+
"size" : "29x29",
36+
"scale" : "1x"
37+
},
38+
{
39+
"idiom" : "ipad",
40+
"size" : "29x29",
41+
"scale" : "2x"
42+
},
43+
{
44+
"idiom" : "ipad",
45+
"size" : "40x40",
46+
"scale" : "1x"
47+
},
48+
{
49+
"idiom" : "ipad",
50+
"size" : "40x40",
51+
"scale" : "2x"
52+
},
53+
{
54+
"idiom" : "ipad",
55+
"size" : "76x76",
56+
"scale" : "1x"
57+
},
58+
{
59+
"idiom" : "ipad",
60+
"size" : "76x76",
61+
"scale" : "2x"
62+
}
63+
],
64+
"info" : {
65+
"version" : 1,
66+
"author" : "xcode"
67+
}
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="11134" systemVersion="15F34" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="01J-lp-oVM">
3+
<dependencies>
4+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="11106"/>
5+
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
6+
</dependencies>
7+
<scenes>
8+
<!--View Controller-->
9+
<scene sceneID="EHf-IW-A2E">
10+
<objects>
11+
<viewController id="01J-lp-oVM" sceneMemberID="viewController">
12+
<layoutGuides>
13+
<viewControllerLayoutGuide type="top" id="Llm-lL-Icb"/>
14+
<viewControllerLayoutGuide type="bottom" id="xb3-aO-Qok"/>
15+
</layoutGuides>
16+
<view key="view" contentMode="scaleToFill" id="Ze5-6b-2t3">
17+
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
18+
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
19+
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
20+
</view>
21+
</viewController>
22+
<placeholder placeholderIdentifier="IBFirstResponder" id="iYj-Kq-Ea1" userLabel="First Responder" sceneMemberID="firstResponder"/>
23+
</objects>
24+
<point key="canvasLocation" x="53" y="375"/>
25+
</scene>
26+
</scenes>
27+
</document>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="12121" systemVersion="16G29" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
3+
<device id="retina4_7" orientation="portrait">
4+
<adaptation id="fullscreen"/>
5+
</device>
6+
<dependencies>
7+
<deployment identifier="iOS"/>
8+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="12089"/>
9+
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
10+
</dependencies>
11+
<scenes>
12+
<!--View Controller-->
13+
<scene sceneID="tne-QT-ifu">
14+
<objects>
15+
<viewController id="BYZ-38-t0r" customClass="ViewController" customModule="SwiftJsonTest" customModuleProvider="target" sceneMemberID="viewController">
16+
<layoutGuides>
17+
<viewControllerLayoutGuide type="top" id="y3c-jy-aDJ"/>
18+
<viewControllerLayoutGuide type="bottom" id="wfy-db-euE"/>
19+
</layoutGuides>
20+
<view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
21+
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
22+
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
23+
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
24+
</view>
25+
<navigationItem key="navigationItem" id="7nZ-Zm-zJO"/>
26+
</viewController>
27+
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
28+
</objects>
29+
<point key="canvasLocation" x="1952.8" y="772.26386806596713"/>
30+
</scene>
31+
</scenes>
32+
</document>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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>CFBundleDevelopmentRegion</key>
6+
<string>en</string>
7+
<key>CFBundleExecutable</key>
8+
<string>$(EXECUTABLE_NAME)</string>
9+
<key>CFBundleIdentifier</key>
10+
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
11+
<key>CFBundleInfoDictionaryVersion</key>
12+
<string>6.0</string>
13+
<key>CFBundleName</key>
14+
<string>$(PRODUCT_NAME)</string>
15+
<key>CFBundlePackageType</key>
16+
<string>APPL</string>
17+
<key>CFBundleShortVersionString</key>
18+
<string>1.0</string>
19+
<key>CFBundleVersion</key>
20+
<string>1</string>
21+
<key>LSRequiresIPhoneOS</key>
22+
<true/>
23+
<key>UILaunchStoryboardName</key>
24+
<string>LaunchScreen</string>
25+
<key>UIMainStoryboardFile</key>
26+
<string>Main</string>
27+
<key>UIRequiredDeviceCapabilities</key>
28+
<array>
29+
<string>armv7</string>
30+
</array>
31+
<key>UISupportedInterfaceOrientations</key>
32+
<array>
33+
<string>UIInterfaceOrientationPortrait</string>
34+
</array>
35+
<key>UISupportedInterfaceOrientations~ipad</key>
36+
<array>
37+
<string>UIInterfaceOrientationPortrait</string>
38+
<string>UIInterfaceOrientationPortraitUpsideDown</string>
39+
<string>UIInterfaceOrientationLandscapeLeft</string>
40+
<string>UIInterfaceOrientationLandscapeRight</string>
41+
</array>
42+
</dict>
43+
</plist>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
//
2+
// MyTabBarController.swift
3+
// SwiftJsonTest
4+
//
5+
// Created by 韩俊强 on 2017/8/14.
6+
// Copyright © 2017年 HaRi. All rights reserved.
7+
//
8+
9+
import UIKit
10+
11+
class MyTabBarController: UITabBarController {
12+
13+
override func viewDidLoad() {
14+
super.viewDidLoad()
15+
16+
/*
17+
swift支持方法的重载;
18+
方法的重载:方法名称相同,但是参数不同.包括:
19+
1.参数的类型不同;
20+
2.参数的个数不同.
21+
*/
22+
23+
// 1.获取json文件路径
24+
guard let jsonPath = Bundle.main.path(forResource: "MainVCSettings.json", ofType: nil) else {
25+
print("没有获取到对应的文件路径")
26+
return
27+
}
28+
29+
// 2.读取json文件中的内容
30+
guard let jsonData = NSData.init(contentsOfFile: jsonPath) else {
31+
print("没有获取到json文件中的数据")
32+
return
33+
}
34+
35+
// 3.将Data转成数组
36+
// 如果在调用系统某个方法时,该方法最后有一个throws,说明该方法会抛出异常,那么需要对该异常进行处理
37+
guard let anyObject = try? JSONSerialization.jsonObject(with: jsonData as Data, options: .mutableContainers) else {
38+
return
39+
}
40+
41+
guard let dicArray = anyObject as?[[String : AnyObject]] else {
42+
return
43+
}
44+
45+
// 4.遍历字典, 获取对应信息
46+
for dict in dicArray {
47+
// 4.1.获取控制器对应的字符串
48+
guard let vcName = dict["vcName"] as? String else {
49+
continue
50+
}
51+
52+
// 4.2.获取控制器显示的title
53+
guard let title = dict["title"] as? String else {
54+
continue
55+
}
56+
57+
// 4.3.获取控制器显示的图标名称
58+
guard let imageName = dict["imageName"] as? String else {
59+
continue
60+
}
61+
62+
// 4.4.添加子控制器
63+
addChildViewController(childVCName: vcName, title: title, imageName: imageName)
64+
}
65+
}
66+
67+
// 方法重载
68+
private func addChildViewController(childVCName : String, title : String, imageName : String) {
69+
70+
// NSClassFromString("这里的名字是SwiftJsonTest.HomeViewController") // 根据字符串获取对应的Class:命名空间+.ViewController的名字
71+
72+
// 如何修改命名空间 Target -> Build Settings -> Product Name, 可以在这里修改命名空间的名称,但是要注意,新修改的命名空间不能有中文,不能以数字开头并且不能包含"-"符号
73+
74+
// 1.获取命名空间
75+
guard let nameSpace = Bundle.main.infoDictionary!["CFBundleExecutable"] as? String else {
76+
print("没有命名空间")
77+
return
78+
}
79+
80+
// 2.根据字符串获取对应的Class
81+
guard let childVCClass = NSClassFromString(nameSpace + "." + childVCName) else {
82+
print("没有获取到字符串对应的Class")
83+
return
84+
}
85+
86+
// 3.将对应的AnyObject转成控制器的类型
87+
guard let childVCType = childVCClass as? UIViewController.Type else {
88+
print("没有获取到对应控制器的类型")
89+
return
90+
}
91+
92+
// 4.创建对应的控制器对象
93+
let childVC = childVCType.init()
94+
95+
// 5.设置子控制器的属性
96+
childVC.title = title
97+
childVC.tabBarItem.image = UIImage(named: "\(imageName)_unselected")
98+
childVC.tabBarItem.selectedImage = UIImage(named: "\(imageName)_selected")
99+
childVC.view.backgroundColor = UIColor.red
100+
101+
// 6.包装导航控制器
102+
let childNav = UINavigationController(rootViewController: childVC)
103+
104+
// 7.添加控制器
105+
addChildViewController(childNav)
106+
107+
}
108+
109+
override func didReceiveMemoryWarning() {
110+
super.didReceiveMemoryWarning()
111+
// Dispose of any resources that can be recreated.
112+
}
113+
114+
115+
/*
116+
// MARK: - Navigation
117+
118+
// In a storyboard-based application, you will often want to do a little preparation before navigation
119+
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
120+
// Get the new view controller using segue.destinationViewController.
121+
// Pass the selected object to the new view controller.
122+
}
123+
*/
124+
125+
}

0 commit comments

Comments
 (0)