@@ -19,7 +19,10 @@ import Cocoa
19
19
*/
20
20
struct VariableType {
21
21
static let kStringType : String = " String "
22
- static let kNumberType = " NSNumber "
22
+ static let kIntNumberType = " Int "
23
+ static let kFloatNumberType = " Float "
24
+ static let kDoubleNumberType = " Double "
25
+ static let kCGFloatNumberType = " CGFloat "
23
26
static let kBoolType = " Bool "
24
27
static let kArrayType = " [] "
25
28
static let kObjectType = " {OBJ} "
@@ -301,6 +304,9 @@ public class ModelGenerator {
301
304
- returns: A generated string for declaring the variable.
302
305
*/
303
306
internal func variableDeclarationBuilder( variableName: String , type: String ) -> String {
307
+
308
+ //We can be smarter about numbers. Rather than toss all numbers to NSNumber, we'll detect the internal type and use a simplified version of that.
309
+
304
310
return " \t public var \( variableName) : \( type) ? \n "
305
311
}
306
312
@@ -322,8 +328,50 @@ public class ModelGenerator {
322
328
type = VariableType . kStringType
323
329
} else if let _ = js. bool {
324
330
type = VariableType . kBoolType
325
- } else if let _ = js. number {
326
- type = VariableType . kNumberType
331
+ } else if let validNumber = js. number {
332
+
333
+ //Smarter number type detection. Rather than use generic NSNumber, we can use a specific type. These are grouped into the common Swift number types.
334
+ let numberRef = CFNumberGetType ( validNumber as CFNumberRef )
335
+
336
+ switch numberRef {
337
+
338
+ case . SInt8Type:
339
+ fallthrough
340
+ case . SInt16Type:
341
+ fallthrough
342
+ case . SInt32Type:
343
+ fallthrough
344
+ case . SInt64Type:
345
+ fallthrough
346
+ case . CharType:
347
+ fallthrough
348
+ case . ShortType:
349
+ fallthrough
350
+ case . IntType:
351
+ fallthrough
352
+ case . LongType:
353
+ fallthrough
354
+ case . LongLongType:
355
+ fallthrough
356
+ case . CFIndexType:
357
+ fallthrough
358
+ case . NSIntegerType:
359
+ type = VariableType . kIntNumberType
360
+
361
+ case . Float32Type:
362
+ fallthrough
363
+ case . Float64Type:
364
+ fallthrough
365
+ case . FloatType:
366
+ type = VariableType . kFloatNumberType
367
+
368
+ case . DoubleType:
369
+ type = VariableType . kDoubleNumberType
370
+
371
+ case . CGFloatType:
372
+ type = VariableType . kCGFloatNumberType
373
+ }
374
+
327
375
} else if let _ = js. array {
328
376
type = VariableType . kArrayType
329
377
}
@@ -527,7 +575,10 @@ public class ModelGenerator {
527
575
- returns: swift variable type.
528
576
*/
529
577
internal func typeToSwiftType( var type: String ) -> String {
530
- if type == VariableType . kNumberType {
578
+
579
+ let isNumber = ( type == VariableType . kIntNumberType || type == VariableType . kFloatNumberType || type == VariableType . kCGFloatNumberType || type == VariableType . kDoubleNumberType)
580
+
581
+ if isNumber {
531
582
type = " number "
532
583
} else {
533
584
type. replaceRange ( type. startIndex... type. startIndex, with: String ( type [ type. startIndex] ) . lowercaseString)
0 commit comments