uuid = UUID(uuidString: String) 创建大写字母

huangapple go评论82阅读模式
英文:

uuid = UUID(uuidString: String) create uppercase Letters

问题

我在函数UUID(uuidString: String)中遇到了一个问题。当将UUID作为小写字母的字符串传递给上述函数时,它会生成一个带有大写字母的UUID。有人可以给我一些提示,是什么原因导致了这个问题。

使用情况:
服务器:使用Vapor 4和Postgres DB的数据库服务器。存储应该是特定于用户的。
客户端:iOS应用程序具有登录过程,服务器响应包含UserID和token的请求,并将其存储在UserDefaults.standard中的键中。
URL是根据UserID动态生成的,并作为HTTP请求发送到服务器。
服务器检查通过这种方式传递的UserID,但无法识别它,因为Postgres DB使用小写字母创建和存储UUID。然而,客户端生成的URL包含已登录用户的相应UUID,其中包含大写字母。

英文:

I have a problem with the function UUID(uuidString: String). Passing a UUID as String with lower case letters the above function generates a UUID with upper case letters. Can anyone give me a hint as to what is causing this.

Use case:
Server :Database server with Vapor 4 and Postgres DB. Storage should be user specific
Client: iOS with login procedure, server responds with UserID and token, both stored in UserDefaults.standard with key.
URL's are dynamically generated with the UserID and sent as HTTP request to the server.
Server checks UserID passed this way and doesn't recognize it, because Postgres DB creates and stores UUID's with lower case letters. However, the URL generated in the client contains the corresponding UUID of the logged-in user with upper case letters.

答案1

得分: 1

技术上讲,uuid = UUID(uuidString: String) 通过一个内部表示为16个 UInt8 字节的 UUID 对象来创建一个 UUID,该表示不区分大小写:

let uuid1 = UUID(uuidString: "9E9BF10C-06E2-4200-AE1A-B15F7960EEAF")!
print(uuid1.uuid)
// (158, 155, 241, 12, 6, 226, 66, 0, 174, 26, 177, 95, 121, 96, 238, 175)
let uuid2 = UUID(uuidString: "9e9bf10c-06e2-4200-ae1a-b15f7960eeaf")!
print(uuid2.uuid)
// (158, 155, 241, 12, 6, 226, 66, 0, 174, 26, 177, 95, 121, 96, 238, 175)

一个复杂的因素是,Swift 实现了 .uuidString 以返回大写表示形式(这是从苹果的 NSUUID 继承而来,具有许多遗留用途)。而许多其他项目遵循 RFC 4122 第3节 "命名空间注册模板" 的指导:

> UUID 的内部表示是内存中的特定位序列...
>
> 十六进制值 "a" 到 "f" 以小写字符输出,并且在输入时不区分大小写。

在我的用例中,我通过 .rfc4122String 扩展了 Swift 的 UUID,以提供一个小写的 UUID 字符串,以表达标准的合规意图。

public extension UUID {
    /// 根据 RFC 4122 第3节的指导,返回小写字符串
    var rfc4122String: String {
        return self.uuidString.lowercased()
    }
}

此外,如果存在大小写不明确的 UUID 字符串,将其转换为 UUID 对象将同时(1)通过 init() 验证 uuid 字符串,不返回 nil,并且(2)允许进行 UUID-to-UUID 的不区分大小写比较。

英文:

Technically, uuid = UUID(uuidString: String) creates a UUID object with an internal representation of 16 UInt8 bytes which is case independent:

uuid = UUID(uuidString: String) 创建大写字母

let uuid1 = UUID(uuidString: "9E9BF10C-06E2-4200-AE1A-B15F7960EEAF")!
print(uuid1.uuid)
// (158, 155, 241, 12, 6, 226, 66, 0, 174, 26, 177, 95, 121, 96, 238, 175)
let uuid2 = UUID(uuidString: "9e9bf10c-06e2-4200-ae1a-b15f7960eeaf")!
print(uuid2.uuid)
// (158, 155, 241, 12, 6, 226, 66, 0, 174, 26, 177, 95, 121, 96, 238, 175)

A complicating factor is that Swift implements .uuidString to return an uppercased representation. (Inherited from Apple's NSUUID which has lots of legacy use.) Whereas, many other projects follow the RFC 4122 Section 3 "Namespace Registration Template" guidance:

> The internal representation of a UUID is a specific
> sequence of bits in memory …
>
> The hexadecimal values "a" through "f" are
> output as lower case characters

> and are case insensitive on input.

In my use case, I've extended the Swift UUID with .rfc4122String to provide a lowercased uuid string in a way that expresses standard compliance intent.

public extension UUID {
    /// lowercased string per RFC 4122 section 3 guidelines
    var rfc4122String: String {
        return self.uuidString.lowercased()
    }
}

Also, if ambiguously-cased UUID strings are present, the conversion to a UUID object will both (1) validate the uuid string by the init() not returning nil and (2) allow for UUID-to-UUID case-independent comparison.

huangapple
  • 本文由 发表于 2023年8月8日 23:51:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76861227.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定