0

I'm banging my head over here

. I'm trying to solve this "easy" to understand issue. I have an API Endpoint which i'm sending to request from many different classes. Let's assume I have a Tweet class and User class.

class Tweet {

    var id: String!
    var description: String!

    required init() { }

 }

class User {

    var id: String!
    var username: String!
    var likes : Int!

    required init() { }

 }

Now, Depending on the Class request origin, I'll get a respond with an Array that each element holds the exact same properties as the source Class as JSON. Sample request from Tweet Class:

  "data":
  [{
     {
     "id": "492",
     "description": "HELP!"
     },
     {
     "id": "574",
     "description": "ME!"
     }
   ]}

Now, I'm trying to create a Generic Method which automatically parse those into an Array with objects from the passes Class type. This is what i've tried

func sendRequest<T>(resource: T.Type,withProperyList propList : [String], completion: @escaping ([T]?) -> ()) { 


// Create NSURLSession request, fetching data.. converting to json..

guard let mainArray = json["data"] as? NSArray else { return }

for element in mainArray  {

      let tweet = T() // **Error here**

 }

Error: ** 'T' cannot be constructed because it has no accessible initializers

How can I access my Generic Type Class in a generic way? Meaning that i'll pass the Class type and be able to apply it to 'T'. I've tried passing it theu the input parameters but it won't help. I just can't figure it out. Any ideas?

Roi Mulia
  • 5,626
  • 11
  • 54
  • 105
  • Wait for Swift 4 and its built-in JSON en-/decoder. Since your classes contain different properties it's not possible with generics anyway. – vadian Jul 09 '17 at 09:09
  • Hey @vadian. Thank you for responding. Do you happend to know when is the official release date? – Roi Mulia Jul 09 '17 at 09:15
  • No, but usually in September or October – vadian Jul 09 '17 at 09:22
  • @vadian Cool. Hey vadian, I know it's not appropriated, but i wondered if there any chance we can open a quick chat for a question i've got in Swift. It's about storing and retrieving arrays with big amount of objects. Totally understand if you'll refuse :) – Roi Mulia Jul 09 '17 at 09:24
  • Please ask a question. – vadian Jul 09 '17 at 09:25
  • @vadian Sorry for my ignorance, did you mean by opening a thread or by asking here at the comments section? – Roi Mulia Jul 09 '17 at 09:26
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/148718/discussion-between-roi-mulia-and-vadian). – Roi Mulia Jul 09 '17 at 09:26
  • I mean [please ask a new question](https://stackoverflow.com/questions/ask) – vadian Jul 09 '17 at 09:27
  • hey @vadian , posted :) https://stackoverflow.com/questions/44995028/swift-performance-wise-comparing-two-arrays-and-get-the-difference-in-each-a . Hope you'll be able to assist me! Thank you sir! – Roi Mulia Jul 09 '17 at 09:43

1 Answers1

0

Instead of T(), give this a shot.

let tweet = resource.init()
Shamas S
  • 7,507
  • 10
  • 46
  • 58