2015-09-28 87 views
0

我有一个解析数据库两类 1)“项目”与表“项目Id”和“商店” 2)“链接”与表“项目Id”和“名称”解析关系/合并表<Swift>

我可以创建一个关系,以便获得所选“名称”的“商店”? 或者,也许我可以合并这两个类?

我现在用斯威夫特和我的想法是这样的:

var query:PFQuery = PFQuery(className:"Items") 
query.orderByAscending("Shop") 
query.findObjectsInBackgroundWithBlock { 
(results: [PFObject]!, error: NSError!) -> Void in 
if error == nil && results != nil { 
query.whereKey("ItemId", equalTo:results["ItemId"] as! String) 
} 

回答

0

你必须建立在分析一个指针列这2个班,以便连接到能够抓住这样的数据,从而使一个可能会提到另一个。请注意,A类到B类的指针列会给A类引用B类,但不会给B类引用A类。

对于您的示例:您在链接中创建指针列类指向你的Items类。那么这样做:

// retrieve all objects from Links class 
var query = PFQuery(classname: "Links") 

// the string for the include key is the name of your pointer column 
// this allows you to get items from the class that the pointer column points to 
query.includekey("PointerToItemClass") 
query.findObjectsInBackgroundWithBlock { 
     (results: [AnyObject]?, error: NSError!) -> Void in 
     if error == nil && results != nil { 

     for x in results! { 

     // this line of code reaches into the Items class and gets 
     // the objects under the "Shop" column 
     self.yourStringArray.append(x.objectForKey("PointerToItemClass")!.objectForKey("Shop") as! String) 

     } 


} 

重新编辑,我要补充的是yourStringArray是一个字符串的空数组,你应该初始化你的类的顶部,就像这样:

var yourStringArray = [String]() 

这将允许你存放从解析数据库获得的项目的地方

+0

因此,后来当我在类A中查询并获取pfobject时,是否可以访问类B指针而不必查询整个类? – PoolHallJunkie

+0

你在寻找的是一个PFObject,对于结果中的x!不是一个字符串... – PoolHallJunkie