Skip to content

Commit

Permalink
updating to xcode 6.3
Browse files Browse the repository at this point in the history
  • Loading branch information
SamuelKlein committed Apr 13, 2015
1 parent dec59ef commit 9b74e3f
Show file tree
Hide file tree
Showing 7 changed files with 1,360 additions and 397 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDESourceControlProjectFavoriteDictionaryKey</key>
<false/>
<key>IDESourceControlProjectIdentifier</key>
<string>E1D7F84A-E66D-4F58-8870-22A55ED624A7</string>
<key>IDESourceControlProjectName</key>
<string>SwiftJson</string>
<key>IDESourceControlProjectOriginsDictionary</key>
<dict>
<key>957C2411939784F97902D82EAC722A5A882F3E35</key>
<string>https://github.com/swiftjson/SwiftJson</string>
</dict>
<key>IDESourceControlProjectPath</key>
<string>SwiftJson.xcodeproj</string>
<key>IDESourceControlProjectRelativeInstallPathDictionary</key>
<dict>
<key>957C2411939784F97902D82EAC722A5A882F3E35</key>
<string>../..</string>
</dict>
<key>IDESourceControlProjectURL</key>
<string>https://github.com/swiftjson/SwiftJson</string>
<key>IDESourceControlProjectVersion</key>
<integer>111</integer>
<key>IDESourceControlProjectWCCIdentifier</key>
<string>957C2411939784F97902D82EAC722A5A882F3E35</string>
<key>IDESourceControlProjectWCConfigurations</key>
<array>
<dict>
<key>IDESourceControlRepositoryExtensionIdentifierKey</key>
<string>public.vcs.git</string>
<key>IDESourceControlWCCIdentifierKey</key>
<string>957C2411939784F97902D82EAC722A5A882F3E35</string>
<key>IDESourceControlWCCName</key>
<string>SwiftJson</string>
</dict>
</array>
</dict>
</plist>
15 changes: 8 additions & 7 deletions SwiftJson/Base.lproj/MainMenu.xib
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="6154.21" systemVersion="14A261i" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES">
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="7531" systemVersion="14D131" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="6154.21"/>
<deployment identifier="macosx"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="7531"/>
</dependencies>
<objects>
<customObject id="-2" userLabel="File's Owner" customClass="NSApplication">
Expand All @@ -10,7 +11,7 @@
</connections>
</customObject>
<customObject id="-1" userLabel="First Responder" customClass="FirstResponder"/>
<customObject id="-3" userLabel="Application"/>
<customObject id="-3" userLabel="Application" customClass="NSObject"/>
<customObject id="Voe-Tx-rLC" customClass="AppDelegate" customModule="SwiftJson" customModuleProvider="target">
<connections>
<outlet property="window" destination="QvC-M9-y7g" id="gIp-Ho-8D9"/>
Expand Down Expand Up @@ -353,7 +354,7 @@
<windowStyleMask key="styleMask" titled="YES" closable="YES" miniaturizable="YES" resizable="YES"/>
<windowPositionMask key="initialPositionMask" leftStrut="YES" rightStrut="YES" topStrut="YES" bottomStrut="YES"/>
<rect key="contentRect" x="335" y="390" width="977" height="737"/>
<rect key="screenRect" x="0.0" y="0.0" width="1280" height="777"/>
<rect key="screenRect" x="0.0" y="0.0" width="1680" height="1027"/>
<view key="contentView" id="EiT-Mj-1SZ">
<rect key="frame" x="0.0" y="0.0" width="977" height="737"/>
<autoresizingMask key="autoresizingMask"/>
Expand Down Expand Up @@ -414,14 +415,14 @@
<autoresizingMask key="autoresizingMask"/>
</scroller>
</scrollView>
<button appearanceType="aqua" misplaced="YES" translatesAutoresizingMaskIntoConstraints="NO" id="thI-38-EBr">
<button misplaced="YES" translatesAutoresizingMaskIntoConstraints="NO" id="thI-38-EBr">
<rect key="frame" x="497" y="27" width="183" height="18"/>
<buttonCell key="cell" type="check" title="Inspect first array element" bezelStyle="regularSquare" imagePosition="left" state="on" inset="2" id="yz3-Ng-D9t">
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
<font key="font" metaFont="system"/>
</buttonCell>
</button>
<button appearanceType="aqua" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="tlI-bW-Bk8">
<button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="tlI-bW-Bk8">
<rect key="frame" x="866" y="19" width="97" height="32"/>
<buttonCell key="cell" type="push" title="Generate" bezelStyle="rounded" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="hQW-mX-fC2">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
Expand All @@ -431,7 +432,7 @@
<action selector="onSelectGenerate:" target="Gqg-Q5-6b0" id="CkA-FH-ZXg"/>
</connections>
</button>
<tokenField appearanceType="aqua" verticalHuggingPriority="750" misplaced="YES" translatesAutoresizingMaskIntoConstraints="NO" id="Nua-Se-Anx">
<tokenField verticalHuggingPriority="750" misplaced="YES" translatesAutoresizingMaskIntoConstraints="NO" id="Nua-Se-Anx">
<rect key="frame" x="20" y="25" width="459" height="22"/>
<tokenFieldCell key="cell" selectable="YES" editable="YES" borderStyle="bezel" alignment="left" drawsBackground="YES" allowsEditingTextAttributes="YES" id="7L3-u7-Wig">
<font key="font" metaFont="system"/>
Expand Down
4 changes: 2 additions & 2 deletions SwiftJson/IndentableOutput.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ class IndentableOutput {
for instruction in right.rawOutput {
switch instruction.type {
case OutputInstructionType.Indent:
left.indentation += instruction.data as Int
left.indentation += instruction.data as! Int
case OutputInstructionType.PrintLine:
left.addLineToOutput(instruction.data as String)
left.addLineToOutput(instruction.data as! String)
}
}

Expand Down
130 changes: 69 additions & 61 deletions SwiftJson/ModelGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,55 +22,57 @@ class ModelGenerator {
}
}

