forked from Ahmed-Ali/JSONExport
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSharedUtilityMethods.swift
More file actions
executable file
·196 lines (157 loc) · 5.21 KB
/
SharedUtilityMethods.swift
File metadata and controls
executable file
·196 lines (157 loc) · 5.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
//
// SharedUtilityMethods.swift
// JSONExport
//
// Created by Ahmed Ali on 11/23/14.
// Copyright (c) 2014 Ahmed Ali. All rights reserved.
//
import Foundation
/**
Creats and returns the type name for the passed value
- parameter value: example value to figure out its type
- returns: the type name
*/
func propertyTypeName(_ value : AnyObject, lang: LangModel) -> String
{
var name = ""
if value is NSArray{
name = typeNameForArrayOfElements(value as! NSArray, lang:lang)
}else if value is NSNumber{
name = typeForNumber(value as! NSNumber, lang: lang)
}else if value is NSString{
let booleans : [String] = ["True", "true", "TRUE", "False", "false", "FALSE"]
if booleans.index(of: (value as! String)) != nil{
name = lang.dataTypes.boolType
}else{
name = lang.dataTypes.stringType
}
}else if value is NSNull{
name = lang.genericType
}
return name
}
/**
Tries to figur out the type of the elements of the passed array and returns the type that can be used as the type of any element in the array
- parameter elements: array to try to find out which type is suitable for its elements
- returns: typeName the type name as String
*/
func typeNameForArrayElements(_ elements: NSArray, lang: LangModel) -> String{
var typeName : String!
let genericType = lang.genericType
if elements.count == 0{
typeName = genericType
}
for element in elements{
let currElementTypeName = propertyTypeName(element as AnyObject, lang: lang)
if typeName == nil{
typeName = currElementTypeName
}else{
if typeName != currElementTypeName{
typeName = genericType
break
}
}
}
return typeName
}
/**
Tries to figur out the type of the elements of the passed array and returns the type of the array that can hold these values
- parameter elements: array to try to find out which type is suitable for its elements
- returns: the type name
*/
func typeNameForArrayOfElements(_ elements: NSArray, lang: LangModel) -> String{
var typeName : String!
let genericType = lang.arrayType.replacingOccurrences(of: elementType, with: lang.genericType)
if elements.count == 0{
typeName = genericType
}
for element in elements{
let currElementTypeName = propertyTypeName(element as AnyObject, lang: lang)
let arrayTypeName = lang.arrayType.replacingOccurrences(of: elementType, with: currElementTypeName)
if typeName == nil{
typeName = arrayTypeName
}else{
if typeName != arrayTypeName{
typeName = genericType
break
}
}
}
return typeName
}
/**
Returns one of the possible types for any numeric value (int, float, double, etc...)
- parameter number: the numeric value
- returns: the type name
*/
func typeForNumber(_ number : NSNumber, lang: LangModel) -> String
{
let numberType = CFNumberGetType(number as CFNumber)
var typeName : String!
switch numberType{
case .charType:
if (number.int32Value == 0 || number.int32Value == 1){
//it seems to be boolean
typeName = lang.dataTypes.boolType
}else{
typeName = lang.dataTypes.characterType
}
case .shortType, .intType:
typeName = lang.dataTypes.intType
case .floatType, .float32Type, .float64Type:
typeName = lang.dataTypes.floatType
case .doubleType:
typeName = lang.dataTypes.doubleType
case .longType, .longLongType:
typeName = lang.dataTypes.longType
default:
typeName = lang.dataTypes.intType
}
return typeName
}
/**
Creates and returns a dictionary who is built up by combining all the dictionary elements in the passed array.
- parameter array: array of dictionaries.
- returns: dictionary that combines all the dictionary elements in the array.
*/
func unionDictionaryFromArrayElements(_ array: NSArray) -> NSDictionary
{
let dictionary = NSMutableDictionary()
for item in array{
if let dic = item as? NSDictionary{
//loop all over its keys
for key in dic.allKeys as! [String]{
dictionary[key] = dic[key]
}
}
}
return dictionary
}
/**
Cleans up the passed string from any control characters.
- parameter string: the string to be cleaned up
- returns: a clean version of the passed string
*/
func stringByRemovingControlCharacters(_ string: String) -> String
{
let controlChars = CharacterSet.controlCharacters
var range = string.rangeOfCharacter(from: controlChars)
var cleanString = string;
while range != nil && !range!.isEmpty{
cleanString = cleanString.replacingCharacters(in: range!, with: "")
range = cleanString.rangeOfCharacter(from: controlChars)
}
return cleanString
}
func runOnBackground(_ task: @escaping () -> Void)
{
DispatchQueue.global(qos: DispatchQoS.QoSClass.default).async {
task();
}
}
func runOnUiThread(_ task: @escaping () -> Void)
{
DispatchQueue.main.async(execute: { () -> Void in
task();
})
}