I'm using the floor as a local database and I want to store this object after running the build_runner command it gives me this error
The column type is not supported for List?.
I know that I should use foreign keys but I don't know How can I use them in this situation.
any ideas, please?
#entity
#JsonSerializable()
class SalesOrder extends BaseResponse<SalesOrder> {
#PrimaryKey(autoGenerate: true)
int? salesId;
String? salesCode;
String? clientName;
int? salesOrderTypeId;
int? clientId;
int? branchId;
int? agentId;
int? representativeId;
int? storeId;
int? priorityTypeId;
int? paymentTermId;
String? salesDate;
String? salesTime;
int? salesOrderStatusId;
int? salesOrderSourceId;
double? latitude;
double? longitude;
double? itemTotal;
double? itemDiscountTotal;
double? taxTotal;
double? cashDiscountTotal;
double? customDiscountTotal;
double? deliveryTotal;
double? netTotal;
String? notes;
int? invoiceRetry;
bool? hasError;
bool? isInvoiced;
String? invoiceCode;
String? invoiceDate;
int? recId;
int? cBy;
String? cDate;
int? eBy;
String? eDate;
List<SalesOrderDetail>? salesOrderDetails;
List<SalesOrderError>? salesOrderErrors;
}
Related
I have a problem with swift strings and arrays. In a function I would like to define 3 numbers (say x, y and z) and then form an array with those variables, and want to return the max() number in the array. However, I am getting an error with the type. (It is asking me to wrap to type etc.
I am posting the code below. can anyone look at it please:
func AAA(x: Int, y: Int, z:Int) -> Int {
let BBB: [Int] = [x, y, z]
var greatest: Int? = BBB.max()
return greatest
}
I am getting an error with the "greatest" variable because of its Int? type, but when I convert Int? to Int then I can not call the max value of the array BBB.
The return is a non optional -> Int and you return greatest which is Int? , so You need
func AAA(x: Int, y: Int, z:Int) -> Int {
return [x, y, z].max()!
}
I'm trying to change the priority of a constraint to be higher than that of another constraint. In Swift 4 / iOS 11, this code no longer compiles:
let p = self.lab2.contentCompressionResistancePriority(for:.horizontal)
self.lab1.setContentCompressionResistancePriority(p+1, for: .horizontal)
How am I supposed to do this?
In Swift 4 / iOS 11, UILayoutPriority is no longer a simple number; it's a RawRepresentable struct. You have to do your arithmetic by passing through the rawValue of the struct.
It may be useful to have on hand an extension such as the following:
extension UILayoutPriority {
static func +(lhs: UILayoutPriority, rhs: Float) -> UILayoutPriority {
let raw = lhs.rawValue + rhs
return UILayoutPriority(rawValue:raw)
}
}
Now the + operator can be used to combine a UILayoutPriority with a number, as in the past. The code in your question will now compile (and work) correctly.
EDIT In iOS 13 this extension is no longer needed, as it is incorporated directly into system (plus you can now initialize a UILayoutPriority without saying rawValue:). Still, it beats me why Apple ever thought it was a good idea to make a priority anything other than a number, because that is all it is or needs to be.
You can do the following to continue using them as before:
import UIKit
// MARK: - Allow to pass a float value to the functions depending on UILayoutPriority
extension UILayoutPriority: ExpressibleByFloatLiteral {
public typealias FloatLiteralType = Float
public init(floatLiteral value: Float) {
self.init(value)
}
}
// MARK: - Allow to pass an integer value to the functions depending on UILayoutPriority
extension UILayoutPriority: ExpressibleByIntegerLiteral {
public typealias IntegerLiteralType = Int
public init(integerLiteral value: Int) {
self.init(Float(value))
}
}
Refer to https://gist.github.com/jeremiegirault/7f73692a162b6ecf8ef60c7809e8679e for a full implementation
In Swift 2.3 I am able to take a [String:AnyObject] and cast it as! [[Double]]. However, using the same object in Swift 3.0, I am unable to cast it as a [[Double]]. When I cast it instead as a [[AnyObject]] or [[Any]], loop through and try and convert, I get the following error:
Could not cast value of type '__NSArrayI' (0x10783ec08) to 'NSNumber' (0x106e47320).
The following code works in my Swift 2.3 implementation, but NOT Swift 3.0
func extractCoordinatesForArea() {
// This first line is where it crashes
let theArraysOfCoordinates = myObject.geoArea!["geometry"]!["coordinates"] as! [[Double]]
var newArea:[CLLocationCoordinate2D] = []
for coordArray in theArraysOfCoordinates {
// x is LONG, LAT
let x = [coordArray[0], coordArray[1]]
// Reverse the coordinates because they are stored as LONG, LAT on the server
let y = CLLocationCoordinate2DMake(x[1],x[0])
print(y)
newArea.append(y)
}
}
While the error makes sense, I cannot seem to get this to work after having explicitly declared the type or converting in the for loop. Any help is greatly appreciated.
Try to break the first statement like this.
if let geometryDic = myObject.geoArea["geometry"] as? [String:Any],
let coordinatesArray = geometryDic["coordinates"] as? [Any] {
let theArraysOfCoordinates = coordinatesArray.first as? [[Double]] {
var newArea:[CLLocationCoordinate2D] = []
for coordArray in theArraysOfCoordinates {
//There is no need to create another array X you can directly use coordArray
// Reverse the coordinates because they are stored as LONG, LAT on the server
let coordinate = CLLocationCoordinate2DMake(coordArray[1],coordArray[0])
print(coordinate)
newArea.append(coordinate)
}
}
This question already has answers here:
Sorting NSArray containing NSDate objects
(3 answers)
Closed 6 years ago.
Are there any functions in Swift 2 that will sort single-dimensional Arrays of NSDate in ascending or descending order without writing my own sorting algorithm?
I've tried the following, but > cannot be used to compare two NSDate objects unfortunately.
var array = [NSDate]()
array.sort { $0 < $1 }
You can take pacification's answer one step further by also declaring that NSDate conforms to Comparable:
public func ==(lhs: NSDate, rhs: NSDate) -> Bool {
return lhs.isEqualToDate(rhs)
}
public func <(lhs: NSDate, rhs: NSDate) -> Bool {
return lhs.compare(rhs) == .OrderedAscending
}
extension NSDate: Comparable { }
Then you can use sort() without providing a comparator:
let dates = [NSDate]()
dates.sort()
Ok, one way of doing this:
public func <(lhs: NSDate, rhs: NSDate) -> Bool {
return lhs.compare(rhs) == .OrderedAscending
}
And then just sort dates like you want:
var array = [NSDate]()
array.sort { $0 < $1 }
I am trying to save an array of optionals Strings to NSUserDefaults, but unfortunately it doesn't work:
var textFieldEntries: [String?]
...
func encodeWithCoder(aCoder: NSCoder!) {
aCoder.encodeObject(textFieldEntries, forKey: "textFieldEntries")
// prints error: Cannot convert value of type '[String?]'
// to expected argument type 'AnyObject?'
}
[String?] is a Swift type that cannot be represented as a Foundation type. Normally, Swift Array bridges to NSArray, but an NSArray cannot contain nil. An Array of Optionals can contain nil, so it doesn't bridge automatically.
You could work around this by using a sparse array representation. (And since your content is strings — a property list type and therefore legal for use in NSUserDefaults — you don't even need to use NSCoding to encode the array.) A dictionary makes a pretty good sparse array:
var textFieldEntries: [String?] = ["foo", nil, "bar"]
func saveToDefaults() {
var sparseArray: [String: String] = [:] // plists need string keys
for (index, entry) in textFieldEntries.enumerate() {
if let e = entry {
sparseArray["\(index)"] = e
}
}
// sparseArray = ["0": "foo", "2": "bar"]
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setObject(sparseArray, forKey: "textFieldEntries")
}
Then, when you go to read in your data from defaults, convert from the sparse-array dictionary form to the array-of-optionals form. That's a little bit more fun because you need to figure out from the sparse representation how many nils you need the array to store.
func readFromDefaults() {
let defaults = NSUserDefaults.standardUserDefaults()
guard let sparseArray = defaults.objectForKey("textFieldEntries") as? [String: String]
else { fatalError("unepxected defaults key format") }
// count of the sparse array = index of highest key + 1
let count = sparseArray.keys.flatMap({Int($0)}).maxElement()! + 1
// wipe the old array to put nils in all the right places
textFieldEntries = [String?](count: count, repeatedValue: nil)
// fill in the values
for (strindex, entry) in sparseArray {
guard let index = Int(strindex)
else { fatalError("non-numeric key") }
textFieldEntries[index] = entry
}
}
(Alternately, you might know that count is constant because it's, say, the number of text fields in your UI.)
Let's say this is the original array
let textFieldEntries: [String?]
First of all let's turn the array of String? into an array of String.
let entries = textFieldEntries.flatMap { $0 }
Now we can save it
NSUserDefaults.standardUserDefaults().setObject(entries, forKey: "entries")
And retrieve it
if let retrieved = NSUserDefaults.standardUserDefaults().objectForKey("entries") as? [String] {
// here your have your array
print(retrieved)
}