Skip to content

Commit 0e2528b

Browse files
committed
- Able to Read the CSV file and convert to CSV class(Object Oriented Approach).
- Refactored CSV Export class.
1 parent bc7cf4f commit 0e2528b

File tree

7 files changed

+114
-19
lines changed

7 files changed

+114
-19
lines changed

Examples/SampleSwift/SampleSwift.xcodeproj/project.pbxproj

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -331,13 +331,16 @@
331331
files = (
332332
);
333333
inputPaths = (
334+
"${PODS_PODFILE_DIR_PATH}/Podfile.lock",
335+
"${PODS_ROOT}/Manifest.lock",
334336
);
335337
name = "[CP] Check Pods Manifest.lock";
336338
outputPaths = (
339+
"$(DERIVED_FILE_DIR)/Pods-SampleSwiftTests-checkManifestLockResult.txt",
337340
);
338341
runOnlyForDeploymentPostprocessing = 0;
339342
shellPath = /bin/sh;
340-
shellScript = "diff \"${PODS_PODFILE_DIR_PATH}/Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n";
343+
shellScript = "diff \"${PODS_PODFILE_DIR_PATH}/Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n# This output is used by Xcode 'outputs' to avoid re-running this script phase.\necho \"SUCCESS\" > \"${SCRIPT_OUTPUT_FILE_0}\"\n";
341344
showEnvVarsInLog = 0;
342345
};
343346
33A400582F818FDFD5E2610C /* [CP] Embed Pods Frameworks */ = {
@@ -346,9 +349,14 @@
346349
files = (
347350
);
348351
inputPaths = (
352+
"${SRCROOT}/Pods/Target Support Files/Pods-SampleSwift/Pods-SampleSwift-frameworks.sh",
353+
"${BUILT_PRODUCTS_DIR}/SwiftCSVExport/SwiftCSVExport.framework",
354+
"${BUILT_PRODUCTS_DIR}/SwiftLoggly/SwiftLoggly.framework",
349355
);
350356
name = "[CP] Embed Pods Frameworks";
351357
outputPaths = (
358+
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/SwiftCSVExport.framework",
359+
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/SwiftLoggly.framework",
352360
);
353361
runOnlyForDeploymentPostprocessing = 0;
354362
shellPath = /bin/sh;
@@ -361,13 +369,16 @@
361369
files = (
362370
);
363371
inputPaths = (
372+
"${PODS_PODFILE_DIR_PATH}/Podfile.lock",
373+
"${PODS_ROOT}/Manifest.lock",
364374
);
365375
name = "[CP] Check Pods Manifest.lock";
366376
outputPaths = (
377+
"$(DERIVED_FILE_DIR)/Pods-SampleSwiftUITests-checkManifestLockResult.txt",
367378
);
368379
runOnlyForDeploymentPostprocessing = 0;
369380
shellPath = /bin/sh;
370-
shellScript = "diff \"${PODS_PODFILE_DIR_PATH}/Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n";
381+
shellScript = "diff \"${PODS_PODFILE_DIR_PATH}/Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n# This output is used by Xcode 'outputs' to avoid re-running this script phase.\necho \"SUCCESS\" > \"${SCRIPT_OUTPUT_FILE_0}\"\n";
371382
showEnvVarsInLog = 0;
372383
};
373384
8523F8CAD25749EA216FAF6D /* [CP] Embed Pods Frameworks */ = {
@@ -436,13 +447,16 @@
436447
files = (
437448
);
438449
inputPaths = (
450+
"${PODS_PODFILE_DIR_PATH}/Podfile.lock",
451+
"${PODS_ROOT}/Manifest.lock",
439452
);
440453
name = "[CP] Check Pods Manifest.lock";
441454
outputPaths = (
455+
"$(DERIVED_FILE_DIR)/Pods-SampleSwift-checkManifestLockResult.txt",
442456
);
443457
runOnlyForDeploymentPostprocessing = 0;
444458
shellPath = /bin/sh;
445-
shellScript = "diff \"${PODS_PODFILE_DIR_PATH}/Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n";
459+
shellScript = "diff \"${PODS_PODFILE_DIR_PATH}/Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n# This output is used by Xcode 'outputs' to avoid re-running this script phase.\necho \"SUCCESS\" > \"${SCRIPT_OUTPUT_FILE_0}\"\n";
446460
showEnvVarsInLog = 0;
447461
};
448462
/* End PBXShellScriptBuildPhase section */

