我想获取 sqlite 表中所有保存的数据。
我目前正在这样做:
func GetAllData() -> NSArray
{
var error : NSError? = nil;
var request : NSFetchRequest = NSFetchRequest(entityName: "Locations");
let result : [AnyObject] = managedObjectContext!.executeFetchRequest(request, error:&error)!;
var elements : NSMutableArray = NSMutableArray();
for fetchedObject in result
{
elements.addObject(fetchedObject[0]);
}
print(elements);
return elements;
}
我在 Objective-C 中获取数据没有问题,但很快我就搞不懂了!
数据保存正常。我有两行“名称”和“类别”。如何显示所有保存的数据?
最佳答案
您应该将所有对象从 CoreData 加载到 NSManaged 对象的数组/字典中。
例如:
var locations = [Locations]() // Where Locations = your NSManaged Class
var fetchRequest = NSFetchRequest(entityName: "Locations")
locations = context.executeFetchRequest(fetchRequest, error: nil) as [Locations]
// Then you can use your properties.
for location in locations {
print(location.name)
}
关于ios - Swift:获取 CoreData 作为数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26524510/