logo
down
shadow

Compare Day/Month in Swift Struct


Compare Day/Month in Swift Struct

By : hiraishi
Date : November 22 2020, 02:59 PM
may help you . I would like sort an array of SpecialDay instances: [struct1, struct2, struct3, ...] using the code below: , The problem is the and in this line:
code :
return lhs.day < rhs.day && lhs.month <= rhs.month
func <(lhs: SpecialDay, rhs: SpecialDay) -> Bool {
    if lhs.month < rhs.month { return true }
    else if lhs.month > rhs.month { return false }
    else {
        return lhs.day < rhs.day
    }
}


Share : facebook icon twitter icon
SQL SUM values of month, compare with same days of same month from previous year for specific names

SQL SUM values of month, compare with same days of same month from previous year for specific names


By : user2899764
Date : March 29 2020, 07:55 AM
hope this fix your issue So consider the following tables: , You can do this with conditional aggregation. Here is an approach
code :
select m.name,
       sum(case when year(startdate) = 2014 and day(startdate) <= 10 then summary_value end) as thisyear,
       sum(case when year(startdate) = 2013 and day(startdate) <= 10 then summary_value end) as lastyear
from meters m join
     meter_values mv
     on m.meterentryid = mv.emterentryid
where year(startdate) in (2013, 2014) and
      month(startdate) = 10
group by m.name;
with d as (
      select date('2014-10-10') as dte
     )
select m.name,
       sum(case when year(startdate) = 2014 and day(startdate) <= day(d.dte) then summary_value end) as thisyear,
       sum(case when year(startdate) = 2013 and day(startdate) <= day(d.dte) then summary_value end) as lastyear
from d cross join
     meters m join
     meter_values mv
     on m.meterentryid = mv.emterentryid
where year(startdate) (year(d.dte), year(d.dte) - 1) and
      month(startdate) = month(d.dte)
group by m.name;
Is there way to define compare (`==`) function automatically for `struct` in Swift?

Is there way to define compare (`==`) function automatically for `struct` in Swift?


By : Laszlo Nacsa
Date : March 29 2020, 07:55 AM
Does that help The following answer shows one possible solution; possibly not a recommended one (however possibly of interest for future readers of this question).
code :
/* Let a heterogeneous protocol act as "pseudo-generic" type
   for the different (property) types in 'SuperStruct'         */
protocol MyGenericType {
    func isEqualTo(other: MyGenericType) -> Bool
}
extension MyGenericType where Self : Equatable {
    func isEqualTo(other: MyGenericType) -> Bool {
        if let o = other as? Self { return self == o }
        return false
    }
}

/* Extend types that appear in 'SuperStruct' to MyGenericType  */
extension Int : MyGenericType {}
extension String : MyGenericType {}
extension Float : MyGenericType {}
    // ...

/* Finally, 'SuperStruct' conformance to Equatable */
func ==(lhs: SuperStruct, rhs: SuperStruct) -> Bool {

    let mLhs = Mirror(reflecting: lhs).children.filter { $0.label != nil }
    let mRhs = Mirror(reflecting: rhs).children.filter { $0.label != nil }

    for i in 0..<mLhs.count {
        guard let valLhs = mLhs[i].value as? MyGenericType, valRhs = mRhs[i].value as? MyGenericType else {
            print("Invalid: Properties 'lhs.\(mLhs[i].label!)' and/or 'rhs.\(mRhs[i].label!)' are not of 'MyGenericType' types.")
            return false
        }
        if !valLhs.isEqualTo(valRhs) {
            return false
        }
    }
    return true
}
/* Example */
var a = SuperStruct()
var b = SuperStruct()
a == b // true
a.field1 = 2
a == b // false
b.field1 = 2
b.field2 = "Foo"
a.field2 = "Foo"
a == b // true
/* 'SuperStruct' conformance to Equatable */
func ==(lhs: SuperStruct, rhs: SuperStruct) -> Bool {

    let mLhs = Mirror(reflecting: lhs).children.filter { $0.label != nil }
    let mRhs = Mirror(reflecting: rhs).children.filter { $0.label != nil }

    for i in 0..<mLhs.count {
        switch mLhs[i].value {
        case let valLhs as Int:
            guard let valRhs = mRhs[i].value as? Int where valRhs == valLhs else {
                return false
            }
        case let valLhs as String:
            guard let valRhs = mRhs[i].value as? String where valRhs == valLhs else {
                return false
            }
        case let valLhs as Float:
            guard let valRhs = mRhs[i].value as? Float where valRhs == valLhs else {
                return false
            }
            /* ... extend with one case for each type
            that appear in 'SuperStruct'  */
        case _ : return false
        }
    }
    return true
}
/* Example */
var a = SuperStruct()
var b = SuperStruct()
a == b // true
a.field1 = 2
a == b // false
b.field1 = 2
b.field2 = "Foo"
a.field2 = "Foo"
a == b // true
How to Handle Encoding a Swift Struct Containing Swift Struct as Property

How to Handle Encoding a Swift Struct Containing Swift Struct as Property


By : elinor
Date : March 29 2020, 07:55 AM
To fix this issue It appears that I should create an encapsulating type to ensure that the address property of my Location instance can be encoded and decoded. That will allow me to simply call coder.encode(address, forKey: "address").
I found a helpful answer after changing my search query terms.
compare two values of one struct and getting extra cells in UITableView swift 4.0

