在Realm Swift中查询一对多关系中的项目

时间:2017-04-09 14:45:32

标签: swift realm one-to-many

如何根据以下代码查询列表中的所有项目?

我想要的是能够说出来,给我所有属于import Foundation import RealmSwift class Item:Object { dynamic var productName: String = "" } 等的项目。

购物清单型号

Lists: Results<ShoppingList> (
    [0] ShoppingList {
        listName = List 1;
        itemList = RLMArray <0x6180000feb80> (
            [0] Item {
                productName = Jitomates;
            },
            [1] Item {
                productName = Grapes;
            },
            [2] Item {
                productName = Oranges;
            }
        );
    },
    [1] ShoppingList {
        listName = List 2;
        itemList = RLMArray <0x6180000fec80> (
            [0] Item {
                productName = Tomatoes;
            },
            [1] Item {
                productName = Grapes;
            },
            [2] Item {
                productName = Oranges;
            },
            [3] Item {
                productName = Green Peppers;
            },
            [4] Item {
                productName = Apples;
            }
        );
    }
)

项目模型

Results

所有购物清单的输出( realm.objects(ShoppingList.self)

List

编辑:

以下查询接近我正在寻找的内容,除了它输出let itemsFromList2 = realm.objects(ShoppingList.self).filter("listName = 'List 2' "),我只需要一个Items: Results<ShoppingList> ( [0] ShoppingList { listName = List 2; itemList = RLMArray <0x6000000e3500> ( [0] Item { productName = Tomatoes; }, [1] Item { productName = Grapes; }, [2] Item { productName = Oranges; }, [3] Item { productName = Green Pappers; }, [4] Item { productName = Apples; } ); } ) 包含列表2中的所有项目

function link_events() { document.getElementById("calculate").onclick = calculate; } function calculate() { var years = document.getElementById('years'); var population = parseInt(document.getElementById('population').value); var rate = parseInt(documet.getElementById('rate').value); var table = '' + '<table>' + '<tr>' + '<th><strong>Year</strong></th>' + '<th><strong>Population</strong></th>' + '<th><strong>Change</strong></th>' + '</tr>' + ''; var currentYear = 2017; for (var i = currentYear; i < (currentYear+years); i++) { var change = (population*rate)/100; population += change; table += '' + '<tr>' + '<td>' + i + '</td>' + '<td>' + population.toFixed() + '</td>' + '<td>' + change.toFixed() + '</td>' + '</tr>' + ''; } table += '' + '</table>' + ''; document.getElementById('result').innerHTML = table; }

<!doctype html>  
<META HTTPEQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">  
<meta httpequiv="expires" content="0" />  
<html lang="en">  
<head>  
  <title> hw8 </title>  
  <link rel="stylesheet" href="hw8.css">  
  <script src="hw8.js"></script> 
</head>  
<body>  

<form name="inputform"> 
<div id="input">  

    <h2> Population Growth </h2>  
    Years to forecast: <input type="text" value="<? print $_POST['years']; ?>" name="years"> <br/> <br/>  
    Current Population: <input type="text" value="<? print $_POST['population']; ?>" name="population"> <br/> <br/>  
    Growth Rate: <input type="text" value="<? print $_POST['rate']; ?>" name="rate"> <br/> <br/>  
    <div id="button"> <input type="submit" value="Calculate" id="calculate"> </div> 

</div>  
</form> 

<div id="result"> </div>


</body> 
</html>

1 个答案:

答案 0 :(得分:1)

您从领域中获取Results,然后从Results获取领域对象。 我希望下面的代码可以帮助你。

let itemsFromList2 = realm.objects(ShoppingList.self).filter("listName = 'List 2'")
// Results acts like an Array
let shoppingList2 = itemsFromList2.first!
// itemList is what you need I think
let itemList = shoppingList2.itemList