Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of unresolved identifier 'Realm'

I am new to both Realm and iOS development and I am stuck due to this error. I downloaded the latest version of Realm i.e. 0.98.0 and followed the steps mentioned in the Getting Started section

  1. Download the latest release of Realm and extract the zip.
  2. Go to your Xcode project’s “General” settings. Drag RealmSwift.framework and Realm.framework from the ios/swift-2.1.1/, watchos/, tvos/ or osx/swift-2.1.1/ directory to the “Embedded Binaries” section. Make sure Copy items if needed is selected and click Finish.
  3. In your unit test target’s “Build Settings”, add the parent path to RealmSwift.framework in the “Framework Search Paths” section.
  4. If using Realm in an iOS, watchOS or tvOS project, create a new “Run Script Phase” in your app’s target’s “Build Phases” and paste the following snippet in the script text field:

    bash "${BUILT_PRODUCTS_DIR}/${FRAMEWORKS_FOLDER_PATH}/Realm.framework/strip-frameworks.sh"

As mentioned in the answer to the following question I added $(PROJECT_DIR) in the Framework Search Paths

How to add the parent path to RealmSwift.framework in the “Framework Search Paths” section?

After that I created a class called Dog and added the following code in AppDelegate.swift

let myDog = Dog()
myDog.name = "Rex"
myDog.age = 1
print("name of dog: \(myDog.name)")

// Get the default Realm
let realm = try! Realm()

// Persist your data easily
try! realm.write {
  realm.add(myDog)
}

When I try to build the project I get an error "Use of unresolved identifier 'Realm'" on the following line :

let realm = try! Realm()

I have tried creating new projects and carefully following the above steps but I still get the error. For Step 2 I tried added the framework with both the "Create groups" and "Create folder references" options while keeping "Copy items if needed" checkbox ticked.

I am using XCode 7.2.1, OS 10.11.13 and Swift 2.1.1. I do not have Cocoapods installed at the moment

Am I missing some step somewhere? Any help would be really appreciated.

like image 443
Joyson Avatar asked Feb 09 '16 08:02

Joyson


1 Answers

The issue was the missing import statement. The examples folder helped in solving the issue.

import RealmSwift

Having worked with Java and the auto import feature in Eclipse this took a little getting used to.

like image 183
Joyson Avatar answered Nov 15 '22 07:11

Joyson