compare two values of one struct and getting extra cells in UITableView swift 4.0


By : Amna
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further First of all, you should filter out the elements from the array that you don't want to show in the table view. Then you will use that filtered array for your data source. You won't have to write the weird condition in your cellForRowAt:.. function.
code :
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    let arrayOne = [One(cellID: "1", name: "hello", lastName: "hello last", cellID2: "1"), One(cellID: "1", name: "hello", lastName: "hello last", cellID2: "1"), One(cellID: "1", name: "hello", lastName: "hello last", cellID2: "2"), One(cellID: "1", name: "hello", lastName: "hello last", cellID2: "1"), One(cellID: "1", name: "hello", lastName: "hello last", cellID2: "2"), One(cellID: "1", name: "hello", lastName: "hello last", cellID2: "1")]

    var filteredArray = [One]()

    @IBOutlet weak var compareTableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Here you apply your filter
        filteredArray = arrayOne.filter { $0.cellID == $0.cellID2 }
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return filteredArray.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "CompareTableViewCell") as! CompareTableViewCell
        let arrayID = filteredArray[indexPath.row]
        // Feed your cell
        return cell
    }
}
Compare value from two struct in swift

Compare value from two struct in swift


By : ashmere
Date : March 29 2020, 07:55 AM
hop of those help? To get objects of array oneData whose ID matched with ID of any object from array twoData. You can do following:
code :
    // Array One
    var oneData = [One]()
    oneData = [One(ID: "1", name: "hello1", lastName: "last2"),
               One(ID: "1", name: "hello2", lastName: "last2"),
               One(ID: "2", name: "hello3", lastName: "last3"),
               One(ID: "3", name: "hello4", lastName: "last4")]

    // Array Two
    var twoData = [Two]()
    twoData = [Two(ID: "1", name2: "hello1", lastName2: "last1"),
               Two(ID: "2", name2: "hello2", lastName2: "last2"),
               Two(ID: "3", name2: "hello3", lastName2: "last3"),
               Two(ID: "4", name2: "hello4", lastName2: "last4"),
               Two(ID: "5", name2: "hello5", lastName2: "last5")]

    let mainArray = oneData.filter { i in twoData.contains{ i.ID == $0.ID } }
    print(mainArray)
Related Posts Related Posts :
  • Adding Multiple Images to UIPageViewController using Camera
  • Should I ever prefer operators overloading over functions/methods?
  • How to call/get date and time from a website in swift
  • rxswift error handle issue
  • Cannot stream video with swift 2 AVPlayerViewController
  • PFFile as UIImageView
  • Sort dictionary keys by value, then by key
  • What's going on with this generic function?
  • How to implement a failable initializer for a class conforming to NSCoding protocol in Swift?
  • Swift: optional or implicit UILabel?
  • Swift: ¨Unrecognized selector sent to instance¨
  • Initialize a dictionary with non-optional empty arrays in Swift
  • swift reflection causes impossible nil value for any
  • Equatable in Swift
  • How to populate an SKShapeNode uniformly
  • Using AWS API Gateway generated SDK for both authenticated and anonymous access
  • Underscored properties in Swift classes?
  • Subclass of UIButton with rounded corners (swift)
  • Google API Maps don't show up on TabBar Application
  • Can't hide status bar in AVPlayerViewController's portrait mode
  • Send JSON Array and store in database using api calls
  • Return one of two possible classes instances
  • What is type of 123_456_789?
  • dispatch queue concurrent not work as except in swift
  • Calculate count of years
  • Swift property observer in protocol extension?
  • Calling function with optional type in Swift
  • in Swift, what does this specific syntax mean?
  • Nil cannot be assigned to type AVCaptureDeviceInput
  • Parse - Query with 'includeKey' not getting objects in proper subclass
  • Best way to set up multiple tableviewcell layouts
  • Swift Bridging Header for Obj-C
  • Swift dictionary with weak reference keys?
  • Commit failed - the repository is locked, XCode 7
  • How to convert Swift Bool? -> String?
  • Swift: Cannot convert value of type 'NSDate' to expected argument type 'NSDateComponents'
  • JSON parsing error in Swift 2.0
  • Varying value inside runAction() not changing
  • Swift - bar button item image / set size and width
  • Radio button UI in Swift
  • NSdate range total removing weekends Swift
  • Why isn't addTarget calling my target's action in an extension
  • How to make a cell hold multiple text labels in ios
  • Setter for dictionary property - OR: get last added item from dictionary
  • Extending Dictionary with constraints
  • Spritekit rotating multiple nodes around center
  • Swift: Trying to implement NSCoding
  • Array of String printing Optional, why?
  • Dynamic attributes (getters/setters) in Swift on data models
  • Setting file attributes in Swift
  • Window visible on all spaces (including other fullscreen apps)
  • How to draw lines on UIImage with swift ?
  • Is it possible use non read-only computed property in extension?
  • How do I save a MKPointAnnotation to Pars?
  • Take a snapshot of current screen with Metal in swift
  • Is there any way to determine sub-protocol conformance for instance conforming to generic protocol in Swift 2 (at runtim
  • Custom physics body adding unwanted extra line
  • Define a Swift protocol which requires a specific type of sequence
  • Swift tvOS in-app purchase doesn't respond
  • Swift calculate incremental average with reduce and
  • shadow
    Privacy Policy - Terms - Contact Us © animezone.co