init(json:JSONValue, className:String, inspectArrays:Bool) {
init(json:JSON, className:String, inspectArrays:Bool) {

// set up the init function
var initOutput:IndentableOutput = IndentableOutput()
(initOutput += "init(json:JSONValue) {").indent()
(initOutput += "init(json:JSON) {").indent()

// model set up
(modelOutput += "class \(className) {").indent()

// generate everything
switch(json) {
case .JArray(let array):
initOutput += "// initial element was array..."
case .JObject(let object):
for (key, value) in object {

var type = ""

switch value {
case .JString(let value):
type = "String"
buildSetStatement(initOutput, key:key, type:type)
case .JNumber(let value):
type = "NSNumber"
buildSetStatement(initOutput, key:key, type:type)
case .JBool(let value):
type = "Bool"
buildSetStatement(initOutput, key:key, type:type)
case .JArray(let array):
if(inspectArrays && array.count >= 1) {
type = handleArray(array, key: key, className: className, inspectArrays: inspectArrays, io: initOutput)
} else {
initOutput += "\(key) = json[\"\(key)\"]"
}
case .JObject(let object):
var cn = self.buildClassName(className, suffix: key as String)
childModels.append(ModelGenerator(json: value, className: cn, inspectArrays:inspectArrays))
type = cn
initOutput += "\(key) = \(type)(json:json[\"\(key)\"])"
default:
type = "AnyObject"

if let array = json.array {
initOutput += "// initial element was array..."
}
else if let object = json.dictionary {
for (key, value) in object {

var type = ""

var js : JSON = value as JSON

if let val = js.string {
type = "String"
buildSetStatement(initOutput, key: key, type: type)
} else if let val = js.number {
type = "NSNumber"
buildSetStatement(initOutput, key: key, type: type)
} else if let val = js.bool {
type = "Bool"
buildSetStatement(initOutput, key: key, type: type)
} else if let array = js.array {
if(inspectArrays && array.count >= 1) {
type = handleArray(array, key: key, className: className, inspectArrays: inspectArrays, io: initOutput)
} else {
initOutput += "\(key) = json[\"\(key)\"]"
}

modelOutput += "var \(key):\(type)"
} else if let object = (value as? JSON)?.object {
var cn = self.buildClassName(className, suffix: key as String)
childModels.append(ModelGenerator(json: value, className: cn, inspectArrays:inspectArrays))
type = cn
initOutput += "\(key) = \(type)(json:json[\"\(key)\"])"

} else {
type = "AnyObject"
}
default:
initOutput += "// unexpected type encountered"

modelOutput += "var \(key):\(type)"
}
}


// merge the init function and close everything up
modelOutput += initOutput

Expand All @@ -84,58 +86,64 @@ class ModelGenerator {

}

func handleArray(array:Array<JSONValue>, key:String, className:String, inspectArrays:Bool, io:IndentableOutput) -> String {
func handleArray(array:Array<JSON>, key:String, className:String, inspectArrays:Bool, io:IndentableOutput) -> String {

var instantiation = "v"
var type = "[AnyObject]"

var js = (array[0] as JSON);

if let val = js.string {
type = "String"

switch array[0] {
case .JString(let value):
type = "[String]"

case .JNumber(let value):
type = "[NSNumber]"

case .JBool(let value):
type = "[Bool]"
} else if let val = js.number {
type = "NSNumber"

case .JArray(let arr):
type = "[JSONValue]"
case .JObject(let object):
var cn = buildClassName(className, suffix: key as String)
childModels.append(ModelGenerator(json: array[0], className: cn, inspectArrays:inspectArrays))
type = "[" + cn + "]"
instantiation = "\(cn)(json:v)"
default:
type = "AnyObject"
} else if let val = js.bool {
type = "Bool"
} else if let array = js.array {
type = "[JSON]"
} else if let object = (array[0] as? JSON)?.object {
var cn = buildClassName(className, suffix: key as String)
childModels.append(ModelGenerator(json: array[0], className: cn, inspectArrays:inspectArrays))
type = "[" + cn + "]"
instantiation = "\(cn)(json:v)"
} else {
type = "AnyObject"
}

io += "\(key) = []"
(io += "if let xs = json[\"\(key)\"].array {").indent()
(io += "for v in xs {").indent()
(io += "\(key) += \(instantiation)").dedent() + "}"
(io += "\(key).append(\(instantiation))").dedent() + "}"
io.dedent() += "}"

return type
}

func buildSetStatement(io:IndentableOutput, key:String, type:String) {

let optionTypeMap = [
let optionTypeMap : [String: String] = [
"Bool": "bool",
"NSNumber": "number",
"String": "string"
]

let optionDefaultValueMap = [
let optionDefaultValueMap : [String : String] = [
"Bool": "false",
"NSNumber": "0",
"String": "\"\""
]

(io += "if let value = json[\"\(key)\"].\(optionTypeMap[type]) {").indent()
let typeMap : String = (optionTypeMap[type] as! String?)!;
let defaultValueMap : String = (optionDefaultValueMap[type] as! String?)!;


(io += "if let value = json[\"\(key)\"].\(typeMap) {").indent()

(((io += "\(key) = value").dedent()) += "} else {").indent()
(io += "\(key) = \(optionDefaultValueMap[type])").dedent() += "}"

(io += "\(key) = \(defaultValueMap)").dedent() += "}"

}

Expand Down
Loading

0 comments on commit 9b74e3f

Please sign in to comment.