Asserting class and protocol

Great method that asserts if an instance is of a class type or of a protocol type

	protocol IDescribable:class{var text:String{get}}
	class A:IDescribable{var text:String;init(_ text:String){self.text = text}}
	class B:A{}
	class C{}

	let a = A("I am a")
	let b = B("I am b")
	let c = C()

	func ofType<T>(instance:Any?,_ type:T.Type) -> T?{/*<--we use the ? char so that it can also return a nil*/
	    if(instance as? T != nil){return instance as? T}
	    return nil

	Swift.print(ofType(a,A.self)!.text)//I am a
	Swift.print(ofType(a,IDescribable.self)!.text)//I am a
	Swift.print(ofType(b,B.self)!.text)//I am b
	Swift.print(ofType(c,C.self))//instance of c

For future reference there is also these available:

class func isOfClassType(obj:AnyObject,_ classType:AnyClass)->Bool{
    return obj.isMemberOfClass(classType)
 * Alternative to the above
class func isOfClassType2(obj:AnyObject, _ classType:AnyClass?, _ protocolType:AnyObject? = nil/**/)->Bool? {
    return object_getClass(obj) == classType!

This also works (not applicable inside a method where you pass the class type)

class A{}
class B:A{}
class C{}

let x = B()

x is A//true
x is B//true
x is C//false

An early idea before I figured out i could use generics to solve the problem:

String(instance.dynamicType) == NSStringFromProtocol(IPositional.self)
//As long as you can pull the name from the protocol you can use that to assert. dynamiccType should return the protocol of an instance

This is a modified version of my type assertion technique. I added the “as? AnyClass” assert so that the type can only be of protocol type. You didn’t specify a specific protocol so I assume this is what you wanted. There might be a more elegant way of doing this but going through my notes and research about class assertion this is what I came up with.

import Foundation

protocol IDescribable:class{}
class A:IDescribable{}
class B:A{}

let a = A()
let b = B()

func ofType<T>(instance:Any?,_ type:T.Type) -> T?{/*<--we use the ? char so that it can also return a nil*/
    if(instance as? T != nil && type as? AnyClass == nil){return instance as? T}
    return nil


Related Posts

Startup Oslo

“Kulturhuset” is a new place in the startup scene in Oslo. It’s “free” and can be used by anyone to collaborate or work on different projects.

Infinite Tree List

My notes on Infinite tree list

Protocol Inheritance

My notes on Protocol Inheritance

Protocol Ambiguity

How to differentiate protocol ambiguity

The Ultimate Xcode Workflow

Spend zero time managing dependencies

Faster Xcode With Spm

How you can speed up compile times in XCode with Swift Package Manager

Spm And Ci Travis

My notes on Swift PM + CI Travis

Spm And Nested Frameworks

My notes on Swift package manager + XCode + Nested frameworks

Xcode And Spm

Here is how you use Swift package manager in your XCode app projects

Carthage And Nested Frameworks

A few workflows concerning Carthage and nested framework