How do I determine the value of a generic variable at runtime? - swift

The following snippet shows the generic variable 'A'.
I'm trying to figure out the source of nil being returned by this load().
Here's a the code snippet:
final class Cache {
var storage = FileStorage()
// 1) 'load': Read from the Cache:
func load<A>(_ resource: Resource<A>) -> A? {
print("--- LOAD ---")
guard case .get = resource.method else { return nil }
// Type of 'data' is optional.
let diskData = storage[resource.cacheKey] // ...type: Data? (optional-Data).
// Want to convert diskData to A? (optional-A):
return diskData.flatMap(resource.parse) // ...cleaning data, removing nils.
}
// 2) 'save' to the Cache:
func save<A>(_ data: Data, for resource: Resource<A>) {
print("--- SAVE ---")
guard case .get = resource.method else { return }
self.storage[resource.cacheKey] = data
}
}
// ----------------------------------
// MARK: - Resource
public struct Resource<A> {
public var url: URL
public var parse: (Data) -> A? // ... convert Data to A?
public var method: HttpMethod<Data> = .get
public init(url: URL, parse: #escaping (Data) -> A?, method: HttpMethod<Data> = .get) {
self.url = url
self.parse = parse
self.method = method
}
}
// ----------------------------------
In the meantime, How I can determine the value of any generic variable during runtime-debugging?
That is, who do I determine the value of A?
Note: code is from a shared source via 'Swift Talk' on objc.io.

To find out what type A represents, print the type of a variable that has a type that include the generic type placeholder.
In your case, resource has type Resource<A> so:
p type(of: resource)
will show something like:
<ProjectName>.Resource<Int>
in which case A is Int.

Related

How can I type check a generic then cast to it in swift?

I like the ideas presented in this post, about making database-agnostic, protocol-oriented code.
So say I have a protocol such as:
protocol Database {
func loadObjects<T>(matching query: Query) -> [T]
func loadObject<T>(withID id: String) -> T?
func save<T>(_ object: T)
}
where Query is a struct that has filter and sort specifiers.
Then, given a persistence framework, such as Realm or CoreData, I can just support this protocol, as such:
extension NSManagedObjectContext: Database {
...
}
extension Realm: Database {
...
}
extension MockedDatabase: Database {
...
}
extension UITestingDatabase: Database {
...
}
The issue arises when I would want to use CoreData.
If we look at the method:
func loadObjects<T>(matching query: Query) -> [T]
I have no way to 'cast' T to NSManagedObject.
For example, my desired implementation might be something like:
extension NSManagedObjectContext: Database {
func loadObjects<T>(matching query: Query<T>) -> [T] {
// you need a fetch request for these models. This guard statement compiles. How do we make it work with NSFetchRequestResult however?
guard T.self is NSManagedObject.Type else {
return []
}
// This line below Fails compiling. Type 'T' does not conform to protocol 'NSFetchRequestResult'
var request = NSFetchRequest<T>(entityName: String(describing: T))
// then set sortDescriptors and predicate, etc.
var objects: [T] = []
self.performAndWait {
do {
if let results = try self.fetch(request!) as? [T] {
objects = results
}
} catch let error {
print("Error fetching: \(error.localizedDescription)")
}
}
return objects
}
}
So if I can determine if T's Type is a kind of NSManagedObject, then is it possible to instantiate a NSFetchRequest by 'casting' T to something that will work? It would seem I can't cast or force T to be anything.
Database is a technology-agnostic protocol and therefore shouldn't know anything about Core Data. I'd like to do this in the event I need to change my data persistence framework.
How might I accomplish this in Swift? Would I have to add to the Model protocol to return optionals that would be used for the given frameworks I would support? Or make them support NSFetchRequestResult? I would rather that only the implementation of the protocol need to care about the details of the persistence framework.
Rather than checking the type at runtime constrain the type at compile time, for example
extension NSManagedObjectContext: Database {
func loadObjects<T: NSManagedObject>(matching query: Query<T>) -> [T] {
let request = NSFetchRequest<T>(entityName: String(describing: T.self))
var objects = [T]()
self.performAndWait {
do {
objects = try self.fetch(request) // a generic fetch request returns an array of the generic type or throws an error
} catch let error {
print("Error fetching: \(error.localizedDescription)")
}
}
return objects
}
}
It looks like I can answer my own question. One can further constrain generic types by overloading them, so that the calling code can remain the same, but what gets called is dependent on the type you pass into it.
This code below demonstrates this in a playground:
public protocol Action {
func doSomething<T>(to object: T)
}
public class MyActor {
}
extension MyActor: Action {
// works for any type
public func doSomething<T>(to object: T) {
print("was generic")
}
// but if you constrain the type and your object fits that constraint...
// this code is called (same method signature)
public func doSomething<T: NSObject>(to object: T) {
print("was an object")
}
}
class MyObject: NSObject {
var name: String = "Object"
}
struct MyStruct {
var name: String = "Struct"
}
let actor = MyActor()
let object = MyObject()
let value = MyStruct()
actor.doSomething(to: value) // prints 'was generic'
actor.doSomething(to: object) // prints 'was an object'
So in the original example, I would then support Database for CoreData with:
extension NSManagedObjectContext: Database {
func loadObjects<T>(matching query: Query<T>) -> [T] {
return [] // return an empty array because we only support NSManagedObject types
}
func loadObjects<T: NSManagedObject>(matching query: Query<T>) -> [T] {
let request = NSFetchRequest<T>(entityName: String(describing: T.self))
var objects = [T]()
self.performAndWait {
do {
objects = try self.fetch(request) // a generic fetch request returns an array of the generic type or throws an error
} catch let error {
print("Error fetching: \(error.localizedDescription)")
}
}
return objects
}
}

