Determine Swift Array Type - swift

I'm trying to determine the type of a collection using Swift. That is my goal. It does not appear to be supported by the new language so I tried to fall back on Objective-C. Collection generics is a new feature and I believe was only added for Swift interoperability.
Is anyone aware of a feature that will allow me to inspect the array type of d?
class SuperClass : NSObject { }
class SubClass: SuperClass { }
let a = SubClass()
if a.isKindOfClass(SuperClass) {
// this works as expected like objective-c
print("yes")
}
let b = Array<SubClass>()
if b.isKindOfClass(Array<SuperClass>) {
// error: value type of 'Array<SubClass>' has no member isKindOfClass
print("yes")
}
if b is Array<SuperClass> {
// error: 'SuperClass' is not a subtype of 'SubClass'
print("yes")
}

You can simply test it with Array<String>() is Array<String> but If you wanna know which type have any variable you can look at d.dynamicType
#crashmstr is right

In your case b is an Array literal (a value type), not a reference type. So it can't call isKindOfClass.

try this
if let array = b as? Array<SuperClass>
{
//perform some action
}

Related

Is it possible to type-check against a list of types?

I know that it's possible to check if one value is an instance of a potential supertype:
func isInstance<T, U>(_ instance: T, of: U.Type) -> Bool {
return instance is U
}
However, what if you want to check against an entire array? Since you can't have an array of generics, the above approach doesn't really work. What I want to do is something like:
func isInstance<T>(_ instance: T, of types: [Any.Type]) -> Bool {
return types.allSatisfy { instance is $0 }
}
However, a variable (such as $0) isn't allowed as the RHS of an is expression. Is this kind of type check possible?
I saw one type check that used Mirror(reflecting:) and superclassMirror to search the inheritance hierarchy, but that only works for an array of classes, and I need this check to work when the array contains protocols as well.
It is possible, but only for classes and #objc protocols, and only if the object conforms to NSObjectProtocol. It is not possible in the general case for Swift protocols or value types.
import ObjectiveC
func isInstance(_ instance: some NSObjectProtocol, of types: [Any.Type]) -> Bool {
return types.allSatisfy { type in
if let `class` = type as? AnyClass {
return instance.isKind(of: `class`)
} else if let `protocol` = type as AnyObject as? Protocol {
return instance.conforms(to: `protocol`)
} else {
print("Cannot type-check instance against \(type)")
return false
}
}
}

How can I use a protocol's type in method signature?

I have a function like this:
// A generic function that can fetch values from the store given a type
// Corresponds to a table and it's rows
func fetch<T: FetchableRecord>(for theType: T.Type) -> [T] {
// ... fetch from a store using the type
// This compiles fine
}
How can I use this with a collection of types?
Ideally, if I have some conformers:
struct ModelA: FetchableRecord { }
struct ModelB: FetchableRecord { }
then I would like to be able to do:
let modelTypes: [FetchableRecord.Type] = [ModelA.self, ModelB.self]
modelTypes.forEach {
fetch(for: $0) // xxx: does not compile: "Cannot invoke 'fetch' with an argument list of type '(for: FetchableRecord.Type)'"
}
At the very least, I would like to figure why this would not be possible.
Thank you.
The reason for the error is FetchableRecord.Type is not the same as ModelA.Type or ModelB.Type. Even if both of the structs conform to FetchableRecord protocol, constraining the models (by conforming to a certain protocol) does not affect the "Types", more technically speaking:
ModelA.self == FetchableRecord.self OR ModelB.self == FetchableRecord.self is false.
In order to resolve this issue, you could implement the method's signiture as:
func fetch(for theType: FetchableRecord.Type) -> [FetchableRecord] {
// ...
}
therefore, your code:
let modelTypes: [FetchableRecord.Type] = [ModelA.self, ModelB.self]
modelTypes.forEach {
fetch(for: $0)
}
should work. At this point, you are dealing with it "Heterogeneously" instead of "Homogeneously".
Furthermore, if that makes it more sensible, note that when calling fetch method as per your implementation, it is:
the parameter type is FetchableRecord.Protocol. However, as per the above-mentioned implementation (in this answer), it is:
the parameter type is FetchableRecord.Type, which is the wanted result.

