Xcode SQLClient问题

问题描述 投票:0回答:1

我使用的是一个名为 "SQLClient "的库,位于这里。https:/github.commartinrybakSQLClient

来访问我位于服务器上的数据库。在安装完库和pods等之后,我试着做了一个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 ']'

我在IDE给我的错误行旁边加了注释。

这里是一个完整的列表。

  1. Initializer element is not a compile time constant(初始化元素不是编译时常数)
  2. 预期标识符或'('
  3. 预期']'
  4. 在信息发送表达式的开头缺少'['。

恳请提供任何建议

objective-c swift xcode sqlclient objective-c-swift-bridge
1个回答
0
投票

下面的解决方案工作 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 ?? "")")
    }

创建了一个样本项目 此处

© www.soinside.com 2019 - 2024. All rights reserved.