Examples/SampleSwift/SampleSwift/ViewController.swift

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,16 @@ class ViewController: UIViewController {
4141
data.add(user1);
4242
data.add(user2);
4343

44-
let filePath:String = SwiftCSVExport.exportCSV("userlist",fields: ["name", "email", "address"],values: data);
44+
// Create a object for write CSV
45+
let writeCSVObj = CSV()
46+
writeCSVObj.rows = data
47+
writeCSVObj.fields = ["name", "email", "address"]
48+
writeCSVObj.name = "userlist"
49+
50+
// Write File using CSV class object
51+
let filePath:String = SwiftCSVExport.exportCSV(writeCSVObj);
4552
print(filePath)
53+
4654

4755
let request = NSURLRequest(url: URL(fileURLWithPath: filePath) )
4856
webview.loadRequest(request as URLRequest)
@@ -53,6 +61,11 @@ class ViewController: UIViewController {
5361
loggly(LogType.Info, dictionary: fileDetails)
5462
}
5563

64+
// Read File in Object Oriented Way
65+
let readCSVObj = readCSVObject(filePath);
66+
67+
// Use 'SwiftLoggly' pod framework to print the Dictionary
68+
loggly(LogType.Info, text: readCSVObj.name)
5669

5770
}
5871

README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Simple way to export csv file with rich feature framework and written in Swift.
1111
- Able to set CSV headers using fields.
1212
- Able to convert JSON string into CSV.
1313
- Able to Read the CSV file and convert to NSDictionary.
14+
- Able to Read the CSV file and convert to CSV class(Object Oriented Approach).
1415
- Support CocoaPods, mac OS and Vapor framework(Swift Package Manager).
1516
- Able to encoding CSV based on String.Encoding Type(utf8, ascii, unicode, utf16, etc) Refer: String.Encoding.
1617
- Able to view the exported CSV documents in iOS Files app by enabling the configuration in your project.
@@ -240,6 +241,53 @@ if fileDetails.allKeys.count > 0 {
240241
}
241242

242243