Swift 4 Using Generics as Return Value

I have a protocol like so:
protocol ModelProtocol{
func parse<T.Model>(data: Data) -> Array<T>? {}
}
The return is an array of option values. The method takes in data, parses it and returns the array of parsed objects from API data.
The type of data that the API returns is called MyData that has an array as the value of the dictionary.
I parse the JSON like so
func parse<T>(data: Data) -> Array<T>? {
do {
let newJSONDecoder = JSONDecoder()
let menu = try newJSONDecoder.decode(MyData.self, from:data)
let dataArray = menu.data //array
let modelArray = Array<T>()
for object in dataArray {
modelArray.append(object)
}
return modelArray
}
catch {
print("error while parsing:\(error.localizedDescription)")
return nil
}
}
I get error on the line where I append into the array to be returned.
Cannot invoke 'append' with an argument list of type '(MyData.Drinks)'
Ultimately I want the returned array to have objects of the type that is in the array MyData.data- in this case, the type is Drinks. But more broadly, I want the method to return any type that is in any JSON payload. The goal is to create a method that can take in any data and return any object as parsed object of type X in the array.
How do I do this?
First of all the code does not compile:
Protocol methods must not have bodies
so you have to remove the braces:
protocol ModelProtocol{
func parse<T : Decodable>(data: Data) -> Array<T>?
}
To solve your problem create MyData also as generic
struct MyData<T : Decodable> : Decodable {
let data : [T]?
}
and declare parse
func parse<T : Decodable>(data: Data) -> Array<T>? {
do {
let newJSONDecoder = JSONDecoder()
let menu = try newJSONDecoder.decode(MyData<T>.self, from:data)
return menu.data
}
catch {
print("error while parsing: ", error)
return nil
}
}
print always the entire error to get detailed information about the decoding error. localizedDescription is too broad.
If data is expected to be non-optional make parse throw and hand over the decoding error
protocol ModelProtocol{
func parse<T : Decodable>(data: Data) throws -> Array<T>
}
struct MyData<T : Decodable> : Decodable {
let data : [T]
}
func parse<T : Decodable>(data: Data) throws -> Array<T> {
let newJSONDecoder = JSONDecoder()
let menu = try newJSONDecoder.decode(MyData<T>.self, from:data)
return menu.data
}

