Created
June 22, 2016 10:21
-
-
Save gauravds/13c42d610cf572fc0ec11b68f39357e8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// | |
/// Author : Gaurav D. Sharma | |
/// Run on Swift 3.0 | |
/// IBM swift sandbox : https://swiftlang.ng.bluemix.net/#/repl | |
/// | |
//// Are you using ObjectMapper for model mapping in swift. | |
//// Object creation like Mapper<T>().map(jsonString) | |
//// And you are irritaed with the type name passing and syntex memorization | |
//// | |
//// what about below code a cleaner approach to make it | |
// public func GetModelObject<T: Mappable>(type: T.Type, jsonString: String) -> T? { | |
// return Mapper<T>().map(jsonString) | |
// } | |
//// | |
//// | |
// GetModelObject(type:UserModel.self, jsonString: json) | |
//// | |
////---- Below is the whole discuss about this | |
// public func get<T: Pro1>(type: T.Type) { | |
// GenericClass<T>().printObj() | |
// } | |
//------------------------- TEST 1 ------------------------- | |
public protocol Pro1 { | |
init() | |
var name: String { get } | |
} | |
public class Class1 : Pro1 { | |
required public init() {} | |
public var name: String { return "Class 1" } | |
} | |
public class Class2 : Class1 { | |
override public var name: String { return "Class 2" } | |
} | |
public class GenericClass<T: Pro1> { | |
public func printObj() { print(T().name) } | |
} | |
//------------------------- TEST 2 ------------------------- | |
public protocol Pro2 { | |
static func get<T : Pro1>(type: T.Type) | |
} | |
extension Pro1 where Self: Pro2 { | |
public static func get<T : Pro1>(type: T.Type) { | |
GenericClass<T>().printObj() | |
} | |
} | |
public class Class3 : Class1, Pro2 { | |
override public var name: String { return "Class 3" } | |
} | |
public class Class4 : Class3 { | |
override public var name: String { return "Class 4" } | |
} | |
//-- Desirable Method : Here Magic happens | |
public func get<T: Pro1>(type: T.Type) { | |
GenericClass<T>().printObj() | |
} | |
//---------------------------------------------------------------- | |
public func test1() { | |
GenericClass<Class1>().printObj() // Class 1 | |
GenericClass<Class2>().printObj() // Class 2 | |
} | |
test1() | |
//---------------------------------------------------------------- | |
public func test2() { | |
Class3.get(type: Class3.self) // Class 3 | |
Class4.get(type: Class4.self) // Class4 | |
// -- Gives undesirable output; | |
// - Type mistaken pass; so not recommended way | |
// - No impact on the call of class method. | |
Class3.get(type: Class4.self) // Class 4 ; Desired Class3 | |
Class4.get(type: Class3.self) // Class 3 ; Desired Class4 | |
// Much better and cleaner way [recommended] | |
get(type: Class3.self) // Class 3 | |
get(type: Class4.self) // Class 4 | |
} | |
test2() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment