Custom equality in swift objects preserving compatibility with legacy Objective-C code - equality

In Objective-C you would do something along the lines of
- (BOOL)isEqual:(id)other {
if (other == self)
return YES;
if (!other || ![other isKindOfClass:[self class]])
return NO;
return [self.customProperty isEqual:other.customProperty];
}
My first naive attempt in swift goes as follows
func isEqual(other: AnyObject) -> Boolean {
if self === other {
return true
}
if let otherTyped = other as? MyType {
return self.myProperty == otherTyper.myProperty
}
return false
}
But I'm far from being happy with it. I don't even know whether the signature is right or whether we're supposed to use anything different than isEqual.
Any thoughts?
EDIT:
I'd also like to keep Objective-C compatibility (my class is used in both legacy Obj-C code and new Swift code). So I think only overriding == isn't enough. Am I wrong?

Yes, you need to override isEqual (and hash) to make your objects fully Objective-C compatible. Here's a Playground-ready example for the syntax:
import Foundation
class MyClass: NSObject {
var value = 5
override func isEqual(object: AnyObject?) -> Bool {
if let object = object as? MyClass {
return value == object.value
} else {
return false
}
}
override var hash: Int {
return value.hashValue
}
}
var x = MyClass()
var y = MyClass()
var set = NSMutableSet()
x.value = 10
y.value = 10
set.addObject(x)
x.isEqual(y) // true
set.containsObject(y) // true
(syntax current as of Xcode 6.3)

You could also implement a custom equatable, for instance:
func == (lhs: CustomClass, rhs: CustomClass) -> Bool {
return lhs.variable == rhs.variable
}
This will allow you to simply check equality like this:
let c1: CustomClass = CustomClass(5)
let c2: CustomClass = CustomClass(5)
if c1 == c2 {
// do whatever
}
Be sure your custom equatable is outside the class scope!

swift3 sig:
open override func isEqual(_ object: Any?) -> Bool {
guard let site = object as? PZSite else {
return false
}
....
}

In Swift you can override infix operators (and even make your own). See here.
So rather than using isEqual you could do:
myType == anotherType

One more example
public class PRSize: NSObject {
public var width: Int
public var height: Int
public init(width: Int, height: Int) {
self.width = width
self.height = height
}
static func == (lhs: PRSize, rhs: PRSize) -> Bool {
return lhs.width == rhs.width && lhs.height == rhs.height
}
override public func isEqual(_ object: Any?) -> Bool {
if let other = object as? PRSize {
if self === other {
return true
} else {
return self.width == other.width && self.height == other.height
}
}
return false
}
override public var hash : Int {
return "\(width)x\(height)".hashValue
}
}

To archive Objective-C compatibility you have to override isEqual method as described on page 16 of this document: https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/BuildingCocoaApps.pdf

Related

Kotlin: How to get a type of a method parameter

