Skip to content

Instantly share code, notes, and snippets.

@mataprasad
Last active October 25, 2017 11:49
Show Gist options
  • Save mataprasad/8b1b10ed9a2a462dacdbde5e1a362f19 to your computer and use it in GitHub Desktop.
Save mataprasad/8b1b10ed9a2a462dacdbde5e1a362f19 to your computer and use it in GitHub Desktop.
import Foundation
class Employee: Codable {
var EmployeeID: String?
var LastName: String?
var FirstName: String?
var Title: String?
var TitleOfCourtesy: String?
var BirthDate: String?
var HireDate: String?
var Address: String?
var City: String?
var Region: String?
var PostalCode: String?
var Country: String?
var HomePhone: String?
var Extension: String?
var ReportsTo: String?
var PhotoPath: String?
init() {
self.EmployeeID=""
self.LastName=""
self.FirstName=""
self.Title=""
self.TitleOfCourtesy=""
self.BirthDate=""
self.HireDate=""
self.Address=""
self.City=""
self.Region=""
self.PostalCode=""
self.Country=""
self.HomePhone=""
self.Extension=""
self.ReportsTo=""
self.PhotoPath=""
}
static func getDummyData()->Array<Employee> {
let obj1:Employee=Employee()
obj1.EmployeeID="1";
obj1.LastName="Davolio";
obj1.FirstName="Nancy";
obj1.Title="Sales Representative";
obj1.TitleOfCourtesy="Ms.";
obj1.BirthDate="12/8/1948 12:00:00 AM";
obj1.HireDate="5/1/1992 12:00:00 AM";
obj1.Address="507 - 20th Ave. EApt. 2A";
obj1.City="Seattle";
obj1.Region="WA";
obj1.PostalCode="98122";
obj1.Country="USA";
obj1.HomePhone="(206) 555-9857";
obj1.Extension="5467";
obj1.ReportsTo="2";
obj1.PhotoPath="http://accweb/emmployees/davolio.bmp";
let obj2:Employee=Employee()
obj2.EmployeeID="2";
obj2.LastName="Fuller";
obj2.FirstName="Andrew";
obj2.Title="Vice President, Sales";
obj2.TitleOfCourtesy="Dr.";
obj2.BirthDate="2/19/1952 12:00:00 AM";
obj2.HireDate="8/14/1992 12:00:00 AM";
obj2.Address="908 W. Capital Way";
obj2.City="Tacoma";
obj2.Region="WA";
obj2.PostalCode="98401";
obj2.Country="USA";
obj2.HomePhone="(206) 555-9482";
obj2.Extension="3457";
obj2.ReportsTo="";
obj2.PhotoPath="http://accweb/emmployees/fuller.bmp";
let obj3:Employee=Employee()
obj3.EmployeeID="3";
obj3.LastName="Leverling";
obj3.FirstName="Janet";
obj3.Title="Sales Representative";
obj3.TitleOfCourtesy="Ms.";
obj3.BirthDate="8/30/1963 12:00:00 AM";
obj3.HireDate="4/1/1992 12:00:00 AM";
obj3.Address="722 Moss Bay Blvd.";
obj3.City="Kirkland";
obj3.Region="WA";
obj3.PostalCode="98033";
obj3.Country="USA";
obj3.HomePhone="(206) 555-3412";
obj3.Extension="3355";
obj3.ReportsTo="2";
obj3.PhotoPath="http://accweb/emmployees/leverling.bmp";
let obj4:Employee=Employee()
obj4.EmployeeID="4";
obj4.LastName="Peacock";
obj4.FirstName="Margaret";
obj4.Title="Sales Representative";
obj4.TitleOfCourtesy="Mrs.";
obj4.BirthDate="9/19/1937 12:00:00 AM";
obj4.HireDate="5/3/1993 12:00:00 AM";
obj4.Address="4110 Old Redmond Rd.";
obj4.City="Redmond";
obj4.Region="WA";
obj4.PostalCode="98052";
obj4.Country="USA";
obj4.HomePhone="(206) 555-8122";
obj4.Extension="5176";
obj4.ReportsTo="2";
obj4.PhotoPath="http://accweb/emmployees/peacock.bmp";
let obj5:Employee=Employee()
obj5.EmployeeID="5";
obj5.LastName="Buchanan";
obj5.FirstName="Steven";
obj5.Title="Sales Manager";
obj5.TitleOfCourtesy="Mr.";
obj5.BirthDate="3/4/1955 12:00:00 AM";
obj5.HireDate="10/17/1993 12:00:00 AM";
obj5.Address="14 Garrett Hill";
obj5.City="London";
obj5.Region="";
obj5.PostalCode="SW1 8JR";
obj5.Country="UK";
obj5.HomePhone="(71) 555-4848";
obj5.Extension="3453";
obj5.ReportsTo="2";
obj5.PhotoPath="http://accweb/emmployees/buchanan.bmp";
let obj6:Employee=Employee()
obj6.EmployeeID="6";
obj6.LastName="Suyama";
obj6.FirstName="Michael";
obj6.Title="Sales Representative";
obj6.TitleOfCourtesy="Mr.";
obj6.BirthDate="7/2/1963 12:00:00 AM";
obj6.HireDate="10/17/1993 12:00:00 AM";
obj6.Address="Coventry HouseMiner Rd.";
obj6.City="London";
obj6.Region="";
obj6.PostalCode="EC2 7JR";
obj6.Country="UK";
obj6.HomePhone="(71) 555-7773";
obj6.Extension="428";
obj6.ReportsTo="5";
obj6.PhotoPath="http://accweb/emmployees/davolio.bmp";
let obj7:Employee=Employee()
obj7.EmployeeID="7";
obj7.LastName="King";
obj7.FirstName="Robert";
obj7.Title="Sales Representative";
obj7.TitleOfCourtesy="Mr.";
obj7.BirthDate="5/29/1960 12:00:00 AM";
obj7.HireDate="1/2/1994 12:00:00 AM";
obj7.Address="Edgeham HollowWinchester Way";
obj7.City="London";
obj7.Region="";
obj7.PostalCode="RG1 9SP";
obj7.Country="UK";
obj7.HomePhone="(71) 555-5598";
obj7.Extension="465";
obj7.ReportsTo="5";
obj7.PhotoPath="http://accweb/emmployees/davolio.bmp";
let obj8:Employee=Employee()
obj8.EmployeeID="8";
obj8.LastName="Callahan";
obj8.FirstName="Laura";
obj8.Title="Inside Sales Coordinator";
obj8.TitleOfCourtesy="Ms.";
obj8.BirthDate="1/9/1958 12:00:00 AM";
obj8.HireDate="3/5/1994 12:00:00 AM";
obj8.Address="4726 - 11th Ave. N.E.";
obj8.City="Seattle";
obj8.Region="WA";
obj8.PostalCode="98105";
obj8.Country="USA";
obj8.HomePhone="(206) 555-1189";
obj8.Extension="2344";
obj8.ReportsTo="2";
obj8.PhotoPath="http://accweb/emmployees/davolio.bmp";
let obj9:Employee=Employee()
obj9.EmployeeID="9";
obj9.LastName="Dodsworth";
obj9.FirstName="Anne";
obj9.Title="Sales Representative";
obj9.TitleOfCourtesy="Ms.";
obj9.BirthDate="1/27/1966 12:00:00 AM";
obj9.HireDate="11/15/1994 12:00:00 AM";
obj9.Address="7 Houndstooth Rd.";
obj9.City="London";
obj9.Region="";
obj9.PostalCode="WG2 7LT";
obj9.Country="UK";
obj9.HomePhone="(71) 555-4444";
obj9.Extension="452";
obj9.ReportsTo="5";
obj9.PhotoPath="http://accweb/emmployees/davolio.bmp";
let data=[obj1,obj2,obj3,obj4,obj5,obj6,obj7,obj8,obj9]
//let encoder = JSONEncoder()
//let jsonData = try? encoder.encode(data)
//let jsonString = String(data: jsonData!, encoding: .utf8)
//print(jsonString!)
return data
}
static func getJson(data:Array<Employee>)->String{
let encoder = JSONEncoder()
let jsonData = try? encoder.encode(data)
let jsonString = String(data: jsonData!, encoding: .utf8)
return jsonString!
//print(jsonString!)
}
static func getFromJson(jsonString:String)->Array<Employee>{
let jsonData = jsonString.data(using: .utf8)!
let decoder = JSONDecoder()
let data = try! decoder.decode(Array<Employee>.self, from: jsonData) as Array<Employee>
return data
}
}
//let json=Employee.getJson(data:Employee.getDummyData())
//print(Employee.getJson(data:Employee.getDummyData()))
//let data=Employee.getFromJson(jsonString:json)
//print((data[0] as Employee).EmployeeID!)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment