passing a struct type as a parameter throwing initialization error - swift

I have the following code which has 3 objects which are then part of a 4th object. I'm getting errors trying to create the init method for the aggregate object (GTFS) because I'm passing (or trying to pass) the type of the 3 component objects (Stop, Route, Trip). I'm not sure why those have to be initialized before just their types being used.
protocol GTFSObject {
static var fileName: String { get }
init(csvRow: [String: String])
}
struct Stop: GTFSObject {
static let fileName = "stops.txt"
let stopID: String
let stopCode: String
let stopName: String
let stopDesc: String
let stopLat: Double
let stopLon: Double
let locationType: Int
let parentStation: String
init(csvRow: [String: String]) {
self.stopID = csvRow["stop_id"]!
self.stopCode = csvRow["stop_code"]!
self.stopName = csvRow["stop_name"]!
self.stopDesc = csvRow["stop_desc"]!
self.stopLat = Double(csvRow["stop_lat"]!)!
self.stopLon = Double(csvRow["stop_lon"]!)!
self.locationType = Int(csvRow["location_type"]!)!
self.parentStation = csvRow["parent_station"]!
}
}
struct Trip: GTFSObject {
static let fileName = "trips.txt"
let routeID: String
let serviceID: String
let tripID: String
init(csvRow: [String: String]) {
tripID = csvRow["trip_id"] ?? ""
routeID = csvRow["route_id"] ?? ""
serviceID = csvRow["service_id"] ?? ""
}
}
struct Route: GTFSObject {
static let fileName = "trips.txt"
let routeID: String
let agencyID: String
let routeShortName: String
let routeLongName: String
let routeDesc: String
let routeType: Int
let routeURL: String
let routeColor: String
let routeTextColor: String
init(csvRow: [String: String]) {
routeID = csvRow["route_id"] ?? ""
agencyID = csvRow["agency_id"] ?? ""
routeShortName = csvRow["route_short_name"] ?? ""
routeLongName = csvRow["route_long_name"] ?? ""
routeDesc = csvRow["route_desc"] ?? ""
routeType = Int(csvRow["route_type"] ?? "0") ?? 0
routeURL = csvRow["route_url"] ?? ""
routeColor = csvRow["route_color"] ?? ""
routeTextColor = csvRow["route_text_color"] ?? ""
}
}
class GTFS {
let routes: [Route]
let stops: [Stop]
let trips: [Trip]
init(gtfsFolderUrl: URL) {
self.stops = init_struct_from_url(gtfsFolderUrl: gtfsFolderUrl, type: Stop.self)
self.trips = init_struct_from_url(gtfsFolderUrl: gtfsFolderUrl, type: Trip.self)
self.routes = init_struct_from_url(gtfsFolderUrl: gtfsFolderUrl, type: Route.self)
}
private func init_struct_from_url<GTFSType>(gtfsFolderUrl: URL, type: GTFSType.Type) -> [GTFSType] where GTFSType : GTFSObject{
var returnList: [GTFSType] = []
let rows = try! NamedCSV(url: GTFS_FOLDER_URL.appendingPathComponent(type.fileName), delimiter: CSVDelimiter.comma, loadColumns: false).rows
for row in rows {
returnList.append(type.init(csvRow: row))
}
return returnList
}
}
The error I get is
'self' used in method call 'init_struct_from_url' before all stored properties are initialized
I don't know why I have to initialize the properties of the struct just to pass the type of the struct to this other function. What am I missing?

It's not about the three types that you are passing to init_struct_from_url. It's about the call init_struct_from_url itself.
You are actually calling:
self.init_struct_from_url(...)
^^^^^
The error is saying that that use of self is not allowed, because self is not initialised. If init_struct_from_url uses a property in self, it could see an uninitialised value.
Since init_struct_from_url doesn't actually use self at all and is just a helper function, you can make it an inner function of init:
init(gtfsFolderUrl: URL) {
func structFromUrl<GTFSType>(gtfsFolderUrl: URL, type: GTFSType.Type) -> [GTFSType] where GTFSType : GTFSObject{
var returnList: [GTFSType] = []
let rows = try! NamedCSV(url: GTFS_FOLDER_URL.appendingPathComponent(type.fileName), delimiter: CSVDelimiter.comma, loadColumns: false).rows
for row in rows {
returnList.append(type.init(csvRow: row))
}
return returnList
}
self.stops = structFromUrl(gtfsFolderUrl: gtfsFolderUrl, type: Stop.self)
self.trips = structFromUrl(gtfsFolderUrl: gtfsFolderUrl, type: Trip.self)
self.routes = structFromUrl(gtfsFolderUrl: gtfsFolderUrl, type: Route.self)
}

Related

How to convert Array of Struct to List Realm?

i want to convert Array from struct to List Realm .
static func mapGenreResponsetoGenreEntity( input genre: [GenreModel]) -> List {
var list = List<GenreEntity>()
return genre.map { result in
let newGenre = GenreEntity()
newGenre.gamesCount = result.gamesCount ?? 0
newGenre.id = result.id ?? 0
newGenre.imageBackground = result.imageBackground ?? "Unknown"
newGenre.name = result.name ?? "Unknown"
newGenre.slug = result.slug ?? "Unknown"
list.append(newGenre)
return list
}
}
and the genre is
struct GenreModel: Codable {
let gamesCount : Int?
let id : Int?
let imageBackground : String?
let name : String?
let slug : String?
}
How can i convert from array genre (Struct) to List realm which is GenreEntity ?
This should just be a matter of adding the new GenreEntity objects to an array and then return the entire array once done.
This should do it
func convertToList(genreArray: [GenreClass]) -> List<GenreEntityRealmModel> {
let genreEntityList = List<GenreEntityRealmModel>()
genreArray.forEach { model in
let genreEntity = GenreEntity()
genreEntity.gamesCount = model.gamesCount
genreEntityList.append(genreEntity)
}
return genreEntityList
}

How do I remove "Optional()" from object in an array

So im using CloudKit and fetching all the data into an array as [StartDay], my StartDay class looks like this:
import UIKit
import CloudKit
class StartDay {
var recordID: CKRecord.ID!
var wakeUp: String!
var sleptWell: String!
var dNN: String!
var created: String! {
get {
return created
}
}
}`
My function loads get an arraylist, which contains information received from the database. In my database it stands like this: "22.01.09:
func checkIfButtonShouldBeEnabled(startDayList: [StartDay]){
let startDayDates = startDayList.map{$0.created}
for i in 0..<startDayDates.count {
print(startDayDates)
}
}`
OUTPUT:
Optional("22.01.2019")
Optional("22.01.2019")
I want to remove "Optional()", so it only says "22.01.2019", how can I do so?
UPDATE: FETCH FUNC
func loadStartDay() -> [StartDay]{
let predicate = NSPredicate(value: true)
let query = CKQuery(recordType: "StartDay", predicate: predicate)
let operation = CKQueryOperation(query: query)
var startDays: [StartDay] = []
operation.desiredKeys = ["wakeUp", "wellSlept", "dNN", "recordID", "createdDato"]
operation.recordFetchedBlock = { (record:CKRecord) in
let newStartDay = StartDay()
newStartDay.wakeUp = record.object(forKey: "wakeUP") as? String
newStartDay.sleptWell = record.object(forKey: "sleptWell") as? String
newStartDay.dNN = record.object(forKey: "dNN") as? String
newStartDay.recordID = record.object(forKey: "recordID") as? CKRecord.ID
newStartDay.created = record.object(forKey: "createdDato") as? String
print(newStartDay.created)
startDays.append(newStartDay)
}
You can use print(startDayDates!) or print(startDayDates ?? "default value").
But I recommend usage of startDayList.compactMap() instead of startDayList.map()to ensure your array doesn't contain nil values.
You can also do like this:
startDayList
.compactMap { $0.created }
.forEach { print($0) }
As you designed the database model you exactly know which record attributes always exist. Declaring class properties as implicit unwrapped optional as an alibi not to write an initializer is very bad practice.
Assuming every attribute in a record does have a value declare the properties as non-optional and write an initializer.
At least created and recordID are supposed to have always a value!
import UIKit
import CloudKit
class StartDay {
var recordID: CKRecord.ID
var wakeUp: String
var sleptWell: String
var dNN: String
var created: String
init(record : CKRecord) {
// recordID can be retrieved directly
self.recordID = record.recordID
self.wakeUp = record.object(forKey: "wakeUP") as! String
self.sleptWell = record.object(forKey: "sleptWell") as! String
self.dNN = record.object(forKey: "dNN") as! String
self.created = record.object(forKey: "createdDato") as! String
}
}
and create instances with
operation.recordFetchedBlock = { record in
startDays.append(StartDay(record: record))
}
Now the Optional has gone.
print(startDayList.map{ $0.created })

how to pass the API parameter and parameter is in array

How to pass array parameter
Parameter
[
{
"id": 0,
"followerId": 1030,
"followingId": 1033,
"followerName": "string",
"followingName": "string",
"createdDate": "string",
"message": "string"
}
] //how to solve this array
API Function
class func postFollowers(params:[String: Any],success:#escaping([FollowingDataProvider]) -> Void, failure:#escaping (String) -> Void){
var request = RequestObject()
request = Services.servicePostForFollower(param: params)
APIManager.Singleton.sharedInstance.callWebServiceWithRequest(rqst: request, withResponse: { (response) in
if (response?.isValid)!
{
//success()
print(response?.object as! JSON)
success(self.followingJSONarser(responseObject: response?.object as! JSON));
//followingJSONarser(responseObject: response?.object as! JSON)
}
else
{
failure((response?.error?.description)!)
}
}, withError: {
(error) in
failure((error?.description)!)
})
}
Parsing
static func followingJSONarser(responseObject:JSON) -> [FollowingDataProvider]{
var dataProvider = [FollowingDataProvider]()
let jsonDataa = responseObject["data"]
print(jsonDataa)
let newJSON = jsonDataa["data"].arrayValue
print(newJSON)
for item in newJSON{
print(item)
dataProvider.append(FollowingDataProvider(id: item["userId"].intValue, followerId: item["userId"].intValue, followingId: item["followingId"].intValue, followerName: item["userName"].stringValue, followingName: item["followingName"].stringValue, createdDate: item["createdDate"].stringValue, message: item["message"].stringValue))
}
return dataProvider
}`
You can try to combine SwiftyJson with Codable
struct Root: Codable {
let id, followerID, followingID: Int
let followerName, followingName, createdDate, message: String
enum CodingKeys: String, CodingKey {
case id
case followerID = "followerId"
case followingID = "followingId"
case followerName, followingName, createdDate, message
}
}
if let con = response?.object as? JSON {
do {
let itemData = try con.rawData()
let res = try JSONDecoder().decode([Root].self, from: itemData)
print(res)
catch {
print(error)
}
}
Also avoid force-unwraps with json
response?.object as! JSON
Following your code you're trying to parse data from API, you can use SwiftyJSON with Alamofire to create an HTTP request (post,get,put,delete,etc)
You should use arrayObject instead of arrayValue
Your code missing the right definition to data parsing
static func followingJSONarser(responseObject:JSON) -> [FollowingDataProvider]{
var dataProvider = [FollowingDataProvider]()
var itemClass = [ItemClass]()
let jsonDataa = responseObject["data"] as! Dictionary
let newJSON = jsonDataa["data"].arrayObject as! Array
Now create a dataModel class to cast data to it
class ItemsClass:NSObject{
var id:Int = 0
var followerId:Int = 0
var followingId:Int = 0
var followerName:String = ""
var followingName:String = ""
var createdDate:String = ""
var message:String = ""
init(data:JSON) {
self.id = data["userId"].intValue
self.followerId = data["userId"].intValue
self.followingId = data["followingId"].intValue
self.followerName = data["userName"].stringValue
self.followingName = data["followingName"].stringValue
self.createdDate = data["createdDate"].stringValue
self.message = data["message"].stringValue
}
}
for item in newJSON{
dataProvider.append(itemClass)
}
return dataProvider
}`

Array of structs: UserDefaults, how to use?

I've already check all of those topics:
How to save an array of custom struct to NSUserDefault with swift?
How to save struct to NSUserDefaults in Swift 2.0
STRUCT Array To UserDefaults
I have a struct containing some Strings and an other struct: MySection.
struct MySection {
var name: String = ""
var values: [MyRow] = []
}
And there is MyRow which is store in MySection.values
struct MyRow {
var value: String = ""
var quantity: String = ""
var quantityType: String = ""
var done: String = ""
}
Two arrays for use it
var arraySection: [MySection] = []
var arrayRow: [MyRow] = []
And in my application, I add dynamically some values in those arrays.
There is the delegate method for get datas from my second ViewController
func returnInfos(newItem: [MyRow], sectionPick: String) {
arrayRow.append(MyRow())
arrayRow[arrayRow.count - 1] = newItem[0]
manageSection(item: sectionPick)
listTableView.reloadData()
}
And there is the manageSection function.
func manageSection(item: String) {
var i = 0
for _ in arraySection {
if arraySection[i].name == item {
arraySection.insert(MySection(), at: i + 1)
arraySection[i + 1].values = [arrayRow[arrayRow.count - 1]]
return
}
i += 1
}
arraySection.append(MySection())
arraySection[arraySection.count - 1].name = item
arraySection[arraySection.count - 1].values = [arrayRow[arrayRow.count - 1]]
}
My need is to store datas of the two arrays in UserDefaults (or CoreData maybe??) and use these datas when the user going back to the application.
I don't know how to do it, I've already try methods from the 3 topics but I'm not even doing a good job.
How can I do it?
Thanks guys!
Since both types contain only property list compliant types a suitable solution is to add code to convert each type to a property list compliant object and vice versa.
struct MySection {
var name: String
var values = [MyRow]()
init(name : String, values : [MyRow] = []) {
self.name = name
self.values = values
}
init(propertyList: [String: Any]) {
self.name = propertyList["name"] as! String
self.values = (propertyList["values"] as! [[String:String]]).map{ MyRow(propertyList: $0) }
}
var propertyListRepresentation : [String: Any] {
return ["name" : name, "values" : values.map { $0.propertyListRepresentation }]
}
}
struct MyRow {
var value: String
var quantity: String
var quantityType: String
var done: String
init(value : String, quantity: String, quantityType: String, done: String) {
self.value = value
self.quantity = quantity
self.quantityType = quantityType
self.done = done
}
init(propertyList: [String:String]) {
self.value = propertyList["value"]!
self.quantity = propertyList["quantity"]!
self.quantityType = propertyList["quantityType"]!
self.done = propertyList["done"]!
}
var propertyListRepresentation : [String: Any] {
return ["value" : value, "quantity" : quantity, "quantityType" : quantityType, "done" : done ]
}
}
After creating a few objects
let row1 = MyRow(value: "Foo", quantity: "10", quantityType: "Foo", done: "Yes")
let row2 = MyRow(value: "Bar", quantity: "10", quantityType: "Bar", done: "No")
let section = MySection(name: "Baz", values: [row1, row2])
call propertyListRepresentation to get a dictionary ([String:Any]) which can be saved to User Defaults.
let propertyList = section.propertyListRepresentation
Recreation of the section is quite easy, too
let newSection = MySection(propertyList: propertyList)
Edit
Use the propertyList initializer only if you get data from UserDefaults in all other cases use the other initializer.
For example replace
#IBAction func addButtonPressed(_ sender: Any) {
newProducts.append(MyRow(propertyList: ["":""]))
newProducts[newProducts.count - 1].value = nameTextField.text!
newProducts[newProducts.count - 1].quantity = quantityTextField.text!
newProducts[newProducts.count - 1].quantityType = type
newProducts[newProducts.count - 1].done = "No"
delegate?.returnInfos(newItem: newProducts, sectionPick: typePick)
navigationController?.popViewController(animated: true)
}
with
#IBAction func addButtonPressed(_ sender: Any) {
let row = MyRow(value: nameTextField.text!,
quantity: quantityTextField.text!,
quantityType: type,
done: "No")
newProducts.append(row)
delegate?.returnInfos(newItem: newProducts, sectionPick: typePick)
navigationController?.popViewController(animated: true)
}
and replace
func returnInfos(newItem: [MyRow], sectionPick: String) {
arrayRow.append(MyRow(propertyList: ["":""]))
arrayRow[arrayRow.count - 1] = newItem[0]
manageSection(item: sectionPick)
listTableView.reloadData()
}
with
func returnInfos(newItem: [MyRow], sectionPick: String) {
arrayRow.append(newItem[0])
manageSection(item: sectionPick)
listTableView.reloadData()
}
Basically first create the object, then append it to the array. The other way round is very cumbersome.

Array of custom types a Alamofire parameter: 'Invalid type in JSON write

I am trying to inline an array of custom types into a dir - as parameter to Alamofire. Alamofire fails with 'Invalid type in JSON write in ParameterEncoding.swift: Line142
case .JSON:
do {
let options = NSJSONWritingOptions()
let data = try NSJSONSerialization.dataWithJSONObject(parameters, options: options) // 'Invalid type in JSON write
MyClass
class MyClass{
var name: String = ""
var address: String = ""
}
This gives the error:
class MyParameters{
var MyClasses: [MyClass] = []
var dictionary: [String: AnyObject]{
get {
return [
"myclasses": self.MyClasses,
]
}
}
}
Tried this from:
class MyClass{
var name: String = ""
var address: String = ""
}
class MyParameters{
var MyClasses: [MyClass] = []
var dictionary: [String: AnyObject]{
get {
return [
"myclasses": self.MyClasses.map({$0.name}),
]
}
}
}