Skip to content

Commit 5c93c29

Browse files
committed
Make NSCoding support optional. It clutters models for lots of people who don't use it.
1 parent 65e3b32 commit 5c93c29

File tree

4 files changed

+86
-65
lines changed

4 files changed

+86
-65
lines changed

SwiftyJSONAccelerator/Base Files/BaseTemplate.txt

Lines changed: 38 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -9,50 +9,43 @@ import Foundation{INCLUDE_SWIFTY}
99

1010
public {OBJECT_KIND} {OBJECT_NAME}: NSObject, NSCoding {
1111

12-
// MARK: Declaration for string constants to be used to decode and also serialize.
13-
{STRING_CONSTANT_BLOCK}
14-
15-
// MARK: Properties
16-
{PROPERTIES}
17-
18-
// MARK: Initalizers
19-
/**
20-
Initates the class based on the object
21-
- parameter object: The object of either Dictionary or Array kind that was passed.
22-
- returns: An initalized instance of the class.
23-
*/
24-
convenience public init(object: AnyObject) {
25-
self.init(json: JSON(object))
26-
}
27-
28-
/**
29-
Initates the class based on the JSON that was passed.
30-
- parameter json: JSON object from SwiftyJSON.
31-
- returns: An initalized instance of the class.
32-
*/
33-
public init(json: JSON) {
34-
{INITALIZER}
35-
}
36-
37-
/**
38-
Generates description of the object in the form of a NSDictionary.
39-
- returns: A Key value pair containing all valid values in the object.
40-
*/
41-
public func dictionaryRepresentation() -> [String : AnyObject ] {
42-
43-
var dictionary: [String : AnyObject ] = [ : ]
44-
{DESC}
45-
46-
return dictionary
47-
}
48-
49-
// MARK: NSCoding Protocol
50-
required public init(coder aDecoder: NSCoder) {
51-
{DECODERS}
52-
}
53-
54-
public func encodeWithCoder(aCoder: NSCoder) {
55-
{ENCODERS}
56-
}
12+
// MARK: Declaration for string constants to be used to decode and also serialize.
13+
{STRING_CONSTANT_BLOCK}
14+
15+
// MARK: Properties
16+
{PROPERTIES}
17+
18+
// MARK: Initalizers
19+
/**
20+
Initates the class based on the object
21+
- parameter object: The object of either Dictionary or Array kind that was passed.
22+
- returns: An initalized instance of the class.
23+
*/
24+
convenience public init(object: AnyObject) {
25+
self.init(json: JSON(object))
26+
}
27+
28+
/**
29+
Initates the class based on the JSON that was passed.
30+
- parameter json: JSON object from SwiftyJSON.
31+
- returns: An initalized instance of the class.
32+
*/
33+
public init(json: JSON) {
34+
{INITALIZER}
35+
}
36+
37+
/**
38+
Generates description of the object in the form of a NSDictionary.
39+
- returns: A Key value pair containing all valid values in the object.
40+
*/
41+
public func dictionaryRepresentation() -> [String : AnyObject ] {
42+
43+
var dictionary: [String : AnyObject ] = [ : ]
44+
{DESC}
45+
46+
return dictionary
47+
}
48+
49+
{NSCODING_SUPPORT}
5750

5851
}

SwiftyJSONAccelerator/Base.lproj/Main.storyboard

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2-
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="9059" systemVersion="15A284" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" initialViewController="B8D-0N-5wS">
2+
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="9059" systemVersion="15A282b" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" initialViewController="B8D-0N-5wS">
33
<dependencies>
44
<deployment identifier="macosx"/>
55
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="9059"/>
@@ -670,11 +670,11 @@
670670
<objects>
671671
<viewController title="SwiftyJSONAccelerator" id="XfG-lQ-9wD" customClass="SJEditorViewController" customModule="SwiftyJSONAccelerator" customModuleProvider="target" sceneMemberID="viewController">
672672
<view key="view" id="m2S-Jp-Qdl">
673-
<rect key="frame" x="0.0" y="0.0" width="717" height="628"/>
673+
<rect key="frame" x="0.0" y="0.0" width="717" height="645"/>
674674
<autoresizingMask key="autoresizingMask"/>
675675
<subviews>
676676
<button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="AwX-MF-DMD">
677-
<rect key="frame" x="501" y="126" width="212" height="36"/>
677+
<rect key="frame" x="501" y="143" width="212" height="36"/>
678678
<constraints>
679679
<constraint firstAttribute="width" constant="200" id="Fsx-eV-1MY"/>
680680
</constraints>
@@ -688,7 +688,7 @@
688688
</connections>
689689
</button>
690690
<scrollView horizontalLineScroll="10" horizontalPageScroll="10" verticalLineScroll="10" verticalPageScroll="10" hasHorizontalScroller="NO" usesPredominantAxisScrolling="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Gmg-Tp-PPn">
691-
<rect key="frame" x="0.0" y="165" width="717" height="463"/>
691+
<rect key="frame" x="0.0" y="182" width="717" height="463"/>
692692
<clipView key="contentView" id="V7d-R1-cDN">
693693
<rect key="frame" x="1" y="1" width="715" height="461"/>
694694
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
@@ -699,10 +699,10 @@
699699
<animations/>
700700
<color key="textColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
701701
<color key="backgroundColor" white="0.0" alpha="1" colorSpace="calibratedWhite"/>
702-
<size key="minSize" width="715" height="461"/>
702+
<size key="minSize" width="700" height="461"/>
703703
<size key="maxSize" width="717" height="10000000"/>
704704
<color key="insertionPointColor" name="controlLightHighlightColor" catalog="System" colorSpace="catalog"/>
705-
<size key="minSize" width="715" height="461"/>
705+
<size key="minSize" width="700" height="461"/>
706706
<size key="maxSize" width="717" height="10000000"/>
707707
</textView>
708708
</subviews>
@@ -716,13 +716,13 @@
716716
<animations/>
717717
</scroller>
718718
<scroller key="verticalScroller" verticalHuggingPriority="750" horizontal="NO" id="9fh-RH-upi">
719-
<rect key="frame" x="700" y="1" width="16" height="461"/>
719+
<rect key="frame" x="701" y="1" width="15" height="461"/>
720720
<autoresizingMask key="autoresizingMask"/>
721721
<animations/>
722722
</scroller>
723723
</scrollView>
724724
<imageView translatesAutoresizingMaskIntoConstraints="NO" id="PJA-AP-s5I">
725-
<rect key="frame" x="15" y="138" width="20" height="20"/>
725+
<rect key="frame" x="15" y="155" width="20" height="20"/>
726726
<constraints>
727727
<constraint firstAttribute="height" constant="20" id="48s-vB-xta"/>
728728
<constraint firstAttribute="width" constant="20" id="t6S-fb-bjE"/>
@@ -731,7 +731,7 @@
731731
<imageCell key="cell" refusesFirstResponder="YES" alignment="left" animates="YES" imageScaling="proportionallyDown" image="failure" id="CKF-pW-DBY"/>
732732
</imageView>
733733
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="GbJ-FJ-Uuc">
734-
<rect key="frame" x="41" y="132" width="460" height="26"/>
734+
<rect key="frame" x="41" y="149" width="460" height="26"/>
735735
<constraints>
736736
<constraint firstAttribute="height" constant="26" id="WGF-kR-SB9"/>
737737
</constraints>
@@ -743,7 +743,7 @@
743743
</textFieldCell>
744744
</textField>
745745
<textField verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="aJ1-yn-Pl7">
746-
<rect key="frame" x="13" y="106" width="692" height="22"/>
746+
<rect key="frame" x="13" y="123" width="692" height="22"/>
747747
<animations/>
748748
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" selectable="YES" editable="YES" sendsActionOnEndEditing="YES" state="on" borderStyle="bezel" title="BaseClass" placeholderString="Base Class Name" bezelStyle="round" id="vuP-t4-ep6">
749749
<font key="font" metaFont="system"/>
@@ -752,7 +752,7 @@
752752
</textFieldCell>
753753
</textField>
754754
<textField verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="b3c-Wz-v36">
755-
<rect key="frame" x="13" y="82" width="692" height="22"/>
755+
<rect key="frame" x="13" y="99" width="692" height="22"/>
756756
<animations/>
757757
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" selectable="YES" editable="YES" sendsActionOnEndEditing="YES" state="on" borderStyle="bezel" placeholderString="Class Prefix (For example, NS)" bezelStyle="round" id="N8s-m9-cf7">
758758
<font key="font" metaFont="system"/>
@@ -761,7 +761,7 @@
761761
</textFieldCell>
762762
</textField>
763763
<textField verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="sNu-7Z-F06">
764-
<rect key="frame" x="13" y="58" width="692" height="22"/>
764+
<rect key="frame" x="13" y="75" width="692" height="22"/>
765765
<animations/>
766766
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" selectable="YES" editable="YES" sendsActionOnEndEditing="YES" state="on" borderStyle="bezel" placeholderString="Company name" bezelStyle="round" id="76e-Gm-0jI">
767767
<font key="font" metaFont="system"/>
@@ -770,7 +770,7 @@
770770
</textFieldCell>
771771
</textField>
772772
<textField verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="mJg-3P-2Aw">
773-
<rect key="frame" x="13" y="34" width="692" height="22"/>
773+
<rect key="frame" x="13" y="51" width="692" height="22"/>
774774
<animations/>
775775
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" selectable="YES" editable="YES" sendsActionOnEndEditing="YES" state="on" borderStyle="bezel" placeholderString="Author name" bezelStyle="round" id="jUe-0u-QLL">
776776
<font key="font" metaFont="system"/>
@@ -779,18 +779,26 @@
779779
</textFieldCell>
780780
</textField>
781781
<button translatesAutoresizingMaskIntoConstraints="NO" id="uZa-9r-6Xo">
782-
<rect key="frame" x="13" y="10" width="265" height="18"/>
782+
<rect key="frame" x="13" y="27" width="265" height="18"/>
783783
<animations/>
784784
<buttonCell key="cell" type="check" title="Add &quot;import SwiftyJSON&quot; in the models." bezelStyle="regularSquare" imagePosition="left" state="on" inset="2" id="tJK-cs-wrm">
785785
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
786786
<font key="font" metaFont="system"/>
787787
</buttonCell>
788788
</button>
789+
<button translatesAutoresizingMaskIntoConstraints="NO" id="or1-PD-m0L">
790+
<rect key="frame" x="13" y="7" width="265" height="18"/>
791+
<animations/>
792+
<buttonCell key="cell" type="check" title="Support NSCoding" bezelStyle="regularSquare" imagePosition="left" state="on" inset="2" id="YUy-wn-qL0">
793+
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
794+
<font key="font" metaFont="system"/>
795+
</buttonCell>
796+
</button>
789797
</subviews>
790798
<constraints>
791799
<constraint firstAttribute="trailing" secondItem="AwX-MF-DMD" secondAttribute="trailing" constant="10" id="1wF-Ly-8ue"/>
792-
<constraint firstAttribute="bottom" secondItem="uZa-9r-6Xo" secondAttribute="bottom" constant="12" id="33g-aa-kCl"/>
793800
<constraint firstItem="aJ1-yn-Pl7" firstAttribute="leading" secondItem="mJg-3P-2Aw" secondAttribute="leading" id="35V-f8-AGH"/>
801+
<constraint firstItem="or1-PD-m0L" firstAttribute="top" secondItem="uZa-9r-6Xo" secondAttribute="bottom" constant="6" id="8NF-CF-XQV"/>
794802
<constraint firstItem="b3c-Wz-v36" firstAttribute="width" secondItem="mJg-3P-2Aw" secondAttribute="width" id="8wA-lO-d5e"/>
795803
<constraint firstItem="aJ1-yn-Pl7" firstAttribute="width" secondItem="mJg-3P-2Aw" secondAttribute="width" id="9m4-gy-MnL"/>
796804
<constraint firstAttribute="trailing" secondItem="Gmg-Tp-PPn" secondAttribute="trailing" id="APF-Xg-5Am"/>
@@ -800,10 +808,13 @@
800808
<constraint firstItem="GbJ-FJ-Uuc" firstAttribute="top" secondItem="PJA-AP-s5I" secondAttribute="top" id="IlE-F0-NZD"/>
801809
<constraint firstItem="aJ1-yn-Pl7" firstAttribute="top" secondItem="AwX-MF-DMD" secondAttribute="bottom" constant="5" id="KM7-XP-CAY"/>
802810
<constraint firstItem="Gmg-Tp-PPn" firstAttribute="top" secondItem="m2S-Jp-Qdl" secondAttribute="top" id="KPd-r3-s21"/>
811+
<constraint firstAttribute="bottom" secondItem="or1-PD-m0L" secondAttribute="bottom" constant="9" id="Klb-Me-gMO"/>
812+
<constraint firstItem="or1-PD-m0L" firstAttribute="width" secondItem="uZa-9r-6Xo" secondAttribute="width" id="OdK-JX-tBs"/>
803813
<constraint firstItem="b3c-Wz-v36" firstAttribute="leading" secondItem="mJg-3P-2Aw" secondAttribute="leading" id="Q9g-6W-fq3"/>
804814
<constraint firstItem="sNu-7Z-F06" firstAttribute="top" secondItem="b3c-Wz-v36" secondAttribute="bottom" constant="2" id="Svz-9v-6zt"/>
805815
<constraint firstItem="PJA-AP-s5I" firstAttribute="leading" secondItem="m2S-Jp-Qdl" secondAttribute="leading" constant="15" id="ZKf-XD-X3C"/>
806816
<constraint firstItem="uZa-9r-6Xo" firstAttribute="leading" secondItem="m2S-Jp-Qdl" secondAttribute="leading" constant="15" id="ZT5-69-AWH"/>
817+
<constraint firstItem="or1-PD-m0L" firstAttribute="leading" secondItem="uZa-9r-6Xo" secondAttribute="leading" id="aLP-RS-Gyu"/>
807818
<constraint firstItem="AwX-MF-DMD" firstAttribute="top" secondItem="Gmg-Tp-PPn" secondAttribute="bottom" constant="7" id="cHK-Sj-6ps"/>
808819
<constraint firstItem="uZa-9r-6Xo" firstAttribute="top" secondItem="mJg-3P-2Aw" secondAttribute="bottom" constant="8" id="dno-LM-r3L"/>
809820
<constraint firstItem="AwX-MF-DMD" firstAttribute="top" secondItem="PJA-AP-s5I" secondAttribute="top" id="ds3-Tk-TKg"/>
@@ -826,12 +837,13 @@
826837
<outlet property="includeSwiftyCheckbox" destination="uZa-9r-6Xo" id="r84-Fc-xwG"/>
827838
<outlet property="messageLabel" destination="GbJ-FJ-Uuc" id="19Z-0L-YwW"/>
828839
<outlet property="prefixClassTextField" destination="b3c-Wz-v36" id="3wr-bV-AAf"/>
840+
<outlet property="supportNSCodingCheckbox" destination="or1-PD-m0L" id="wyH-nh-iSo"/>
829841
<outlet property="textView" destination="fWl-7Q-Cq6" id="1lh-CW-y1Y"/>
830842
</connections>
831843
</viewController>
832844
<customObject id="rPt-NT-nkU" userLabel="First Responder" customClass="NSResponder" sceneMemberID="firstResponder"/>
833845
</objects>
834-
<point key="canvasLocation" x="151.5" y="651"/>
846+
<point key="canvasLocation" x="819.5" y="504.5"/>
835847
</scene>
836848
</scenes>
837849
<resources>

