Unity3D for iOS: ParseInstance.GET(string) throws “InvalidCastException: Cannot cast from source type to destination type” for Date objects from Parse

The Error

//UniParse boiler plate:
var pObj = new ParseClass("/classes/Tournaments");			
string query = "...your query here..."; 	
ParseInstanceCollection MyParseList = pObj.List( query );			
while(!MyParseList.isDone) yield return null;					
ParseInstance[] items = MyParseList.items; 		

//get custom Date from a Parse.com Class (database table)
string MyDateFromParse = items[0].Get<string>("MyCustomDate"); 

The last line above throws this error because MyCustomDate is of type “date” in the Parse class:

/*
InvalidCastException: Cannot cast from source type to destination type.
ParseInstance.Get[String] (System.String key) (at Assets/Plugins/ParseClass.cs:238)
MyManager+<getMyCustomDateFromParse>c__Iterator15.MoveNext () (at Assets/Scripts/MyManager.cs:9)				
*/

The Solution

Read up on Data Types in Parse.com’s documentation.

Hashtable d = (Hashtable)items[0].Get<object>("MyCustomDate");
string MyDateFromParse = d["iso"];
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s