//--------------------------增
获取Friend的实体对象
varpeople = [NSManagedObject]()
varentity =NSEntityDescription.entityForName("FriendGroup", inManagedObjectContext:self.managedObjectContext!)
varfriendgroup1 =FriendGroup(entity: entity!, insertIntoManagedObjectContext:self.managedObjectContext!)
//给friend赋值
friendgroup1.groupid=111
friendgroup1.id=1
friendgroup1.uid=2015
friendgroup1.groupname="我的好友"
var error1:NSError?
if self.managedObjectContext!.save(&error1) {
println("插入成功")
}else{
}
//people.append(friendgroup1)
varfriendgroup2 =FriendGroup(entity: entity!, insertIntoManagedObjectContext:self.managedObjectContext!)
//给friend赋值
friendgroup2.groupid=222
friendgroup2.id=2
friendgroup2.uid=2014
friendgroup2.groupname="我的同事"
var error2:NSError?
if self.managedObjectContext!.save(&error2) {
people.append(friendgroup2)
//--------------------------查
//通过实体名获取请求
letfetchRequest =NSFetchRequest(entityName:"FriendGroup")
//定义分组和排序规则
varsortDescriptor =NSSortDescriptor(key:"uid", ascending:true)
fetchRequest.sortDescriptors= [sortDescriptor]
letfetchedRequests =self.managedObjectContext?.executeFetchRequest(fetchRequest, error:nil)as? [FriendGroup]
ifletresult = fetchedRequests{
forone:FriendGroupinresult{
println(one.description)
}
}
println("插入成功")
}else{
}