2017-01-05 30 views
0

我开始在iOS上编程,我来自ASP.NET MVC。 我试图找到绑定的模式,以数据库的方式,或至少相似的实体框架模型绑定的东西,我想建立两个模型,如下所示:Swift Sqlite模型绑定

class A{ 
let id = Expression<Int64>("id") 
let name = Expression<String?>("name") 
} 

class B{ 
let id = Expression<Int64>("id") 
let name = Expression<A>("A") 
} 

我想知道,如果这是可能的或可能有人指向我正确的方向,当我取数据我想填补我的模型从sqlite

在此先感谢 和抱歉,如果我不清楚我的问题。

+1

你可能会考虑核心数据(见[什么是核心数据](https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/CoreData/index.html)),这可能是比EF直接使用SQLite更好的类比。您仍然将数据存储在SQLite中,但您正在使用模型对象,而不是直接与数据库连接。这不是一个完美的模拟EF,但它更接近... – Rob

+0

谢谢,我会研究它我认为这可能是我正在寻找的东西 –

回答

1

GRDB.swift SQLite库侧重于从数据库帮助应用程序加载模型(又名记录):https://github.com/groue/GRDB.swift#records

也许这只是你在找什么:

let persons = try Person.fetchAll(db) // [Person] 
for luckyPerson in try Person.filter(isLucky).order(name).fetchAll(db) { person in 
    print(person.name) 
} 
if let person = try Person.fetchOne(db, key: 1) { 
    print(person.name) 
} 
+0

谢谢,这也会帮助我 –