Get all key paths from a struct in Swift 4

Let's say I have that struct:
struct MyStruct {
let x: Bool
let y: Bool
}
In Swift 4 we can now access it's properties with the myStruct[keyPath: \MyStruct.x] interface.
What I need is a way to access all it's key paths, something like:
extension MyStruct {
static func getAllKeyPaths() -> [WritableKeyPath<MyStruct, Bool>] {
return [
\MyStruct.x,
\MyStruct.y
]
}
}
But, obviously, without me having to manually declare every property in an array.
How can I achieve that?
DISCLAIMER:
Please note that the following code is for educational purpose only and it should not be used in a real application, and might contains a lot of bugs/strange behaviors if KeyPath are used this way.
Answer:
I don't know if your question is still relevant today, but the challenge was fun :)
This is actually possible using the mirroring API.
The KeyPath API currently doesn't allow us to initialize a new KeyPath from a string, but it does support dictionary "parsing".
The idea here is to build a dictionary that will describe the struct using the mirroring API, then iterate over the key to build the KeyPath array.
Swift 4.2 playground:
protocol KeyPathListable {
// require empty init as the implementation use the mirroring API, which require
// to be used on an instance. So we need to be able to create a new instance of the
// type.
init()
var _keyPathReadableFormat: [String: Any] { get }
static var allKeyPaths: [KeyPath<Foo, Any?>] { get }
}
extension KeyPathListable {
var _keyPathReadableFormat: [String: Any] {
let mirror = Mirror(reflecting: self)
var description: [String: Any] = [:]
for case let (label?, value) in mirror.children {
description[label] = value
}
return description
}
static var allKeyPaths: [KeyPath<Self, Any?>] {
var keyPaths: [KeyPath<Self, Any?>] = []
let instance = Self()
for (key, _) in instance._keyPathReadableFormat {
keyPaths.append(\Self._keyPathReadableFormat[key])
}
return keyPaths
}
}
struct Foo: KeyPathListable {
var x: Int
var y: Int
}
extension Foo {
// Custom init inside an extension to keep auto generated `init(x:, y:)`
init() {
x = 0
y = 0
}
}
let xKey = Foo.allKeyPaths[0]
let yKey = Foo.allKeyPaths[1]
var foo = Foo(x: 10, y: 20)
let x = foo[keyPath: xKey]!
let y = foo[keyPath: yKey]!
print(x)
print(y)
Note that the printed output is not always in the same order (probably because of the mirroring API, but not so sure about that).
After modifying rraphael's answer I asked about this on the Swift forums.
It is possible, discussion here:
Getting KeyPaths to members automatically using Mirror
Also, the Swift for TensorFlow team has this already built in to Swift for TensorFlow, which may make its way to pure swift:
Dynamic property iteration using key paths
I propose my solution. It has the advantage of dealing correctly with #Published values when using the Combine framework.
For the sake of clarity, it is a simplified version of what I have really. In the full version, I pass some options to the Mirror.allKeyPaths() function to change behaviour ( To enumerate structs and/or classes properties in sub-dictionaries for example ).
The first Mirror extension propose some functions to simplify properties enumeration.
The second extension implements the keyPaths dictionaries creation, replacing
#Published properties by correct name and value
The last part is the KeyPathIterable protocol, that add enumeration
capability to associated object
swift
// MARK: - Convenience extensions
extension String {
/// Returns string without first character
var byRemovingFirstCharacter: String {
guard count > 1 else { return "" }
return String(suffix(count-1))
}
}
// MARK: - Mirror convenience extension
extension Mirror {
/// Iterates through all children
static func forEachProperty(of object: Any, doClosure: (String, Any)->Void) {
for (property, value) in Mirror(reflecting: object).children where property != nil {
doClosure(property!, value)
}
}
/// Executes closure if property named 'property' is found
///
/// Returns true if property was found
#discardableResult static func withProperty(_ property: String, of object: Any, doClosure: (String, Any)->Void) -> Bool {
for (property, value) in Mirror(reflecting: object).children where property == property {
doClosure(property!, value)
return true
}
return false
}
/// Utility function to determine if a value is marked #Published
static func isValuePublished(_ value: Any) -> Bool {
let valueTypeAsString = String(describing: type(of: value))
let prefix = valueTypeAsString.prefix { $0 != "<" }
return prefix == "Published"
}
}
// MARK: - Mirror extension to return any object properties as [Property, Value] dictionary
extension Mirror {
/// Returns objects properties as a dictionary [property: value]
static func allKeyPaths(for object: Any) -> [String: Any] {
var out = [String: Any]()
Mirror.forEachProperty(of: object) { property, value in
// If value is of type Published<Some>, we transform to 'regular' property label and value
if Self.isValuePublished(value) {
Mirror.withProperty("value", of: value) { _, subValue in
out[property.byRemovingFirstCharacter] = subValue
}
} else {
out[property] = value
}
}
return out
}
}
// MARK: - KeyPathIterable protocol
protocol KeyPathIterable {
}
extension KeyPathIterable {
/// Returns all object properties
var allKeyPaths: [String: Any] {
return Mirror.allKeyPaths(for: self)
}
}

