48
48

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

SwiftでCoreDataを使うときのメモ

Last updated at Posted at 2014-06-26

いろいろ記述に迷ったのでメモメモ

// CoreDataの取得
var appDel:AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
var context:NSManagedObjectContext = appDel.managedObjectContext

var request = NSFetchRequest(entityName: "Setting")
request.returnsObjectsAsFaults = false;
request.predicate = NSPredicate(format: "key = %@", "myid")
var results = context.executeFetchRequest(request, error:nil)

if(results.count > 0){
    // データがあった場合
    var userdata : AnyObject = results[0]
    var uid = userdata.valueForKey("value") as String
    println(uid)
}else{
    // データが無かった場合
    var Setting = NSEntityDescription.insertNewObjectForEntityForName("Setting",inManagedObjectContext:context) as NSManagedObject
    Setting.setValue("myid",forKey:"key")
    Setting.setValue("dsdsdsssdsd",forKey:"value")
    context.save(nil)
}
48
48
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
48
48

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?