2015-09-28 42 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