Cannot convert value of type [AnyObject] to expected argument type [PFObject] -- Parse & Swift II Error

This is the line that is failing, with error message "Cannot convert value of type [AnyObject] to expected argument type [PFObject]"
self.customObtainAllHandlerWithObjects(objects, success: success, failure: failure)
import UIKit
import Parse
class AbstractService: BaseService {
private let _cache = AbstractCache<T>()
private let _parser = AbstractParser<T>()
/// Store all completionHandlers for convertFromParseObject operations here. Need this to avoid a concurrent conversions of the same object.
var conversions = Dictionary<String, [(entity: T) -> Void]>()
/// Contains ids of objects, which need their videos to be downloaded.
var queue: [(entityId: String, file: PFFile)] = []
var isQueueDownloading = false
var className: String {
get {
fatalError("This property must be overridden")
}
}
// MARK: - Create
func createEntity() -> T {
let entity = T()
cache().appendEntity(entity)
return entity
}
// MARK: - Obtain all
/// Base method, which obtains entities from Parse database.
///
/// - parameter skip: Number of records, which will be skipped.
/// - parameter limit: Max number of entities returned.
/// - parameter constraints: A block, which applies constraints to PFQuery. E.g. query.includeKey("author") or query.whereKey("user", equalTo: PFUser.currentUser()).
/// - parameter success: Success block. Executes when operation successfully finished.
/// - parameter failure: Failure block. Executes when operation fails.
func obtain(skip skip: Int?, limit: Int?, constraints applyConstraints: ((query: PFQuery) -> PFQuery)?, success: (entities: [T]) -> Void, failure: FailureCompletionHandler) {
var query = PFQuery(className: className)
if let skip = skip {
query.skip = skip
}
query.limit = limit ?? 1000
if let applyConstraints = applyConstraints {
query = applyConstraints(query: query)
}
query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
if let error = error {
self.callFailureCompletionHandler(failure, error: error)
}
else {
if let objects = objects {
self.customObtainAllHandlerWithObjects(objects, success: success, failure: failure)
}
else {
failure(allowRetry: false, errorMessage: "Cannot load \(self.className)")
}
}
}
}
You're getting this error because object is of type [AnyObject] but the function you're calling--customObtainHandler--is expecting PFObject so you're going to have to convert your object to a PFObject.
You can convert between types with the as keyword.
so you do something like this:
if let myVar as? PFObject {
// now myVar is a PFObject
}
I don't recommend this but you can also force convert it using:
myVar as! PFObject

How to pass a class type as a function parameter