Generic constrained type default value

Consider the following code:
protocol JSONParserType {
associatedtype Element
}
// MARK: - Entities
struct Item {}
// MARK: - Parsers
struct OuterParser<T: JSONParserType where T.Element == Item>: JSONParserType {
typealias Element = Item
let innerParser: T
init(innerParser: T = InnerParser()) {
self.innerParser = innerParser
}
}
struct InnerParser: JSONParserType {
typealias Element = Item
}
The OuterParser has a child parser that should be constrained to a specific type. Unfortunately providing a default value in the initializer (or in the property definition itself) does lead to the compiler throwing a "Default argument value of type 'InnerParser' cannot be converted to type 'T'".
If I remove the default value assignment and just instantiate the OuterParser providing the InnerParser explicitly, everything is fine.
let outerParser = OuterParser(innerParser: InnerParser())
My question is what's the reason that the approach providing a default value that actually meets the constraints does not work.
The problem is that the actual type of T isn't defined by the class – it's defined by the code that uses the class. It will therefore be defined before you do anything in your class (at either instance or static level). You therefore can't assign InnerParser to T, as T has already been defined to be a given type by that point, which may well not be InnerParser.
For example, let's consider that you have another parser struct:
struct AnotherParser: JSONParserType {
typealias Element = Item
}
and let's assume that your current code compiles. Now consider what would happen when you do this:
let parser = OuterParser<AnotherParser>()
You've defined the generic type to be AnotherParser – but the initialiser will try to assign InnerParser to your property (now of type AnotherParser). These types don't match, therefore it cannot possibly work.
Following the same logic, this implementation also won't work:
struct OuterParser<T: JSONParserType where T.Element == Item>: JSONParserType {
typealias Element = Item
let innerParser: T
init() {
self.innerParser = InnerParser()
}
init(innerParser: T) {
self.innerParser = innerParser
}
}
As there's no guarantee that the generic type T will be the same type as InnerParser. Sure, you can force downcast to T – but that'll just make you code crash if the types aren't compatible.
Unfortunately, there's no real clean solution to this problem. I think the best your best option is probably to create two factory methods for creating your OuterParser instance.
enum Parser {
static func createParser() -> OuterParser<InnerParser> {
return OuterParser(innerParser:InnerParser())
}
static func createParser<T>(innerParser:T) -> OuterParser<T> {
return OuterParser(innerParser:innerParser)
}
}
let innerParser = Parser.createParser() // OuterParser<InnerParser>
let anotherParser = Parser.createParser(AnotherParser()) // OuterParser<AnotherParser>
We're using an caseless enum here to avoid polluting the global namespace with extra functions.
Although this isn't very Swifty, and for that reason I would also recommend maybe rethinking your logic for how you define your parsers.
type T more like a child protocol of JSONParserType you can convert it:
init(innerParser: T = InnerParser() as! T) {
self.innerParser = innerParser
}

Get the real type of a protocol to compare with type stored in a variable

I would like to be able to check the object's type of an object that implements a certain protocol.
In the following method, I would like to be able to loop over all the objects from my array of IObject objects and find the one that has as its real type, the type passed in parameter.
func findObject(forType type: Any) -> IObject? {
for (key, value) in self.objects {
if value.Type == type {
return value
}
}
return nil
}
When I try to compile the above code, I've an error message saying: 'IObject' does not have a member named 'Type'.
Any suggestion?
I am using Swift 2 with Xcode 7 beta 6.
I found this question interesting. The only way I have managed to get this to work is by string comparison - i.e. if "\(object.dynamicType)" == "\(forType)".
Also - if I have properly understood the question then I think that the function possibly needs to return an optional array of IObject - because the array of objects may contain more than one of the given type.
protocol IObject: class {}
class SomeObject1: IObject {}
class SomeObject2: IObject {}
let myIObject01 = SomeObject1()
let myIObject02 = SomeObject2()
let myIObject03 = SomeObject1()
let myIObject04 = SomeObject1()
let myIObject05 = SomeObject2()
var objects: [IObject] = [myIObject01,myIObject02,myIObject03,myIObject04,myIObject05]
func findObject(forType: Any) -> [IObject]? {
var result = [IObject]()
for object in objects {
if "\(object.dynamicType)" == "\(forType)" {
result.append(object)
}
}
return result
}
let result = findObject(SomeObject1)
Try to replace this code:
if value.Type == type
to this:
if value.dynamicType == type
If it still doesn't work, try this:
if value.dynamicType == type.self

Swift 2: understanding AnyObject and Self

I couldn't find any good explanation to my questions so I'd like to ask you directly. First of all I'd like to refine my code in this post.
My problem is the protocol AnyObject and the Self type. I didn't implement AnyObject into my code because it is marked with #objc and I don't want any Objective-C stuff involved in my code (don't judge me for that). I also couldn't find any explanation about the Self type. It just worked as expected, but Xcode does not replace Self with the type the static function is called at.
Here is some example:
extension Int : Instance {}
Int.singleton { (customInstanceName) -> Self in 0 } // Self shall be replaced with Int
As you can see Xcode produces a Self instead an Int. Is there any chance I could fix this? Am I right that Self does return the dynamicType and my implementation is fine as it is in my post above? I would really appreciate any good explanation about the Self type.
As you have seen in my code. I am using a custom protocol to check whether my instance is a class or not. Is there any other shiny implementation to check my instances if they are classes or structure types, or am I forced to use AnyObject if I want to get rid of my ClassInstance protocol?
Thank you for your time.
UPDATE:
protocol Test {}
class A : Test {}
struct B : Test {}
let aClass : Test = A()
let aStruct : Test = B()
if let someClass = aClass as? AnyObject {
print(someClass) // only this will print
}
if let someStruct = aStruct as? AnyObject {
print(someStruct)
}
This will work, but AnyObject is still marked as an #objc protocol.
The Self type can be only used in protocols where it is a implicit typealias of the type which conforms to it:
protocol Testable {
func test() -> Self
}
If you want to conform to this protocol you than have to replace Self with the name of the type. For instance:
struct Product: Testable {
func test() -> Product {
return Product()
}
}
Important Edit:
As DevAndArtist pointed out in the comments there is a working class check in Swift 1.2 (without automatic bridging to Objective C) but not Swift 2 (Xcode 7 beta 3; probably a bug):
if instance.dynamicType is AnyClass {
// instance is a class
} else {
// instance is not a class
}
You can see workaround (mainly) for Swift 2 below.
End Edit
With respect to classes you should use AnyObject if you want to keep it simple but you can also use reflection which would be much more effort.
Below you can see some reflection results of string interpolations (only the first few characters):
"\(reflect(classType))" // Swift._ClassMirror
"\(reflect(0))" // Swift._LeafMirror
"\(reflect(enumType))" // Swift._EnumMirror
"\(reflect(structure))" // Swift._StructMirror
"\(reflect([0, 4]))" // Swift._ArrayTypeMirror
"\(reflect(NSDate()))" // Foundation._NSDateMirror
"\(reflect(NSURLRelationship.Contains))" // Swift._EnumMirror
"\(reflect(Int?(2)))" // Swift._OptionalMirror
As you can see enums are consistent if they are not defined in the Swift standard library (unfortunately also Optional...). So you can distinguish also structs and enums:
public enum Type {
case Enum, Class, Struct
}
public func getType<T>(anything: T) -> Type {
if anything is AnyObject {
return .Class
}
if "\(reflect(anything))".hasPrefix("Swift._EnumMirror") {
return .Enum
}
return .Struct
}
So for a better result you have to put some effort into it to differentiate between all the different cases.
But the easiest way to distinguish only between reference types and value types (aka classes and structs/enums) is still (unfortunately only works for own declared structs and not built in types because they can be bridged to Objective C; I'm working on it...):
if instance is AnyObject {}
// or: if instance is of type Any
if let classInstance = instance as? AnyObject {}