SwiftyJSONAccelerator/SJEditorViewController.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class SJEditorViewController: NSViewController, NSTextViewDelegate {
2020
@IBOutlet var companyNameTextField: NSTextField?
2121
@IBOutlet var authorNameTextField: NSTextField?
2222
@IBOutlet var includeSwiftyCheckbox: NSButton?
23+
@IBOutlet var supportNSCodingCheckbox: NSButton!
2324

2425
// MARK: View methods
2526
override func loadView() {
@@ -101,7 +102,9 @@ class SJEditorViewController: NSViewController, NSTextViewDelegate {
101102
if object != nil {
102103

103104
let swiftyState = self.includeSwiftyCheckbox?.state == 1 ? true : false
104-
let generator: ModelGenerator = ModelGenerator.init(baseContent: JSON(object!), prefix: prefixClassTextField?.stringValue, baseClassName: (baseClassTextField?.stringValue)!, authorName: authorNameTextField?.stringValue, companyName: companyNameTextField?.stringValue, type: ModelType.kClassType, filePath: filePath!, includeSwiftyJSON: swiftyState)
105+
let nscodingState = self.supportNSCodingCheckbox?.state == 1 ? true : false
106+
107+
let generator: ModelGenerator = ModelGenerator.init(baseContent: JSON(object!), prefix: prefixClassTextField?.stringValue, baseClassName: (baseClassTextField?.stringValue)!, authorName: authorNameTextField?.stringValue, companyName: companyNameTextField?.stringValue, type: ModelType.kClassType, filePath: filePath!, includeSwiftyJSON: swiftyState, supportNSCoding: nscodingState)
105108
generator.generate()
106109
} else {
107110
let alert:NSAlert = NSAlert()

0 commit comments

Comments
 (0)