Converting RealmSwift to Xcode9 / Swift 4

crea
crea
Sep 13, 2017 · 2 min read
environmentXcode:    9.0 GM (9A235)
Swift: 4.0
Carthage: 0.25.0
(tried on 2017/9/12)

In your existing xcode project, you might see this error if you convert swift version to 4.

Image for post
Image for post

Usually I must be able to use it through Carthage, but now I couldn’t.
It seems the problem is RealmSwift doesn’t take measures against Swift 4.

So let me share how I fixed it.

Thanks in advance

https://stackoverflow.com/questions/44640852/how-can-i-use-realm-with-swift-4
https://ja.stackoverflow.com/questions/28951/realm%E3%81%8Cbuild-%E3%81%A7%E3%81%8D%E3%81%AA%E3%81%84 (Japanese)

Solution

Before that, confirm the xcodebuild version.

$ sudo xcode-select -s /Applications/Xcode9.app
$ xcodebuild -version
Xcode 9.0
Build version 9A235
  1. Change swift version to 4

select TARGETS then move to Build Settings > Swift Language Version.
Choose Swift 4.

Image for post
Image for post

2. Build RealmSwift by yourself

a. For now, download RealmSwift manually

$ git clone --recursive https://github.com/realm/realm-cocoa.git
$ cd realm-cocoa
$ git checkout v2.10.0

b. fetch submodule

# at root of realm-cocoa directory
$ git submodule update --init

c. build using script, passing argument by devices you want

# build will fail if you forget previous step
$ REALM_SWIFT_VERSION=4.0 sh build.sh ios-swift

It takes time.

※ Or you can choose the other devices.

$ REALM_SWIFT_VERSION=4.0 sh build.sh osx-swift
$ REALM_SWIFT_VERSION=4.0 sh build.sh watchos-swift
$ REALM_SWIFT_VERSION=4.0 sh build.sh tvos-swift

d. Gotcha! binaries will be produced in the directory realm-cocoa/build/ios/swift-4.0.

Image for post
Image for post

You only have to replace the old frameworks with the new stuffs.

Well done! Mission completed.

That’s it!

In my private projects, Conversion to Swift 4 was easy (I only have to fix NSAttributedStringKey and add @objc annotation before keyword “dynamic”. Xcode9 taught me everything I should do.)

Thanks for reading! Happy coding.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store