SwiftSparql 0.12.0

SwiftSparql 0.12.0

BAN Jun 维护。



SwiftSparql

CI verbgen Version License Platform

SwiftSparql 包括

  • SPARQL 查询生成器
  • SPARQL 响应解析器(SPARQL 1.1 查询结果 JSON 格式(application/sparql-results+json))
  • 端点中重要动词的 Swift 代码生成器 verbgen

示例

要运行示例项目,请先克隆存储库,然后首先在示例目录中运行 pod install

安装

CocoaPods

SwiftSparql 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中

pod 'SwiftSparql'

Swift 包管理器

步骤

mkdir sparql
cd sparql
swift package init --type executable

编辑生成的 Package.swift

// swift-tools-version:4.2
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "sparql",
    dependencies: [
        // Dependencies declare other packages that this package depends on.
        .package(url: "https://github.com/banjun/SwiftSparql", from: "0.2.0"),
    ],
    targets: [
        .target(
            name: "sparql",
            dependencies: ["SwiftSparql"])
    ]
)
swift package generate-xcodeproj
open sparql.xcodeproj

编辑 main.swift 并在 macOS 目标下运行。

import Foundation
import SwiftSparql
:

例如

// construct query
let q = SelectQuery(
    where: WhereClause(patterns:
        subject(Var("s"))
            .rdfTypeIsImasIdol() // in the followings, verbs for this type can be auto-completed
            .imasType(is: "Cu")
            .foafAge(is: 17)
            .schemaName(is: Var("name"))
            .schemaHeight(is: Var("height"))
            .triples),
    order: [.desc(v: Var("height"))])

// capture results by Codable type
struct Idol: Codable {
    var name: String
    var height: Double
}

// request query to the endpoint URL
Request(endpoint: URL(string: "https://sparql.crssnky.xyz/spql/imas/query")!, select: q)
    .fetch()
    .onSuccess { (idols: [Idol]) in // Codable type annotation for being decoded by fetch()
        idols.forEach { idol in
            print("\(idol.name) (\(idol.height)cm)")
        }
        exit(0)
    }.onFailure {fatalError(String(describing: $0))}

dispatchMain()

特别感谢

im@sparql 数据集及其端点:https://github.com/imas/imasparql

许可

SwiftSparql 采用 MIT 许可协议。详情请参阅 LICENSE 文件。