在iOS框架中使用RealmSwift:多数符号可找到但部分缺失
RealmSwift 部分API运行时符号未找到问题排查
我正在构建一个集成RealmSwift源码的iOS框架(同时支持.framework和.xcframework),发现仅使用Realm API的子集时,框架可正常构建并在App中运行,但调用部分API会触发运行时符号未找到错误。
例如,打开数据库、添加数据、查询数据等操作都能正常执行,但引用Results对象的相关属性时,会抛出如下错误:
dyld[11749]: Symbol not found:
_$s10RealmSwift7ResultsVyxGSlAASl5countSivgTW
示例代码
框架内核心代码
public class ObjectA: Object, Codable { @objc dynamic var category: String = "" override public static func primaryKey() -> String? { return "category" } } public func addA(key:String) { do { let objectA = ObjectA() objectA.category = key let realm = try Realm(configuration: configuration!) try? realm.write { realm.add(objectA, update: Realm.UpdatePolicy.all) } } catch let error as NSError { NSLog(TAG + "ERROR calling add() \(error.code) \(error.description)") } } public func getAllAs() -> Results<ObjectA> { let realm = try! Realm(configuration: configuration!) let items = realm.objects(ObjectA.self) return items }
App端调用代码
RealmDatabase.instance().addA(key: "something") // 正常运行 let found = RealmDatabase.instance().findObjectA(key: "something") // 正常运行 let allA = RealmDatabase.instance().getAllAs() // 正常运行 let c = allA.count // 触发错误
注:若App代码包含最后一行则触发错误,省略则可正常运行。
构建配置
框架的Podfile配置
target 'TheFramework' do use_frameworks! pod 'Realm', '10.49.1' pod 'RealmSwift', '10.49.1' end post_install do |installer| installer.pods_project.targets.each do |target| target.build_configurations.each do |config| config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '16.0' config.build_settings['BUILD_LIBRARY_FOR_DISTRIBUTION'] = 'YES' end end end
App的Podfile配置
target 'ReferenceApp' do project 'ReferenceApp.xcodeproj' use_frameworks! pod 'Realm', '10.49.1' pod 'RealmSwift', '10.49.1' end post_install do |installer| installer.pods_project.targets.each do |target| target.build_configurations.each do |config| config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '16.0' end end end
Xcode项目设置
- 框架项目:Pods.framework在
Frameworks & Libraries中设置为Do Not Embed,Build library for distribution设为YES - App项目:Pods.framework同样设置为Do Not Embed,自定义框架设置为Embed & Sign
核心疑问
为何能正常使用90%的Realm API,却有小部分API(如Results的count属性)触发符号未找到错误?
内容的提问来源于stack exchange,提问作者Piepants
相关产品推荐
相关产品推荐

