khenid

Untitled

Jan 30th, 2015
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.04 KB | None | 0 0
  1. //
  2. // ViewController.swift
  3. // CHCSVParser
  4. //
  5. // Created by Anil on 28/01/15.
  6. // Copyright (c) 2015 Variya Soft Solutions. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10. import Foundation
  11.  
  12. class ViewController: UIViewController, CHCSVParserDelegate {
  13.  
  14. var currentRow = NSMutableArray()
  15. var dict : NSMutableDictionary = NSMutableDictionary()
  16. @IBOutlet weak var txtRo: UITextField!
  17. @IBOutlet weak var txtName: UITextField!
  18. @IBOutlet weak var txtMarks: UITextField!
  19. override func viewDidLoad() {
  20. super.viewDidLoad()
  21.  
  22. }
  23.  
  24. func parserDidBeginDocument(parser: CHCSVParser!) {
  25.  
  26. currentRow = NSMutableArray()
  27. }
  28.  
  29. func parserDidEndDocument(parser: CHCSVParser!) {
  30.  
  31. for var i = 0; i < currentRow.count; ++i {
  32.  
  33. println((currentRow.objectAtIndex(i).valueForKey("0")),
  34. (currentRow.objectAtIndex(i).valueForKey("1")),
  35. (currentRow.objectAtIndex(i).valueForKey("2")))
  36. }
  37. }
  38.  
  39. func parser(parser: CHCSVParser!, didFailWithError error: NSError!) {
  40.  
  41. println("Parse failed with error: \(error.localizedDescription), \(error.userInfo)")
  42. }
  43. func parser(parser: CHCSVParser!, didBeginLine recordNumber: UInt) {
  44.  
  45. dict = NSMutableDictionary()
  46. }
  47. func parser(parser: CHCSVParser!, didReadField field: String!, atIndex fieldIndex: Int) {
  48.  
  49. dict.setObject(field, forKey: String(fieldIndex))
  50. }
  51. func parser(parser: CHCSVParser!, didEndLine recordNumber: UInt) {
  52.  
  53. currentRow.addObject(dict)
  54. }
  55. @IBAction func btnWrite(sender: AnyObject) {
  56.  
  57. if !NSFileManager.defaultManager().fileExistsAtPath(self.dataFilePath()){
  58.  
  59. NSFileManager.defaultManager().createFileAtPath(self.dataFilePath(), contents: nil, attributes: nil)
  60. println("File Created")
  61.  
  62. }
  63.  
  64. var csvWriter : CHCSVWriter = CHCSVWriter(forWritingToCSVFile: NSHomeDirectory().stringByAppendingPathComponent("fav.csv"))
  65. csvWriter.writeField("Roll Number")
  66. csvWriter.writeField("Name")
  67. csvWriter.writeField("Marks")
  68. csvWriter.finishLine()
  69.  
  70. for var i = 0; i < currentRow.count; ++i{
  71.  
  72. csvWriter.writeField(currentRow.objectAtIndex(i).valueForKey("0"))
  73. csvWriter.writeField(currentRow.objectAtIndex(i).valueForKey("1"))
  74. csvWriter.writeField(currentRow.objectAtIndex(i).valueForKey("2"))
  75.  
  76. }
  77. // var handle : NSFileHandle = NSFileHandle()
  78. // handle = NSFileHandle(forWritingAtPath: self.dataFilePath())!
  79. // handle.truncateFileAtOffset(handle.seekToEndOfFile())
  80. // handle.writeData(currentRow.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!)
  81.  
  82. csvWriter.writeField(txtRo.text)
  83. csvWriter.writeField(txtName.text)
  84. csvWriter.writeField(txtMarks.text)
  85.  
  86. csvWriter.closeStream()
  87.  
  88. var alert : UIAlertController = UIAlertController(title: "Success", message: "Your Data has been suceesFully saved", preferredStyle: UIAlertControllerStyle.Alert)
  89. alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.Default, handler: nil))
  90. self.presentViewController(alert, animated: true, completion: nil)
  91.  
  92. self.txtRo.text = ""
  93. self.txtName.text = ""
  94. self.txtMarks.text = ""
  95. }
  96.  
  97.  
  98.  
  99. @IBAction func btnDismissKeyboardClicked(sender: AnyObject) {
  100.  
  101. txtRo.resignFirstResponder()
  102. txtName.resignFirstResponder()
  103. txtMarks.resignFirstResponder()
  104.  
  105. }
  106.  
  107. func dataFilePath() -> String{
  108.  
  109. var paths : NSArray = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
  110. var documentsDirectory: AnyObject = paths.objectAtIndex(0)
  111. return documentsDirectory.stringByAppendingPathComponent("fav.csv")
  112. }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment