Skip to content

Commit

Permalink
[Feat] .json 파일 구현 (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
pcsoyeon committed May 28, 2022
1 parent 6d58bb3 commit 3cdbd7c
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 6 deletions.
16 changes: 16 additions & 0 deletions Seminar/7th-Seminar/7th-Seminar.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
92DD4FC22841F2AA0020CABF /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 92DD4FC02841F2AA0020CABF /* Main.storyboard */; };
92DD4FC42841F2AB0020CABF /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 92DD4FC32841F2AB0020CABF /* Assets.xcassets */; };
92DD4FC72841F2AB0020CABF /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 92DD4FC52841F2AB0020CABF /* LaunchScreen.storyboard */; };
92DD4FCF2841F3E70020CABF /* Todo.json in Resources */ = {isa = PBXBuildFile; fileRef = 92DD4FCE2841F3E70020CABF /* Todo.json */; };
92DD4FD12841F49D0020CABF /* MockParser.swift in Sources */ = {isa = PBXBuildFile; fileRef = 92DD4FD02841F49D0020CABF /* MockParser.swift */; };
92DD4FD32841F65F0020CABF /* TodoManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 92DD4FD22841F65F0020CABF /* TodoManager.swift */; };
92DD4FD52841F6830020CABF /* Todo.swift in Sources */ = {isa = PBXBuildFile; fileRef = 92DD4FD42841F6830020CABF /* Todo.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -24,6 +28,10 @@
92DD4FC32841F2AB0020CABF /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
92DD4FC62841F2AB0020CABF /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
92DD4FC82841F2AB0020CABF /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
92DD4FCE2841F3E70020CABF /* Todo.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = Todo.json; sourceTree = "<group>"; };
92DD4FD02841F49D0020CABF /* MockParser.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockParser.swift; sourceTree = "<group>"; };
92DD4FD22841F65F0020CABF /* TodoManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TodoManager.swift; sourceTree = "<group>"; };
92DD4FD42841F6830020CABF /* Todo.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Todo.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -63,6 +71,10 @@
92DD4FC32841F2AB0020CABF /* Assets.xcassets */,
92DD4FC52841F2AB0020CABF /* LaunchScreen.storyboard */,
92DD4FC82841F2AB0020CABF /* Info.plist */,
92DD4FCE2841F3E70020CABF /* Todo.json */,
92DD4FD02841F49D0020CABF /* MockParser.swift */,
92DD4FD22841F65F0020CABF /* TodoManager.swift */,
92DD4FD42841F6830020CABF /* Todo.swift */,
);
path = "7th-Seminar";
sourceTree = "<group>";
Expand Down Expand Up @@ -127,6 +139,7 @@
files = (
92DD4FC72841F2AB0020CABF /* LaunchScreen.storyboard in Resources */,
92DD4FC42841F2AB0020CABF /* Assets.xcassets in Resources */,
92DD4FCF2841F3E70020CABF /* Todo.json in Resources */,
92DD4FC22841F2AA0020CABF /* Main.storyboard in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -138,8 +151,11 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
92DD4FD32841F65F0020CABF /* TodoManager.swift in Sources */,
92DD4FD52841F6830020CABF /* Todo.swift in Sources */,
92DD4FBF2841F2AA0020CABF /* ViewController.swift in Sources */,
92DD4FBB2841F2AA0020CABF /* AppDelegate.swift in Sources */,
92DD4FD12841F49D0020CABF /* MockParser.swift in Sources */,
92DD4FBD2841F2AA0020CABF /* SceneDelegate.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
33 changes: 28 additions & 5 deletions Seminar/7th-Seminar/7th-Seminar/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,24 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="13122.16" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="20037" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<device id="retina6_1" orientation="portrait" appearance="light"/>
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13104.12"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="20020"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="System colors in document resources" minToolsVersion="11.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--View Controller-->
<scene sceneID="tne-QT-ifu">
<objects>
<viewController id="BYZ-38-t0r" customClass="ViewController" customModuleProvider="target" sceneMemberID="viewController">
<viewController id="BYZ-38-t0r" customClass="ViewController" customModule="_th_Seminar" customModuleProvider="target" sceneMemberID="viewController">
<view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<rect key="frame" x="0.0" y="0.0" width="414" height="896"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" xcode11CocoaTouchSystemColor="systemBackgroundColor" cocoaTouchSystemColor="whiteColor"/>
<subviews>
<tableView clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="-1" estimatedRowHeight="-1" sectionHeaderHeight="-1" estimatedSectionHeaderHeight="-1" sectionFooterHeight="-1" estimatedSectionFooterHeight="-1" translatesAutoresizingMaskIntoConstraints="NO" id="Tlk-7c-rpQ">
<rect key="frame" x="0.0" y="44" width="414" height="818"/>
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
</tableView>
</subviews>
<viewLayoutGuide key="safeArea" id="6Tk-OE-BBY"/>
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
<constraints>
<constraint firstItem="6Tk-OE-BBY" firstAttribute="trailing" secondItem="Tlk-7c-rpQ" secondAttribute="trailing" id="AXU-se-p8X"/>
<constraint firstItem="6Tk-OE-BBY" firstAttribute="bottom" secondItem="Tlk-7c-rpQ" secondAttribute="bottom" id="EzY-Zk-Yrh"/>
<constraint firstItem="Tlk-7c-rpQ" firstAttribute="leading" secondItem="6Tk-OE-BBY" secondAttribute="leading" id="j4L-ls-EQf"/>
<constraint firstItem="Tlk-7c-rpQ" firstAttribute="top" secondItem="6Tk-OE-BBY" secondAttribute="top" id="rNG-ZJ-SuC"/>
</constraints>
</view>
<connections>
<outlet property="tableView" destination="Tlk-7c-rpQ" id="mwc-E2-ZtW"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="35" y="98"/>
</scene>
</scenes>
<resources>
<systemColor name="systemBackgroundColor">
<color white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</systemColor>
</resources>
</document>
30 changes: 30 additions & 0 deletions Seminar/7th-Seminar/7th-Seminar/MockParser.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// MockParser.swift
// 7th-Seminar
//
// Created by 소연 on 2022/05/28.
//

import Foundation

final class MockParser {
static func load<D: Codable>(_ type: D.Type, from resourceName: String) -> D? {
guard let path = Bundle.main.path(forResource: resourceName, ofType: "json") else {
return nil
}

guard let jsonString = try? String(contentsOfFile: path) else {
return nil
}

print(jsonString)

let decoder = JSONDecoder()
let data = jsonString.data(using: .utf8)
print(data)

guard let data = data else { return nil }
return try? decoder.decode(type, from: data)
}
}

26 changes: 26 additions & 0 deletions Seminar/7th-Seminar/7th-Seminar/Todo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[
{
"userId": 1,
"id": 1,
"title": "delectus aut autem",
"completed": false
},
{
"userId": 1,
"id": 2,
"title": "quis ut nam facilis et officia qui",
"completed": false
},
{
"userId": 1,
"id": 3,
"title": "fugiat veniam minus",
"completed": false
},
{
"userId": 1,
"id": 4,
"title": "et porro tempora",
"completed": true
},
]
15 changes: 15 additions & 0 deletions Seminar/7th-Seminar/7th-Seminar/Todo.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// Todo.swift
// 7th-Seminar
//
// Created by 소연 on 2022/05/28.
//

import Foundation

struct Todo: Codable {
let userId: Int
let id: Int
let title: String
let completed: Bool
}
20 changes: 20 additions & 0 deletions Seminar/7th-Seminar/7th-Seminar/TodoManager.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// TodoManager.swift
// 7th-Seminar
//
// Created by 소연 on 2022/05/28.
//

import Foundation

final class TodoManager {
// service code
static let shared = TodoManager()
private init() {}
}

extension TodoManager {
func getTodoMock() -> [Todo] {
return MockParser.load([Todo].self, from: "Todo")!
}
}
25 changes: 24 additions & 1 deletion Seminar/7th-Seminar/7th-Seminar/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,35 @@
import UIKit

class ViewController: UIViewController {

@IBOutlet weak var tableView: UITableView!

var todos: [Todo] = [] {
didSet {
print("new value update")
}
}

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.

tableView.dataSource = self

guard let data = MockParser.load([Todo].self, from: "Todo") else { return }
print(data)
}


}

extension ViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
todos.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = todos[indexPath.row].title
return cell
}
}

0 comments on commit 3cdbd7c

Please sign in to comment.