244+
```
245+
246+
### Example 7 - Swift - Object Oriented Approach
247+
248+
```swift
249+
250+
// Generate CSV file
251+
let user1:NSMutableDictionary = NSMutableDictionary()
252+
user1.setObject("vignesh", forKey: "name" as NSCopying);
253+
user1.setObject("[email protected]", forKey: "email" as NSCopying);
254+
user1.setObject("Hi Vignesh, \nhow are you? \t Shall we meet tomorrow? \r Thanks ", forKey: "address" as NSCopying);
255+
256+
let user2:NSMutableDictionary = NSMutableDictionary()
257+
user2.setObject("vinoth", forKey: "name" as NSCopying);
258+
user2.setObject("[email protected]", forKey: "email" as NSCopying);
259+
user2.setObject("Hi Vinoth, \nHow are you? \t Shall we meet tomorrow? \r Thanks ", forKey: "address" as NSCopying);
260+
261+
262+
let data:NSMutableArray = NSMutableArray()
263+
data.add(user1);
264+
data.add(user2);
265+
266+
// Create a object for write CSV
267+
let writeCSVObj = CSV()
268+
writeCSVObj.rows = data
269+
writeCSVObj.fields = ["name", "email", "address"]
270+
writeCSVObj.name = "userlist"
271+
272+
// Write File using CSV class object
273+
let filePath:String = SwiftCSVExport.exportCSV(writeCSVObj);
274+
print(filePath)
275+
276+
// Read File in Object Oriented Way
277+
let readCSVObj = readCSVObject(filePath);
278+
279+
// Use 'SwiftLoggly' pod framework to print the Dictionary
280+
loggly(LogType.Info, text: readCSVObj.name)
281+
282+
283+
```
284+
285+
### Write Output:
286+
287+
```swift
288+
289+
Output: userlist
290+
243291
```
244292

245293

SwiftCSVExport.podspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ Pod::Spec.new do |s|
1616
#
1717

1818
s.name = "SwiftCSVExport"
19-
s.version = "1.0.5"
20-
s.summary = "Simple way to export csv file with rich feature framework and written in Swift 3."
19+
s.version = "1.0.6"
20+
s.summary = "Simple way to export csv file with rich feature framework and written in Swift 3 & 4."
2121

2222
# This description is used to generate tags and improve search results.
2323
# * Think: What does it do? Why did you write it? What is the focus?

SwiftCSVExport.xcodeproj/project.pbxproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@
514514
PRODUCT_NAME = "$(TARGET_NAME)";
515515
SKIP_INSTALL = YES;
516516
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
517-
SWIFT_SWIFT3_OBJC_INFERENCE = On;
517+
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
518518
SWIFT_VERSION = 4.0;
519519
};
520520
name = Debug;
@@ -538,7 +538,7 @@
538538
PRODUCT_BUNDLE_IDENTIFIER = vigneshuvi.SwiftCSVExport;
539539
PRODUCT_NAME = "$(TARGET_NAME)";
540540
SKIP_INSTALL = YES;
541-
SWIFT_SWIFT3_OBJC_INFERENCE = On;
541+
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
542542
SWIFT_VERSION = 4.0;
543543
};
544544
name = Release;

SwiftCSVExport/Sources/CSVExport.swift

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ public enum DividerType: String {
1414
case semicolon = ";"
1515
}
1616

17-
@objc open class CSV:NSObject {
18-
var fields:NSArray = []
19-
var rows:NSArray = []
20-
var name:String = ""
21-
var delimiter:String = DividerType.comma.rawValue
17+
@objc public class CSV:NSObject {
18+
open var fields:NSArray = []
19+
open var rows:NSArray = []
20+
open var name:String = ""
21+
open var delimiter:String = DividerType.comma.rawValue
2222
}
2323

2424
//MARK: - Extension for String to find length
@@ -30,6 +30,21 @@ extension String {
3030
return result
3131
}
3232

33+
func split(regex pattern: String) -> [String] {
34+
35+
guard let re = try? NSRegularExpression(pattern: pattern, options: [])
36+
else { return [] }
37+
38+
let nsString = self as NSString // needed for range compatibility
39+
let stop = "<SomeStringThatYouDoNotExpectToOccurInSelf>"
40+
let modifiedString = re.stringByReplacingMatches(
41+
in: self,
42+
options: [],
43+
range: NSRange(location: 0, length: nsString.length),
44+
withTemplate: stop)
45+
return modifiedString.components(separatedBy: stop)
46+
}
47+
3348
var length: Int {
3449
return self.count
3550
}
@@ -227,9 +242,10 @@ extension String {
227242
return rowsDictionary;
228243
}
229244

230-
func splitUsingDelimiter(_ string: String, separatedBy: String) -> NSArray {
245+
func splitUsingDelimiter(_ string: String, separatedBy: String) -> [String] {
231246
if string.length > 0 {
232-
return string.components(separatedBy: separatedBy) as NSArray;
247+
let t1 = string.components(separatedBy: separatedBy) as [String];
248+
return t1.filter{ !$0.isEmpty }
233249
}
234250
return [];
235251
}
@@ -261,8 +277,7 @@ extension String {
261277
if csvText.length > 0 {
262278

263279
// Split based on Newline delimiter
264-
//let csvArray = self.splitUsingDelimiter(csvText, separatedBy: "\n") as NSArray
265-
let csvArray = csvText.lines
280+
let csvArray:[String] = self.splitUsingDelimiter(csvText, separatedBy: "\n")
266281
if csvArray.count >= 2 {
267282
var fieldsArray:NSArray = [];
268283
let rowsArray:NSMutableArray = NSMutableArray()
@@ -273,9 +288,9 @@ extension String {
273288
fieldsArray = self.splitUsingDelimiter(row, separatedBy: div) as NSArray;
274289
} else {
275290
// Get the CSV values
276-
let valuesArray = self.splitUsingDelimiter(row, separatedBy: div) as NSArray;
291+
let valuesArray = self.splitUsingDelimiter(row, separatedBy: "\"\(div)") as NSArray;
277292
if valuesArray.count == fieldsArray.count && valuesArray.count > 0{
278-
let rowJson:NSMutableDictionary = self.generateDict(fieldsArray, valuesArray: valuesArray)
293+
let rowJson:NSMutableDictionary = self.generateDict(fieldsArray, valuesArray: valuesArray as NSArray)
279294
if rowJson.allKeys.count > 0 && valuesArray.count == rowJson.allKeys.count && rowJson.allKeys.count == fieldsArray.count {
280295
rowsArray.add(rowJson)
281296
}

SwiftCSVExportTests/SwiftCSVExportTests.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,15 @@ class SwiftCSVExportTests: XCTestCase {
3434
let path:String = SwiftCSVExport.exportCSV("numberList",fields: fields,values: numberArray);
3535
print(path)
3636

37+
// Read CSV as NSMutableDictionary object
3738
let numberDetails = readCSV(path);
3839
if numberDetails.allKeys.count > 0 {
3940
print(numberDetails)
4041
}
42+
43+
// Read CSV as CSV class object
44+
let csvObj = readCSVObject(path);
45+
loggly(LogType.Info, text: csvObj.name)
4146
}
4247

4348
func testPerformanceExample() {

0 commit comments

Comments
 (0)