I am having some trouble putting entries from one attribute into an Array. This Array will then be used in a UIPickerview. Language is Swift.
There are many tutorials on Pickerviews in swift and Core Data in swift. But I haven't found one that explains how to fill an array with just one of the attributes.
To make matters worse I am still pretty bad at converting objective c into swift.
So I have two questions:
1 is my solution correct to build an array from one attribute and then fill the Pickerview with the content from that array?
2 how do you get that array in swift?
objective c code on core data => array
Objective C from link:
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]
initWithEntityName:@"WeightLog"];
self.contactarray = [[managedObjectContext executeFetchRequest:fetchRequest
error:nil] mutableCopy];
titleNames = [[NSMutableArray alloc] init];
for (int i =0; i<=self.contactarray.count; i++) {
NSManagedObject *device = [self.contactarray objectAtIndex:i];
[titleNames addObject:device];
}
The part I can't write in Swift:
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]
initWithEntityName:@"WeightLog"];
self.contactarray = [[managedObjectContext executeFetchRequest:fetchRequest
error:nil] mutableCopy];
Thank you!
Edit: Working code!
var myTitle: String = ""
var appDel:AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
var context:NSManagedObjectContext = appDel.managedObjectContext!
let fetchRequest = NSFetchRequest(entityName:"MyEntity")
var myTitles: [String] = []
if let myLogs = context.executeFetchRequest(fetchRequest, error: nil) {
myTitles = myLogs.map { $0.myTitle } // get an array of the 'myTitle' attributes
println(myTitles)
I'm just typing this in here, so not sure if it even compiles:
let moc = self.managedObjectContext()
let fetchRequest = NSFetchRequest(entityName:"WeightLog")
var titleNames: [String] = []
if let weightLogs = moc.executeFetchRequest(fetchRequest, error: nil) as? [WeightLog] {
titleNames = weightLogs.map { $0.title } // get an array of the 'title' attributes
}
If you're returning the titleNames
array from a method then I'd write it like this, ie not use a var titleNames
:
let moc = self.managedObjectContext()
let fetchRequest = NSFetchRequest(entityName:"WeightLog")
if let weightLogs = moc.executeFetchRequest(fetchRequest, error: nil) as? [WeightLog] {
return weightLogs.map { $0.title } // get an array of the 'title' attributes
} else {
return []
}
lazy var managedObjectContext : NSManagedObjectContext? = {
let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
if let managedObjectContext = appDelegate.managedObjectContext
{
return managedObjectContext
}
else
{
return nil
}
}()
let fetchRequest = NSFetchRequest(entityName: "Entity")
if let fetchResults = managedObjectContext!.executeFetchRequest(fetchRequest, error: nil) as? [Entity]
{
for info in fetchResults
{
println(info.valueForKey("keyname"));
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.