LoginSignup
22
22

More than 5 years have passed since last update.

Realm(Objective-C版をSwiftで使う)からRealmSwiftへの移行

Last updated at Posted at 2015-05-18

メモです。RealmSwiftはiOS8以上でないと使えません。

// Realm
Tweet.allObjects() 
// RealmSwift
Realm().objects(Tweet)

// Realm
Tweet(forPrimaryKey: tweetId)
// RealmSwift
Realm().objectForPrimaryKey(Tweet.self, key: tweetId)

// Realm
realm.addOrUpdateObject(tweet)
// RealmSwift
realm.add(tweet, update: true)

// Realm
user.tweets.removeAllObjects()
// RealmSwift
realm.delete(user.tweets)

// Realm
user.tweets.addObject(newTweet)
// RealmSwift
user.tweets.append(newTweet) 

// Realm
if Int(tweets.indexOfObject(tweet)) == NSNotFound
// RealmSwift
tweets.indexOf(tweet) == nil

// Realm
realmToken = realm.addNotificationBlock { (note :String!, realm: RLMRealm!) -> Void in
    self.reloadData()
}
// RealmSwift
realmToken = realm.addNotificationBlock { notification, realm in
    self.reloadData()
}
22
22
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
22
22