How to convert a binary to decimal in Swift?

Update for Swift 2: All integer types have an public init?(_ text: String, radix: Int = default) method now, which converts a string to an integer according to a given base: let binary = “11001” if let number = Int(binary, radix: 2) { print(number) // Output: 25 } (Previous answer:) You can simply use the … Read more

Can’t cast value of type UIViewController to PatternDetailViewController

The problem, as you have said, is in these lines: if segue.identifier == “patternDetailSegue” { var detailViewController = segue.destinationViewController as! PatternDetailViewController // Could not cast value of type ‘UIViewController’ to ‘Patternz.PatternDetailViewController’ The error message tells you that the destinationViewController of this segue is not, in fact, a PatternDetailViewController. You may think it is, but it … Read more

Warning: UICollectionViewFlowLayout has cached frame mismatch for index path ‘abc’

This is likely occurring because the flow layout “xyz” is modifying attributes returned by UICollectionViewFlowLayout without copying them And sure enough, that’s just what you are doing: private override func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes? { let attributes = super.layoutAttributesForItemAtIndexPath(indexPath) let distance = CGRectGetMidX(attributes!.frame) – self.midX; var transform = CATransform3DIdentity; transform = CATransform3DTranslate(transform, -distance, 0, -self.width); … Read more

Whats the Swift animate WithDuration syntax?

Swift 3/4 Syntax Here’s an update with the Swift 3 Syntax: UIView.animate(withDuration: 0.5, delay: 0.3, options: [.repeat, .curveEaseOut, .autoreverse], animations: { self.username.center.x += self.view.bounds.width }, completion: nil) If you need to add a completion handler just add a closure like so: UIView.animate(withDuration: 0.5, delay: 0.3, options: [.repeat, .curveEaseOut, .autoreverse], animations: { // animation stuff }, … Read more

Difference between static function and singleton class in swift [closed]

Sure this sounds confusing and can be debated. However, from the best practices i can put some suggestions. Singleton is usually used to create a resource intensive and one timer initialisation for instance: a database connector, login handler and such. Utility class are classes that only have static functions and variables. It should not deal … Read more

How to configure ContextMenu buttons for delete and disabled in SwiftUI?

All of the asked situations are now supported in iOS 15 Destructive: (works from iOS 15) Set .destructive as the role argument of the button: Button(role: .destructive) { // 👈 This argument // delete something } label: { Label(“Delete”, systemImage: “trash”) } Disabled: (works from iOS 14.2) Add .disabled modifier to the button. Button { … Read more

Check if date falls between 2 dates

Swift ≧ 3 Swift 3 makes this a lot easier. let fallsBetween = (startDate … endDate).contains(Date()) Now that NSDate is bridged to the value type Date and Date conforms to Comparable we can just form a ClosedRange<Date> and use the contains method to see if the current date is included. Caveat: endDate must be greater … Read more