【问题标题】:Xcode SQLClient IssuesXcode SQLClient 问题
【发布时间】:2020-06-03 18:10:11
【问题描述】:

我正在使用一个名为“SQLClient”的库,位于:https://github.com/martinrybak/SQLClient

访问我位于服务器上的数据库。安装库、pod 等后,我尝试制作一个 Objective-c 头文件并将示例代码示例放入文件中以测试是否没有错误,但 IDE 给我语法错误。

这是我刚刚放入应用程序的示例代码:

SQLClient* client = [SQLClient sharedInstance]; //initializer element is not a compile time constant

[client connect:@"server\instance:port" username:@"user" password:@"pass" database:@"db" completion:^(BOOL success) { //expected identifier or '('
    if (success) {
      [client execute:@"SELECT * FROM Users" completion:^(NSArray* results) {
        for (NSArray* table in results) {
          for (NSDictionary* row in table) {
            for (NSString* column in row) {
              NSLog(@"%@=%@", column, row[column]);
            }
          }
        }
        [client disconnect];
      }];
    }
}]; // expected ']'

我将 cmets 放在我的 IDE 给我的错误行旁边。

这是一个完整的列表:

  1. 初始化元素不是编译时间常数
  2. 预期标识符或'('
  3. 预期为“]”
  4. 消息发送表达式开头缺少“[”

任何建议将不胜感激

【问题讨论】:

    标签: objective-c swift xcode sqlclient objective-c-swift-bridge


    【解决方案1】:

    以下解决方案适用于 Swift 5 。您需要将#import "SQLClient.h" 添加到桥接头文件中。

    podfile

    use_frameworks!
    platform :ios, '9.0'
    
    install! 'cocoapods'
    
    target 'SQLClient' do
    use_frameworks!
    pod 'SQLClient', '~> 1.0.0'
    end
    

    ViewController 文件看起来像,

    override func viewDidLoad() {
            super.viewDidLoad()
             //Adding the observer for error and to receive the message
            NotificationCenter.default.addObserver(self, selector: #selector(error(_:)), name: NSNotification.Name.SQLClientError, object: nil)
            NotificationCenter.default.addObserver(self, selector: #selector(message(_:)), name: NSNotification.Name.SQLClientMessage, object: nil)
    
            let client = SQLClient.sharedInstance()!
                      client.connect("ServerNameOrIP", username: "cool", password: "cool", database: "database") { success in
                      client.execute("SELECT * FROM table", completion: { (_ results: ([Any]?)) in
                       for table in results as! [[[String:AnyObject]]] {
                           for row in table {
                               for (columnName, value) in row {
                                   print("\(columnName) = \(value)")
                               }
                           }
                       }
                       client.disconnect()
                   })
               }
              }
    
        @objc func error(_ notification: Notification?) {
         let code = notification?.userInfo?[SQLClientCodeKey] as? NSNumber
         let message = notification?.userInfo?[SQLClientMessageKey] as? String
         let severity = notification?.userInfo?[SQLClientSeverityKey] as? NSNumber
         if let code = code, let severity = severity {
             print("Error #\(code): \(message ?? "") (Severity \(severity))")
         }
     }
    
        @objc func message(_ notification: Notification?) {
            let message = notification?.userInfo?[SQLClientMessageKey] as? String
            print("Message: \(message ?? "")")
        }
    

    创建了一个示例项目here

    【讨论】:

    • 嗨,当我使用你的代码时,请帮助我,它说使用未解析的标识符 SQLClien 我必须导入一些东西吗??
    • 是的,您需要在桥接头文件中添加#import "SQLClient.h"。
    猜你喜欢
    • 2021-10-13
    • 1970-01-01
    • 2011-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多