I have a generic function that calls a web service and serialize the JSON response back to an object.
class func invokeService<T>(service: String, withParams params: Dictionary<String, String>, returningClass: AnyClass, completionHandler handler: ((T) -> ())) {
/* Construct the URL, call the service and parse the response */
}
What I'm trying to accomplish is is the equivalent of this Java code
public <T> T invokeService(final String serviceURLSuffix, final Map<String, String> params,
final Class<T> classTypeToReturn) {
}
Is my method signature for what I'm trying to accomplish correct?
More specifically, is specifying AnyClass as a parameter type the
right thing to do?
When calling the method, I'm passing MyObject.self as the returningClass value, but I get a compilation error "Cannot convert the expression's type '()' to type 'String'"
CastDAO.invokeService("test", withParams: ["test" : "test"], returningClass: CityInfo.self) { cityInfo in /*...*/
}
Edit:
I tried using object_getClass, as mentioned by holex, but now I get:
error: "Type 'CityInfo.Type' does not conform to protocol 'AnyObject'"
What need to be done to conform to the protocol?
class CityInfo : NSObject {
var cityName: String?
var regionCode: String?
var regionName: String?
}
You are approaching it in the wrong way: in Swift, unlike Objective-C, classes have specific types and even have an inheritance hierarchy (that is, if class B inherits from A, then B.Type also inherits from A.Type):
class A {}
class B: A {}
class C {}
// B inherits from A
let object: A = B()
// B.Type also inherits from A.Type
let type: A.Type = B.self
// Error: 'C' is not a subtype of 'A'
let type2: A.Type = C.self
That's why you shouldn't use AnyClass, unless you really want to allow any class. In this case the right type would be T.Type, because it expresses the link between the returningClass parameter and the parameter of the closure.
In fact, using it instead of AnyClass allows the compiler to correctly infer the types in the method call:
class func invokeService<T>(service: String, withParams params: Dictionary<String, String>, returningClass: T.Type, completionHandler handler: ((T) -> ())) {
// The compiler correctly infers that T is the class of the instances of returningClass
handler(returningClass())
}
Now there's the problem of constructing an instance of T to pass to handler: if you try and run the code right now the compiler will complain that T is not constructible with (). And rightfully so: T has to be explicitly constrained to require that it implements a specific initializer.
This can be done with a protocol like the following one:
protocol Initable {
init()
}
class CityInfo : NSObject, Initable {
var cityName: String?
var regionCode: String?
var regionName: String?
// Nothing to change here, CityInfo already implements init()
}
Then you only have to change the generic constraints of invokeService from <T> to <T: Initable>.
Tip
If you get strange errors like "Cannot convert the expression's type '()' to type 'String'", it is often useful to move every argument of the method call to its own variable. It helps narrowing down the code that is causing the error and uncovering type inference issues:
let service = "test"
let params = ["test" : "test"]
let returningClass = CityInfo.self
CastDAO.invokeService(service, withParams: params, returningClass: returningClass) { cityInfo in /*...*/
}
Now there are two possibilities: the error moves to one of the variables (which means that the wrong part is there) or you get a cryptic message like "Cannot convert the expression's type () to type ($T6) -> ($T6) -> $T5".
The cause of the latter error is that the compiler is not able to infer the types of what you wrote. In this case the problem is that T is only used in the parameter of the closure and the closure you passed doesn't indicate any particular type so the compiler doesn't know what type to infer. By changing the type of returningClass to include T you give the compiler a way to determine the generic parameter.
you can get the class of AnyObject via this way:
Swift 3.x
let myClass: AnyClass = type(of: self)
Swift 2.x
let myClass: AnyClass = object_getClass(self)
and you can pass it as paramater later, if you'd like.
I have a similar use case in swift5:
class PlistUtils {
static let shared = PlistUtils()
// write data
func saveItem<T: Encodable>(url: URL, value: T) -> Bool{
let encoder = PropertyListEncoder()
do {
let data = try encoder.encode(value)
try data.write(to: url)
return true
}catch {
print("encode error: \(error)")
return false
}
}
// read data
func loadItem<T: Decodable>(url: URL, type: T.Type) -> Any?{
if let data = try? Data(contentsOf: url) {
let decoder = PropertyListDecoder()
do {
let result = try decoder.decode(type, from: data)
return result
}catch{
print("items decode failed ")
return nil
}
}
return nil
}
}
Simply copy paste each code here into swift file:
# save as: APICaller.swift
import Foundation
struct APICaller
{
public static func get<T: Decodable>(url: String, receiveModel: T.Type, completion:#escaping (Decodable) -> ())
{
send(url: url, json: nil, receiveModel: receiveModel, completion: completion, httpMethod: "GET")
}
public static func post<T: Decodable>(url: String, json: [String: Any]?, receiveModel: T.Type, completion:#escaping (Decodable) -> ())
{
send(url: url, json: nil, receiveModel: receiveModel, completion: completion, httpMethod: "POST")
}
public static func delete<T: Decodable>(url: String, json: [String: Any]?, receiveModel: T.Type, completion:#escaping (Decodable) -> ())
{
send(url: url, json: nil, receiveModel: receiveModel, completion: completion, httpMethod: "DELETE")
}
private static func send<T: Decodable>(url: String, json: [String: Any]?, receiveModel: T.Type, completion:#escaping (Decodable) -> (), httpMethod: String)
{
// create post request
let urlURL: URL = URL(string: url)!
var httpRequest: URLRequest = URLRequest(url: urlURL)
httpRequest.httpMethod = httpMethod
if(json != nil)
{
// serialize map of strings to json object
let jsonData: Data = try! JSONSerialization.data(withJSONObject: json!)
// insert json data to the request
httpRequest.httpBody = jsonData
httpRequest.addValue("application/json", forHTTPHeaderField: "Content-Type")
}
// create an asynchronus task to post the request
let task = URLSession.shared.dataTask(with: httpRequest)
{ jsonData, response, error in
// on callback parse the json into the receiving model object
let receivedModelFilled: Decodable = Bundle.main.decode(receiveModel, from: jsonData!)
// cal the user callback with the constructed object from json
DispatchQueue.main.async {
completion(receivedModelFilled)
}
}
task.resume()
}
}
# save as: TestService.swift
import Foundation
struct TestService: Codable
{
let test: String
}
then you can use it like this:
let urlString: String = "http://localhost/testService" <--- replace with your actual service url
// call the API in post request
APICaller.post(url: urlString, json: ["test": "test"], receiveModel: TestService.self, completion: { testReponse in
// when response is received - do something with it in this callback
let testService: TestService = testReponse as! TestService
print("testService: \(testService)")
})
Tip:
i use online service to turn my JSONs into swift files, so all i have left is to write the call and handle the response
i use this one: https://app.quicktype.io but you can search for the one you prefer
Swift 5
Not exactly the same situation, but I was having similar problem. What finally helped me was this:
func myFunction(_ myType: AnyClass)
{
switch myType
{
case is MyCustomClass.Type:
//...
break
case is MyCustomClassTwo.Type:
//...
break
default: break
}
}
Then you can call it inside an instance of said class like this:
myFunction(type(of: self))
Hope this helps someone in my same situation.
Use obj-getclass:
CastDAO.invokeService("test", withParams: ["test" : "test"], returningClass: obj-getclass(self)) { cityInfo in /*...*/
}
Assuming self is a city info object.
I recently came across this looking for a way to make my UINavigationController invisible to everything but the subview buttons. I put this in a custom nav controller:
// MARK:- UINavigationBar Override
private extension UINavigationBar {
override open func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
// Make the navigation bar ignore interactions unless with a subview button
return self.point(inside: point, with: event, type: UIButton.self)
}
}
// MARK:- Button finding hit test
private extension UIView {
func point<T: UIView>(inside point: CGPoint, with event: UIEvent?, type: T.Type) -> Bool {
guard self.bounds.contains(point) else { return false }
if subviews.contains(where: { $0.point(inside: convert(point, to: $0), with: event, type: type) }) {
return true
}
return self is T
}
}
Don't forget to use bounds instead of frame as point is converted before calling.