I know I can get the type of a method parameter by using "Method#parameters#name".
However, my parameters are all the subclass of A and I dont want to get the type A. I want to get the subclass name.
if (checkMethod(i)) {
val type = i.parameters[0].simpleName
if (!functions.containsKey(type)) {
functions[type] = HashMap()
}
if (!functions[type]?.containsKey(identifier)!!) {
functions[type]?.put(identifier, ArrayList())
}
functions[type]?.get(identifier)?.add(i)
}
Final Solution:
private fun analysis(clazz: KClass<EventHandler>, identifier: String) {
clazz.members.forEach {
if(it is KFunction) {
if(checkMethod(it)) {
val type = methodEventType(it)
if(!invokeMethods.containsKey(type)) invokeMethods[type] = HashMap()
if(!invokeMethods[type]!!.containsKey(identifier)) invokeMethods[type]!![identifier] = ArrayList()
invokeMethods[type]!![identifier]!!.add(it.javaMethod)
}
}
}
}
private fun checkMethod(method: KFunction<*>): Boolean {
method.annotations.forEach {
if(it is EventSubscriber) {
val type = method.parameters[1].type.classifier
if(type is KClass<*>) {
if(method.parameters.size == 2 && type.superclasses.contains(Event::class)) {
return true
}
}
}
}
return false
}
And notice here. I dont know why the method`s first parameter is allways a instance of its class. So the real parameter is start from 1 instead of 0.
Maybe you'll find this example useful (works with kotlin-reflect:1.4.21)
import kotlin.reflect.full.createType
import kotlin.reflect.full.isSubtypeOf
import kotlin.reflect.jvm.reflect
open class A
val aType = A::class.createType()
class B: A()
class C: A()
val foo = { b: B, c: C ->
println(b)
println(c)
}
println(foo.reflect()!!.parameters[0].type.classifier == B::class) // true
println(foo.reflect()!!.parameters[1].type.classifier == C::class) // true
println(foo.reflect()!!.parameters[0].type.isSubtypeOf(aType)) // true
To get all subclasses
println((foo.reflect()!!.parameters[0].type.classifier as KClass<*>).allSuperclasses.contains(A::class)) // true
Try this to get the class of the first parameter:
i.parameters[0]::class.java

Expose Swift Array Of Enums in Objective-C

I have this enum in swift
#objc(PaymentMethods)
public enum PaymentMethods: Int, RawRepresentable {
public typealias RawValue = String
case card
case account
case paypal
public var rawValue: RawValue {
switch self {
case .card:
return "CARD"
case .account:
return "ACCOUNT"
case .paypal:
return "PAYPAL"
}
}
public init(rawValue: RawValue){
switch rawValue {
case "CARD":
self = .card
case "ACCOUNT":
self = .account
case "PAYPAL":
self = .paypal
default:
self = .card
}
}
}
And this property in a class.
#objc public class SomeClass: ExtendingSomeOtherStuffs {
var supportedPaymentMethods:[PaymentMethods]!
}
my problem is how to bridge supportedPaymentMethods into Objective-C and use it.
I have looked at this post and this but still can't figure it out.
can someone help me out with an example at least.
Am trying to use this in Native-script and I need to expose that property from Swift to Objective
You can do it like so:
#objc public class SomeClass: NSObject {
var supportedPaymentMethods: [PaymentMethods]
#objc init(supportedPaymentMethods: [String]) {
self.supportedPaymentMethods = supportedPaymentMethods.map { .init(rawValue: $0) }
}
}
and use it like this in your Objective-C code:
[[SomeClass alloc] initWithSupportedPaymentMethods:#[#"ACCOUNT", #"CARD", #"PAYPAL"]];
If you don't want your class initializer to take any String I would suggest doing the following:
Make your init(rawValue:) failable and return nil when the argument is invalid:
public init?(rawValue: RawValue) {
switch rawValue {
case "CARD":
self = .card
case "ACCOUNT":
self = .account
case "PAYPAL":
self = .paypal
default:
return nil
}
}
Use compactMap instead of map in the init of the SomeClass like so:
#objc init(supportedPaymentMethods: [String]) {
self.supportedPaymentMethods = supportedPaymentMethods.compactMap { .init(rawValue: $0) }
}
(it will eliminate the nil values)

How do I check the currently loaded view controller ? (ObjC + Swift)

In my app I have a function where I want it to print something depending on the current view controller that is loaded. I do this by setting a global variable (Bool) and then toggling the flags in the view controller classes. From my main class I have something like this:
var FirstViewControllerisVisible: Bool = false
var SecondViewControllerisVisible: Bool = false
var ThirdViewControllerisVisible: Bool = false
#objc func PlayAgainfunc(_ sender: Any) {
if counter % 15 == 0 {
if FirstViewControllerisVisible == true {
print("First View Controller is visible")
} else if SecondViewControllerisVisible == true {
print("Second View Controller is visible")
} else if ThirdViewControllerisVisible == true {
print("Third View Controller is visible")
}
}
counter += 1
}
Then, in those classes I can set the flags like this:
override func viewDidAppear(_ animated: Bool) {
FirstViewControllerisVisible = true
}
override func viewDidDisappear(_ animated: Bool) {
FirstViewControllerisVisible = false
}
This worked great when it was exclusively Swift, but the problem with global variables is that they can't be accessed by Objective-C. I can't find a way to set flags in my ObjC classes and then check if true or false. For instance, If I tried:
- (void)viewDidAppear:(BOOL)animated; {
[(FirstViewControllerisVisible) == true];
}
- (void)viewDidDisappear:(BOOL)animated; {
[(FirstViewControllerisVisible) == false];
}
I would get the error 'use of undeclared identifier' because my global vars declared in Swift are Swift only.
After the view controller has loaded, you can
if let viewController = UIApplication.shared.keyWindow?.rootViewController {
print(type(of: viewController))
}
add below mentioned extension. you will get the desired result.
extension UIApplication {
/// will return currently showing view controller
static var topMostViewController: UIViewController? {
return UIApplication.shared.keyWindow?.rootViewController?.visibleViewController
}
}
extension UIViewController {
/// The visible view controller from a given view controller
var visibleViewController: UIViewController? {
if let navigationController = self as? UINavigationController {
return navigationController.topViewController?.visibleViewController
} else if let tabBarController = self as? UITabBarController {
return tabBarController.selectedViewController?.visibleViewController
} else if let presentedViewController = presentedViewController {
return presentedViewController.visibleViewController
} else {
return self
}
}
}
and access it like:
let viewController = UIApplication.topMostViewController
Happy Coding

How do we return an optional enum value from Swift to Obj-C?

We know how to expose Swift enums to Obj-C:
#objc enum Animal: Int {
case Cat, Dog
}
But the compiler complains that the following "cannot be represented in Obj-C":
func myAnimal() -> Animal? {
if hasPet() {
return .Cat
} else {
return nil
}
}
Ideas?
Optional Ints in Swift don't map to a type in Objective-C. The issue is that your myAnimal() method is returning a type that can't be represented in Objective-C.
The way I see it, you have two options...
Option1: Change your method's return type:
func myAnimal() -> NSNumber? {
// method body goes here.
}
This doesn't really seem great since in Objective-C you'd have to do something like this:
if (myAnimal().integerValue == AnimalCat) {
// do stuff here
}
Option 2: Add a catch-all case in your enum
#objc enum Animal: Int {
case none = 0
case cat = 1
case dog = 2
init(rawValue: Int) {
switch rawValue {
case 1: self = Cat
case 2: self = Dog
default: self = None
}
}
}
// Updated method signature that makes the compiler happy.
func myAnimal() -> Animal {
if hasPet() {
return .Cat
} else {
return .None
}
}
This way, you can change your method signature to not return an optional, and the compiler will be happy.

Swift: write a function that allocates an instance of (any) class

I'm looking for the equivalent of the following Obj-C code in Swift:
- newInstanceOf:(id)classRef {
return [classRef new];
}
and then to use it:
id instance = [whatever newInstanceOf:NSArray.class]
[instance isKindOfClass:NSArray.class] == YES
I have tried using a Swift template:
func newSomething<T>(classRef:T.Type) -> T {
return classRef()
}
I get the error: error: 'T' cannot be constructed because it has no accessible initializers
You could create a protocol to act as a type constraint for objects initializable by a void-argument initializer, and thereafter extend your types of choice to this protocol.
protocol SimplyInitializable {
init()
}
extension Int : SimplyInitializable { }
extension Double : SimplyInitializable { }
extension String : SimplyInitializable { }
struct MyStruct {
var myInt : Int
init() {
myInt = 0
}
}
extension MyStruct : SimplyInitializable { }
func newSomething<T: SimplyInitializable>(classRef: T.Type) -> T {
return classRef.init()
}
/* Examples */
var a = newSomething(Int)
var b = newSomething(Double)
var c = newSomething("".dynamicType)
var d = newSomething(MyStruct)
var e = newSomething(a.dynamicType)
print(a.dynamicType) // Int
print(b.dynamicType) // Double
print(c.dynamicType) // String
print(d.dynamicType) // MyStruct
print(e.dynamicType) // Int
Actually in Swift, not all classes are guaranteed to have an init() initializer. It can work with NSObject classes though because NSObject does have that requirement.
func newInstanceOf<T:NSObject>(aClass:T.Type) -> NSObject
{ return aClass.init() }
let string = newInstanceOf(NSString) // ""
let date = newInstanceOf(NSDate) // 2016-01-15 05:27:29 +0000