一些swift问题

写得比较快,如果有问题请私信。

序列化和反序列化

反序列化的jsonString2只是给定的任意json字符串

Swift 复制代码
private func p_testDecodeTable() {
    let arr = ["recordID123456", "recordID2"]

    // 序列化[string] -> json data
    let jsonData = try? JSONEncoder().encode(arr)
    print("data:")
    print(String(describing: jsonData))

    let jsonString = String(data: jsonData!, encoding: .utf8)
    print("jsonString:")
    print(jsonString!)

    // 反序列化, json data -> [string]
    let jsonString2 = jsonString
    let jsondata2 = jsonString2!.data(using: .utf8)
    print("decode data:\(jsondata2!)")
    if let decodeArr = try? JSONDecoder().decode([String].self, from: jsondata2!) {
        print("decode arr:")
        decodeArr.forEach {
            print($0)
        }
    }
}

字符串插值和数据库sql语句结合

参考下面代码,需要注意String(format:)是多参数的形式, 不需要放数组里, 有多少个参数都可以直接写入。

database.executeUpdate则是需要把参数都放withArgumentsIn的数组里。

Swift 复制代码
// 某处定义了这个
static let sUpdateTable = """
update %@ set %@ = ? where id = ?;
"""

// 在处理的地方可以这么写, dbQueue是FMDatabaseQueue(path:)方法创建的
dbQueue.inDatabase { database in
    // 组成字符串
    let sentence1 = sUpdateTable
    let tableName1 = "address"
    let column1 = "detail"
    let str1 = String(format: sentence1, tableName1, column1)
    print(str1)
            
    // 组装成sql语句并且执行
    let value1 = "somePlace"
    let id1 = 30
    if !database.executeUpdate(str1, withArgumentsIn: [value1, id1]) {
    print("error: table \(tableName1) detail \(column1)")
    }
}

得到的数据库语句如下,问号符号在database.executeUpdate语句中会替换成value1和id1的值

字符串裁剪

Swift 复制代码
let title = "sdfjkhdsfashdfkjlhsakdsdfhdgdfg"

// 方法1            
let endIndex = title.index(title.startIndex, offsetBy: 10)
let subtitle = title[..<endIndex]

// 方法2
let subtitle2 = title.truncate(toLength: 20)

字符串和数组的转化

Swift 复制代码
// 为了得到["key3", "key4"]的数组
let string = "#key3#key4"
let arr = string.components(separatedBy: "#")

// 为了得到"#key5#key6"的字符串
let keywordArr = ["key5", "key6"]
let str = keywordArr.joined(separator: "#")

sql语句可以用三个引号

Swift 复制代码
dbQueue.inDatabase { database in
    let sql = """
    insert into marks (titles)
    values (?);
    """

    database.executeUpdate(sql, withArgumentsIn: ["我的标题222"])
}


/// 可以写多个参数, 只要与问号个数以及ArgumentIn数组的值对应起来即可

let sql = """
insert into marks (titles, detail, referenceImage)
values (?, ?, ?);
"""
复制代码
一些内容在swiftGG文档上说得挺清楚的
相关推荐
TouchWorld14 小时前
iOS逆向-哔哩哔哩增加3倍速播放(2)-[横屏视频-半屏播放]增加3倍速播放
ios·swift
1024小神15 小时前
xcode 中配置AR Resource Group并设置图片宽度等
ios·swiftui·ar·xcode·swift
Wcowin1 天前
OneClip 开发经验分享:从零到一的 macOS 剪切板应用开发
mac·swift·粘贴板
崽崽长肉肉1 天前
Swift中的知识点总结
ios·swift
代码不行的搬运工1 天前
面向RDMA网络的Swift协议
开发语言·网络·swift
大熊猫侯佩2 天前
拯救发际线行动:用 Swift 和 Image Playground 驾驭 AI 绘图
人工智能·ai·文生图·swift·图生图·imageplayground·apple 智能
linweidong2 天前
网易ios面试题及参考答案(下)
objective-c·swift·ios开发·切面编程·ios面试·苹果开发·mac开发
大熊猫侯佩3 天前
Swift 迭代三巨头(下集):Sequence、Collection 与 Iterator 深度狂飙
swift·编程语言·apple
大熊猫侯佩3 天前
Swift 迭代三巨头(中集):Sequence、Collection 与 Iterator 深度狂飙
swift·编程语言·apple
大熊猫侯佩3 天前
Swift 迭代三巨头(上集):Sequence、Collection 与 Iterator 深度狂飙
swift·编程语言·apple