Group Group Group Group Group Group Group Group Group

raywenderlich.com Forums

Video Tutorial: Intermediate Realm on iOS Part 3: Encrypted Realm Files

Learn how to use encrypted Realm files to safely store sensitive data.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/3245-intermediate-realm-on-ios/lessons/4

You forgot to include Challenge PDF again

Already alerted the team

Great Realm tutorial, Marin - I’m really enjoying it!

Fwiw, to get the starter project to build and run on Xcode 8.2.1 and Swift 3, I had to do a few things:

  1. Allow Xcode to ‘Convert to current Swift syntax,’ and then make a couple of corrections beyond what Xcode was able to do. No big deal, to be expected with each new version of Xcode.
  2. Use the Realm Browser to open up and upgrade the two bundled Realm files, static.realm and default_v1.0.realm, to the latest Realm format. Without doing this, something in the Realm lib crashes when either RealmConfig.static or RealmConfig.main is accessed. My pod install got me RealmSwift v2.4.2; I suppose there’s some incompatibility with the .realm files that were bundled with the starter project?
  3. In SubjectsViewController:viewDidLoad, wrap the results from mainRealm.objects(Exam.self).map{$0.name} inside an Array() initializer, like this: Array(mainRealm.objects(Exam.self).map{$0.name}). Without that, when the line .filter("NOT name IN %@", examNames) is run, there’s a crash stating that the NSPredicate expects an array. I’m new to .map{}, and thought it was supposed to return an Array with just the mapped $0.name value, so I’m not sure why this is necessary.

Anyhow, I know this video series is coming up on being a year old; just wanted to post this info in case it is helpful to anyone else working through the videos with the latest Xcode 8.2.1 and Swift 3.

1 Like

While I’m at it, here’s an implementation of the SHA512 String extension property that works with Xcode 8.2.1 & Swift 3. There’s probably a better way to do it, but this does the job so that you can continue on with the encrypted Realm tutorial.

var sha512Data: Data
{
	var digest = [UInt8](repeating: 0, count: Int(CC_SHA512_DIGEST_LENGTH))
	if let srcData = self.data(using: .utf8)
	{
		_ = srcData.withUnsafeBytes { (srcU8Ptr: UnsafePointer<UInt8>) in
			
			CC_SHA512(srcU8Ptr, CC_LONG(srcData.count), &digest)
		}
	}

	return Data(buffer: UnsafeBufferPointer<UInt8>(start: digest, count: digest.count))
}