siegesmund / SwiftDDP

A Meteor client, written in Swift
MIT License
145 stars 60 forks source link

MeteorCollection #6

Closed Sadmansamee closed 8 years ago

Sadmansamee commented 8 years ago

hey @siegesmund I'm getting two issue when trying to follow the dictionary way (Simple in-memory persistence)

below

  1. Cannot assign value of type MeteorCollection<.MeteorDocument.> to type 'MeteorCollection'
  2. Generic parameter 'T' could not be inferred when using on table view functions like "collection.count"
Sadmansamee commented 8 years ago

Issue solved On https://github.com/siegesmund/SwiftDDP/blob/master/Examples/Dictionary/SwiftTodos/Lists.swift

var collection:MeteorCollection = (UIApplication.sharedApplication().delegate as! AppDelegate).lists

I followed this way then I got that problem there was type mismatch... my mistake when removed ":MeteorCollection" it worked fine...

siegesmund commented 8 years ago

Glad you solved it!

Sadmansamee commented 8 years ago

thanks...but facing another issue like the collection list it not initilized even after successful subscription. Like I declare a variable (a class subclassed from MeteorDocument) on appdelegate and use it on other VC and subscribe there just like as you demontrated but then later found out that value is not getting populated , do I need explicitly populate any how?

siegesmund commented 8 years ago

You must create a collection that will contain the documents, not a document. Here is an example, from the documentation:

class List: MeteorDocument {

    var collection:String = "lists"
    var name:String?
    var userId:String?

}

let lists = MeteorCollection<List>(name: "lists")   // As with Meteorjs, the name is the name of the server-side collection  
Meteor.subscribe("lists") 

You might declare the collection in your AppDelegate. It doesn't make any sense to declare a document there.

siegesmund commented 8 years ago

By the way, per your original question: you're running into the basics of Swift's strong typing.

You can write it as:

var collection:MeteorCollection = (UIApplication.sharedApplication().delegate as! AppDelegate).lists

or, more commonly, you can cast it:

var collection = (UIApplication.sharedApplication().delegate as! AppDelegate).lists as! MeteorCollection

This is something that you will run into repeatedly in Swift.

Sadmansamee commented 8 years ago

actually that was not my question but I'm clear now.

siegesmund commented 8 years ago

Great!