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
Download the latest release of Realm and extract the zip.
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.
In your unit test target’s “Build Settings”, add the parent path to RealmSwift.framework in the “Framework Search Paths” section.
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.
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.
Related
Recently, I was looking to begin an iOS project that uses the DataStore service of AWS Amplify. To get familiar, I followed the 'Getting Started' documentation found at https://sandbox.amplifyapp.com/start#datastore and accepted all the defaults to create a simple Blog application.
When I was in the 'Test' stage and on the last part of step 4, my Xcode project was failing to find 'AmplifyModels' even though they were generated successfully and exist in the /amplify/generated/models folder.
This is the code
import SwiftUI
import Amplify
import AmplifyPlugins
#main
struct BlogApp: App {
public init() {
let dataStorePlugin = AWSDataStorePlugin(modelRegistration: AmplifyModels())
//let apiPlugin = AWSAPIPlugin(modelRegistration: AmplifyModels()) // UNCOMMENT this line once backend is deployed
do {
try Amplify.add(plugin: dataStorePlugin)
//try Amplify.add(plugin: apiPlugin) // UNCOMMENT this line once backend is deployed
try Amplify.configure()
print("Initialized Amplify");
} catch {
print("Could not initialize Amplify: \(error)")
}
}
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
This is the error
Cannot find 'AmplifyModels' in scope
Steps taken to try and resolve the error
Clean and build Xcode project, along with quitting and restarting Xcode
Manually adding the files to the project in Xcode. The code was able to find the files only if I removed them from the 'amplify' folder and placed them in the same folder as BlogApp and ContentView, but that cannot be correct I imagine.
Cleared the DerivedData for the project and rebuilt.
I am running on Xcode 12.2. The reason that I decided to follow the 'Getting Started' documentation step by step is because I was failing with this same error for my own project I was creating, so I wanted to make sure that it was not some simple user error on my part.
Thank you in advance!
The instructions leave out one small step. You need to add the generated source files to the project so they'll get compiled.
pick menu "File > Add Files to ..."
select the amplify folder, and pick the 'Create groups' option
Im working on an iOS project, which shows the customer number in a barcode. I had installed the framework ZXingObjC with CocoaPods, described in GitHub.
I can compile my Project without errors. I can also use the classes of ZXingObjC in my Objective-C classes, without errors. After than, I have added the import Command #import <ZXingObjC/ZXingObjC.h> to my bridging header file, like my other custom objective-c classes, without compile errors. (I had testet the header file by destroying some import statements and got the expected file not found exception.)
But now, I can't use any class of ZXingObjC in my swift classes. I only got the following compile error: Use of undeclared type '...'. The Xcode autocomplete is not working, too.
e.g.
var test : ZXMultiFormatWriter?
>> Use of undeclared type 'ZXMultiFormatWriter'
I tried:
setup new project, same issue
checked header search path: $(SRCROOT)/Pods/Headers/Public/Adjust
reinstalled the ZXingObjC framework
checked build settings: Enable Modules: YES
checked build settings: Other Linker Flags: $(inherited) -ObjC
-framework "ZXingObjC"
checked linked binaries in the build phases: framework is added
checked import statement in the bridging header file (#import
<ZXingObjC/ZXingObjC.h> and #import "ZXingObjC/ZXingObjC.h" -- no
difference)
Windows style: restarting Xcode and Mac ;-)
I'm using:
Xcode: 6.3.2
CocoaPods: 0.37.2
Project Deployment target: iOS 8.0
SDK: 8.3
Does anyone know the problem? Can anyone help?
How can I make the ZXingObjC framework available in swift?
Actually it is an easy issue:
Podfile
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!
pod 'ZXingObjC', '~> 3.1'
So, on terminal:
cd workspace
pod install
Then, once opened project on Xcode, you have to edit bridging-header adding ZXingObj:
#import <ZXingObjC/ZXingObjC.h>
Finally, in your swift classes that uses ZXingObjC, you have to import ZXingObjC.
import ZXingObjC
class ZXingObjCWrapper {
func encode() {
let writer = ZXMultiFormatWriter.writer()
....
}
}
The rest of the code for when you need to set an UIImage with this bar code:
func generateDataMatrixQRCode(from string: String) -> UIImage? {
do {
let writer = ZXMultiFormatWriter()
let hints = ZXEncodeHints() as ZXEncodeHints
let result = try writer.encode(string, format: kBarcodeFormatDataMatrix, width: 1000, height: 1000, hints: hints)
if let imageRef = ZXImage.init(matrix: result) {
if let image = imageRef.cgimage {
return UIImage.init(cgImage: image)
}
}
}
catch {
print(error)
}
return nil
}
The header search path was not correct in my project. The right values are:
$(inherited)
"${PODS_ROOT}/Headers/Public"
"${PODS_ROOT}/Headers/Public/ZXingObjC"
The second and third lines were not added by installation with CocoaPods.
EDIT: The installed framework have to be added to "Embedded Binaries" in General tab of the project.
I tried everything on this page to add ZXingObjC as a Pod. My goal was to generate an Aztec barcode.
I checked my Header Search Path. As Reddas said, I had to manually add "${PODS_ROOT}/Headers/Public/ZXingObjC". I also added ZXingObjC as an Embedded Binary (in the General Tab).
I checked my bridging file & all was good. I checked my view controllers where I wanted to generate the barcode. The import ZXingObjC was there.
No compile errors. But I can't declare a variable of ZXingObjC.
No luck. Any more suggestions?
EDIT - I went into the Targets, Build Settings and searched for Header Search Paths. I added in BOTH "${PODS_ROOT}/Headers/Public/ZXingObjC" and "${PODS_ROOT}/Headers/Private/ZXingObjC"
This seemed to unclog whatever broke. It works now. Strangely, I can now even delete those entries and it works.
My team has a few full Obj-C libraries we like te reuse in our projects. To do so, we usually set up a git submodule, and then add it to the xcode project as a sub-project (Using target dependency, link binary with library and updating the User Header Search Paths)
So far it's only been done in full Obj-C projects, and I'm now trying to use it in a Swift project but with very little success so far. I tried to add the briding-header file, referenced it in the project and filled it like so :
#import "MyLibraryHeader.h"
With the target header being in the User Header Search Paths.
It lets me build, but when using it in my Swift files:
let test = MyLib();
let secondTest = MyLib.classMethod1("some_arguments");
I get an EXC_BAD_INSTRUCTION on secondTest, and the following logs in the debugger:
(lldb) po test
error: <EXPR>:1:1: error: use of unresolved identifier 'test'
(lldb) po secondTest
error: Error in auto-import:
failed to get module 'MyProject' from AST context:
/Users/siegele/Sources/MyProject_iOS/MyProject/Classes/MyProject-Bridging-Header.h:12:9: error: 'MyLibraryHeader.h' file not found
#import "MyLibraryHeader.h"
^
failed to import bridging header '/Users/siegele/Sources/MyProject_iOS/MyProject/Classes/MyProject-Bridging-Header.h'
Found the following question with no answer : Xcode 6 crashing when using Objective-C subproject inside Swift
Any help would be appreciated.
I followed the HockeyApp tutorial that can be found here:
http://support.hockeyapp.net/kb/client-integration-ios-mac-os-x/integrate-hockeyapp-for-ios-as-a-subproject
In the end, I was on the right tracks but messed up the Header Search Paths:
Add subproject xcodeproj to the workspace
On the main project : Link binary with library, and add the subproject product library (Bonus points : add it as a target dependency too)
Update Header Search Paths (not User Header Search Paths) accordingly
Import your Library.h in the main project Bridging-Header.h file
What threw me off for a while is that the Swift Compiler setting for the Objective-C Bridging Header was not set automatically. Check the Build Settings for your target and ensure the "Objective-C Bridging Header" setting is not blank.
I am trying to add Realm to my app written in swift. I have followed the tutorial and I can't seem to get it to work. The biggest problem is that when I try to import Realm I get No such module 'Realm' I don't know what else to try. You can see my efforts below.
You can see the instructions here: http://realm.io/docs/cocoa/0.85.0/#swft
I have also copied the instructions below:
Due to the current lack of proper infrastructure for Swift dependency management, using Realm in your project requires the following steps:
Add Realm as a submodule by opening the Terminal, cd-ing into your top-level project directory, and entering the command git submodule add git#github.com:realm/realm-cocoa.git
Open the realm-cocoa folder, and drag Realm.xcodeproj into the file navigator of your Xcode project.
In Xcode, navigate to the target configuration window by clicking on the blue project icon, and selecting the application target under the “Targets” section in the sidebar.
In the tab bar at the top of that window, open the “Build Phases” panel.
Expand the “Target Dependencies” gorup, and add Realm’s iOS framework.
Expand the “Link Binary with Libraries” group, and add Realm’s iOS framework as well as libc++.dylib.
Click on the + button at the top left of the panel and select “New Copy Files Phase”. Rename this new phase to “Copy Frameworks”, set the “Destination” to “Frameworks”, and add Realm.framework.
Drag the file at realm-cocoa/Realm/Swift/RLMSupport.swift into the file navigator of your Xcode project, unchecking the “Copy items if needed” checkbox.
Below is what it looks like in my project:
I am not sure exactly why this isn't working, but here is a workaround:
Follow the latest instructions.
Create a bridging header, for example by
Add a new Objective-C class to your xcode project.
Agree to have a bridging header created
Delete the Objective-C class
Add this in the bridging header:
#import "Realm/Realm.h"
Remove any Import Realm statements from your code, including from RLMSupport.swift
Now it should work. For example, I test with putting this in my ViewController.swift
import UIKit
class Person: RLMObject {
dynamic var name = ""
dynamic var birthdate = NSDate(timeIntervalSince1970: 1)
}
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let author = Person()
author.name = "David Foster Wallace"
// Get the default Realm
let realm = RLMRealm.defaultRealm()
// Add to the Realm inside a transaction
realm.beginWriteTransaction()
realm.addObject(author)
realm.commitWriteTransaction()
// Print all Persons
println(Person.allObjects())
}
}
Which prints:
RLMArray <0x7a243760> (
[0] Person {
name = David Foster Wallace;
birthdate = 1970-01-01 00:00:01 +0000;
}
)
I have been talking with the guys at Realm, and it turns out that the latest instructions don't work with Realm <= 0.85 They changed they way the build the framework and it won't work anymore. They said they will release 0.86 later today that should fix the problems anyone is having with Swift. In the meantime I have a test project that anyone can take the latest framework from. https://github.com/smitt04/testRealm
Version 0.86 is now out and this is no longer an issue.
The Swift installation instructions were long and convoluted, so I'm not surprised you and several other users ran into issues.
Please follow the latest installation instructions here.
After adding StoreKit to my Xcode 5 project, I now see...
"Malformed or corrupted AST file: mismatched umbrella header in submodule"
...whenever I've imported any header from StoreKit. I haven't changed those system headers, and clearing derived data and the usual Clean Build Folder fix doesn't work either, nor does restarting Xcode change anything.
I see in the Clang sources where the error is being reported, but I can't tell why. Here's the relevant Clang code from http://clang.llvm.org/doxygen/ASTReader_8cpp_source.html:
case SUBMODULE_UMBRELLA_HEADER: {
03728 if (First) {
03729 Error("missing submodule metadata record at beginning of block");
03730 return true;
03731 }
03732
03733 if (!CurrentModule)
03734 break;
03735
03736 if (const FileEntry *Umbrella = PP.getFileManager().getFile(Blob)) {
03737 if (!CurrentModule->getUmbrellaHeader())
03738 ModMap.setUmbrellaHeader(CurrentModule, Umbrella);
03739 else if (CurrentModule->getUmbrellaHeader() != Umbrella) {
03740 Error("mismatched umbrella headers in submodule");
03741 return true;
03742 }
03743 }
03744 break;
03745 }
It evidently compares my imported umbrella header
#import <StoreKit/StoreKit.h>
to something else, but I can't determine what that something else is.
Has anyone else encountered this, and hopefully found a way to resolve it?
I was able to encounter this problem, following the direction mentioned by John above doesn't work for me. I was able to solve this problem by doing the following:
close all open XCode projects.
Delete all the folders inside Derived Data folder.
How to go to Derived Data folder? just right click on your Product build and show finder, browse through the hierarchy of folders and look for Derived Data.
Hope this helps. This seems to be a bug in Xcode? but not sure.
This solved it for me:
go to project build settings
scroll down to LLVM 5.1 Language Modules
set Enable Modules (C and Obj C) to NO
In Xcode go to Window->Organizer->Projects select your project and press delete button next to Derived data. Just did it (and it worked) with information the following question:
fatal error: malformed or corrupted AST file - Xcode
In your Project folder there would be couple of folders named Derived Data and build. Just remove those folders and the problem would be resolved. These folders get created automatically once you open your XCode.