我正在使用核心數據,當我嘗試讀取我的屬性時,它們被讀作數字,如0和1.這是我的核心數據建立。我的最終目標是將布爾值保存爲true或false,然後閱讀它。我在我的設置中做錯了什麼,或者錯過了什麼?我看過帖子 iPhone: Save boolean into Core Data除了在OBJ C,我相信布爾值在Datamodel選項中現在可用。我正在使用核心數據,我的布爾值正在被讀作
func saveFilters(bathAny: Bool, bath1: Bool, bath2: Bool, bath3: Bool, bath4: Bool){
let context = getContext()
let entity = NSEntityDescription.entity(forEntityName: "FilterCoreDataObj", in: context)
let transc = NSManagedObject(entity: entity!, insertInto: context)
//SET ENTITY VALUES
transc.setValue(bathAny, forKey: "bathAny")
transc.setValue(bath1, forKey: "bath1")
transc.setValue(bath2, forKey: "bath2")
transc.setValue(bath3, forKey: "bath3")
transc.setValue(bath4, forKey: "bath4")
do {
try context.save()
print("saved")
} catch let error as NSError {
print("could not save \(error), \(error.userInfo)")
} catch {
}
}
讀Func鍵
func getTranscriptions(bathAny: Bool, bath1: Bool, bath2: Bool, bath3: Bool, bath4: Bool) {
let fetchRequest: NSFetchRequest<FilterCoreDataObj> = FilterCoreDataObj.fetchRequest()
do {
let searchResults = try getContext().fetch(fetchRequest)
print("num of results = \(searchResults.count)")
for trans in searchResults {
print("bath 1 \(trans.value(forKey: "bath1"))")
}
} catch {
print("Error with request: \(error)")
}
}
可能重複的[iPhone:保存布爾到核心數據](http://stackoverflow.com/questions/2937945/iphone-save-boolean-into-core-data) – keithbhunter
嗯,這是迅速,核心數據已經改變了在Swift 3中很多。它似乎在datModel中,它可以讓你設置一個屬性爲一個布爾值。 –