Skip to content

Commit

Permalink
Complete code
Browse files Browse the repository at this point in the history
  • Loading branch information
orihpt committed Nov 9, 2020
1 parent 06a8ebb commit 6a7184c
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 7 deletions.
76 changes: 70 additions & 6 deletions Encodable/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,24 +1,88 @@
<?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="17156" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="0EQ-41-sqT">
<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="17126"/>
<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-->
<!--Person-->
<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="Encodable" 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>
<stackView opaque="NO" contentMode="scaleToFill" axis="vertical" spacing="10" translatesAutoresizingMaskIntoConstraints="NO" id="XiI-if-aU8">
<rect key="frame" x="190" y="413" width="34" height="70"/>
<subviews>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="p13-Iq-nre">
<rect key="frame" x="0.0" y="0.0" width="34" height="30"/>
<state key="normal" title="Save"/>
<connections>
<action selector="saveButtonPressed:" destination="BYZ-38-t0r" eventType="touchUpInside" id="RD3-8G-BA9"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="gwU-pe-Vli">
<rect key="frame" x="0.0" y="40" width="34" height="30"/>
<state key="normal" title="Load"/>
<connections>
<action selector="loadButtonPressed:" destination="BYZ-38-t0r" eventType="touchUpInside" id="Slz-na-PS0"/>
</connections>
</button>
</subviews>
</stackView>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Information label" textAlignment="center" lineBreakMode="tailTruncation" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="lom-xL-qCP">
<rect key="frame" x="20" y="488" width="374" height="20.5"/>
<fontDescription key="fontDescription" style="UICTFontTextStyleHeadline"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<viewLayoutGuide key="safeArea" id="6Tk-OE-BBY"/>
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
<constraints>
<constraint firstItem="XiI-if-aU8" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="26p-Ar-Que"/>
<constraint firstItem="lom-xL-qCP" firstAttribute="top" secondItem="XiI-if-aU8" secondAttribute="bottom" constant="5" id="CIp-gg-h9D"/>
<constraint firstItem="6Tk-OE-BBY" firstAttribute="trailing" secondItem="lom-xL-qCP" secondAttribute="trailing" constant="20" id="K4i-f4-qkr"/>
<constraint firstItem="XiI-if-aU8" firstAttribute="centerY" secondItem="8bC-Xf-vdC" secondAttribute="centerY" id="hAV-tj-n9q"/>
<constraint firstItem="lom-xL-qCP" firstAttribute="leading" secondItem="6Tk-OE-BBY" secondAttribute="leading" constant="20" id="nOi-3E-gr5"/>
</constraints>
</view>
<navigationItem key="navigationItem" title="Person" id="j6R-Ui-tbN"/>
<connections>
<outlet property="infoLabel" destination="lom-xL-qCP" id="WoO-Zd-ZCB"/>
<outlet property="loadButton" destination="gwU-pe-Vli" id="Zey-Mm-InH"/>
<outlet property="saveButton" destination="p13-Iq-nre" id="hw2-gg-N8w"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="-196" y="94"/>
</scene>
<!--Navigation Controller-->
<scene sceneID="RRl-Cn-VHq">
<objects>
<navigationController id="0EQ-41-sqT" sceneMemberID="viewController">
<navigationBar key="navigationBar" contentMode="scaleToFill" insetsLayoutMarginsFromSafeArea="NO" id="3oU-fi-yEo">
<rect key="frame" x="0.0" y="44" width="414" height="44"/>
<autoresizingMask key="autoresizingMask"/>
</navigationBar>
<connections>
<segue destination="BYZ-38-t0r" kind="relationship" relationship="rootViewController" id="Ww7-Rj-MTh"/>
</connections>
</navigationController>
<placeholder placeholderIdentifier="IBFirstResponder" id="JKo-9s-pvG" userLabel="First Responder" customClass="UIResponder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="-997" y="94"/>
</scene>
</scenes>
<resources>
<systemColor name="systemBackgroundColor">
<color white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</systemColor>
</resources>
</document>
6 changes: 6 additions & 0 deletions Encodable/Code/Person.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,10 @@ struct Person: Encodable, Decodable {
var firstName: String
var lastName: String
var birthday: Date

init() {
self.firstName = "Tim"
self.lastName = "Cook"
self.birthday = Date()
}
}
36 changes: 35 additions & 1 deletion Encodable/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,45 @@ import UIKit

class ViewController: UIViewController {

@IBOutlet weak var saveButton: UIButton!
@IBOutlet weak var loadButton: UIButton!
@IBOutlet weak var infoLabel: UILabel!

var person: Person? = nil

override func viewDidLoad() {
super.viewDidLoad()
self.infoLabel.text = ""
// Do any additional setup after loading the view.
}


@IBAction func saveButtonPressed(_ sender: Any) {
print("Saving object...")
let person: Person = Person()

do {
try UserDefaults.standard.setToObject(person, forKey: "person")
print("Object saved successfully")
} catch let err {
infoLabel.textColor = .red
infoLabel.text = err.localizedDescription
print("Error while saving object:\n\(err.localizedDescription)")
}

}

@IBAction func loadButtonPressed(_ sender: Any) {
print("Loading object...")

do {
self.person = try UserDefaults.standard.getToObject(forKey: "person", castTo: Person.self)
print("Successfully load object:\n\(self.person!)")
} catch let err {
infoLabel.textColor = .red
infoLabel.text = err.localizedDescription
print("Error while loading object:\n\(err.localizedDescription)")
}
}

}

0 comments on commit 6a7184c

Please sign in to comment.