在Swift 3.x中,我们通常使用Data; 从中你可以生成大多数其他重要类型,并且它上面有很多有用的功能.
但是我如何创建Data一个InputStream?有一个很好的方式吗?
根据文档,应该可以使用项目访问令牌访问 GitLab 存储库:
用户名设置为project_{project_id}_bot,如project_123_bot。
别介意那是谎言——project_4194_bot1在我的例子中调用的是实际用户;显然,他们为后续令牌增加了一个数字。
无论哪种方式——我都试过有尾随和没有尾随1——我希望
git clone "https://project_4194_bot1:$PROJECT_TOKEN@my.gitlab.host/my-group/my-project.git"
Run Code Online (Sandbox Code Playgroud)
成功,与my.username:$PERSONAL_TOKEN(完美运作)相同。但是,我得到
remote: HTTP Basic: Access denied
fatal: Authentication failed for '<snip>'
Run Code Online (Sandbox Code Playgroud)
这里可能发生了什么?如何使用项目访问令牌访问 GitLab 存储库?
好像我们不会走那么远,但是 FWIW,令牌似乎有足够的权限:
我正在编写一个Ruby脚本/应用程序,可以帮助我将LaTeX编译为(至少)PDF。我希望它具有的功能之一是它应该pdflatex迭代运行,直到PDF收敛为止(我猜应该如此)。
这个想法是将一次迭代中生成的PDF与使用其指纹的前一次迭代中生成的PDF进行比较。特别是,我目前使用Digest::MD5.file(.)。
现在的问题是,这永远不会收敛。(希望的)罪魁祸首是PDF的时间戳,时间戳至少设置为秒pdflatex。由于运行时间pdflatex通常超过一秒钟,因此结果不断变化。也就是说,我希望在某个时间点之后,PDF的时间戳等于时间戳。这个假设可能是错误的。提示表示赞赏。
我该怎么办?到目前为止,我的基本想法是:
您有更多的想法,甚至解决方案吗?解决方案只能使用在Linux上运行的免费软件。这样,只使用Ruby是首选,但是完全可以使用外部软件。
顺便说一句,我不完全了解PDF的编码方式,但我怀疑仅比较包含的文本对我不起作用,因为在以后的迭代中仅图形或链接可能会更改。
可能相关:
假设我想foo在A我控制之外的现有类型上提供方法.据我所知,在Scala中执行此操作的规范方法是实现从A实现的某种类型的隐式转换foo.现在我基本上看到两个选项.
为此目的定义一个单独的,甚至是隐藏的类:
protected class Fooable(a : A) {
def foo(...) = { ... }
}
implicit def a2fooable(a : A) = new Fooable(a)
Run Code Online (Sandbox Code Playgroud)定义内联匿名类:
implicit def a2fooable(a : A) = new { def foo(...) = { ... } }
Run Code Online (Sandbox Code Playgroud)变体2)肯定是较少的样板,特别是当发生许多类型参数时.另一方面,我认为它应该创建更多的开销,因为(概念上)每个转换创建一个类,而不是1)中的一个全局类.
有一般指导方针吗?没有区别,因为编译器/ VM摆脱了2)的开销?
对于随机字符串生成器,我认为将其CharacterSet用作要使用的字母表的输入类型会很好,因为诸如此类的预定义集CharacterSet.lowercaseLetters显然很有用(即使它们可能包含比您预期的更多样化的字符集) .
但是,显然您只能查询字符集的成员资格,而不能枚举,更不用说索引它们了。我们得到的只是_.bitmapRepresentation8kb 的数据块,每个 (?) 字符都有一个指示位。但是,即使您按索引剥离单个位i(这不太好,通过面向字节的Data),Character(UnicodeScalar(i))也不会给出正确的字母。这意味着格式有些晦涩——当然,它没有记录在案。
当然,我们可以迭代所有字符(每个平面),但这是一个坏主意,成本方面:20 个字符集可能需要迭代数万个字符。用 CS 术语来说:位向量是稀疏集的(非常)糟糕的实现。为什么他们选择在这里以这种方式进行权衡,我不知道。
我在这里遗漏了什么,或者CharacterSet只是FoundationAPI 中的另一个死胡同?
考虑这段代码:
extension Collection {
func foo() -> Int {
if self.first is Collection {
return (self.first as! Collection).underestimatedCount // ERROR
}
else {
return self.underestimatedCount
}
}
}
Run Code Online (Sandbox Code Playgroud)
我们得到了令人恐惧且明显令人困惑的情况:
协议“Collection”只能用作通用约束,因为它具有 Self 或关联的类型要求。
然而,这很高兴编译:
func foo<C: Collection>(_ c: C) -> Int where C.Iterator.Element: Collection {
if let first = c.first {
return first.underestimatedCount // *
} else {
return c.underestimatedCount
}
}
Run Code Online (Sandbox Code Playgroud)
为什么?!
特别是,编译器不知道关联*类型(的类型)first是如何实现的;它只得到了它们已经得到的承诺(因为任何类型的对象都Collection 必须实现它们)。第一个示例中也有同样的保证!那么为什么编译器会抱怨其中之一而不是另一个呢?
我的问题是:在 line 处*,编译器知道什么是它不 in …
我想擦除输入字符串。让我们从这个开始:
func foo(s: String) {
s.replaceSubrange(0..<s.characters.count,
with: String(repeating: "0", count: s.characters.count))
}
Run Code Online (Sandbox Code Playgroud)
可以预见的是,这会导致
不能对不可变值使用变异成员:“s”是“let”常量
美好的:
func foo(s: inout String) {
s.replaceSubrange(0..<s.characters.count,
with: String(repeating: "0", count: s.characters.count))
}
Run Code Online (Sandbox Code Playgroud)
但现在:
“inout String”无法转换为“String”
指着.character——什么?!
奇怪的是,当我这样做时:
func foo(s: inout String) {
let n = s.characters.count
s.replaceSubrange(0..<n,
with: String(repeating: "0", count: n))
}
Run Code Online (Sandbox Code Playgroud)
调用.characters完全没问题,但是
无法使用“(CountableRange, with: String)”类型的参数列表调用“replaceSubrange”
使用0...n-1也不行。
如何替换参数字符串中的字符?
我正在尝试使用多个构造函数实现不可变数据类.我觉得这样的事情应该是可能的:
data class Color(val r: Int, val g: Int, val b: Int) {
constructor(hex: String) {
assert(Regex("#[a-fA-F0-6]{6}").matches(hex), { "$hex is not a hex color" } )
val r = hex.substring(1..2).toInt(16)
val g = hex.substring(3..4).toInt(16)
val b = hex.substring(5..6).toInt(16)
this(r,g,b)
}
}
Run Code Online (Sandbox Code Playgroud)
当然,它不是:Kotlin期望对主构造函数的调用在顶部声明:
constructor(hex: String): this(r,g,b) {
assert(Regex("#[a-fA-F0-6]{6}").matches(hex), { "$hex is not a hex color" } )
val r = hex.substring(1..2).toInt(16)
val g = hex.substring(3..4).toInt(16)
val b = hex.substring(5..6).toInt(16)
}
Run Code Online (Sandbox Code Playgroud)
这也没有用,因为调用是在构造函数体之前执行的,并且无法访问局部变量.
我可以做这个,当然是:
constructor(hex: …Run Code Online (Sandbox Code Playgroud) MyFramework.framework我想以编程方式访问运行时包含的版本和构建信息。
我在这里找到了一些解决方案,但它们不起作用。翻译成Swift 3后,我发现它Bundle.main.infoDictionary是空的。
我怎样才能得到这些信息?
这段代码过去很好(从编译器没有抱怨的意义上说):
extension OutputStream {
func write(_ data: Data) -> Int {
return data.withUnsafeBytes { pointer in
return self.write(pointer, maxLength: data.count)
}
}
}
Run Code Online (Sandbox Code Playgroud)
从 Swift 5.0 开始,这会产生警告:
警告:不推荐使用“withUnsafeBytes”:
withUnsafeBytes<R>(_: (UnsafeRawBufferPointer) throws -> R) rethrows -> R改用
我试着用所提出的方法,但我似乎无法缠斗UnsafeRawBufferPointer到UnsafePointer<UInt8>这OutputStream.write最终需要。
如何以非弃用的方式编写此函数?
我有一个非常简单的程序如下:#include
int main(){
char* mystring = "ABCDEFGHIJKLMNO";
puts(mystring);
char otherstring[15];
otherstring[0] = 'a';
otherstring[1] = 'b';
otherstring[2] = 'c';
otherstring[3] = 'd';
otherstring[4] = 'e';
otherstring[5] = 'f';
otherstring[6] = 'g';
otherstring[7] = 'h';
otherstring[8] = 'i';
otherstring[9] = 'j';
otherstring[10] = 'k';
otherstring[11] = 'l';
otherstring[12] = 'm';
otherstring[13] = 'n';
otherstring[14] = 'o';
puts(otherstring);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译器是MS VC++.
无论是否使用优化构建此程序,我都可以使用十六进制编辑器在可执行文件中找到字符串"ABCDEFGHIJKLMNO".
但是,我找不到字符串"abcdefghijklmno"
对于其他字符串,编译器执行的操作有何不同?
我使用的十六进制编辑器是Hexedit - 但尝试了其他人,但仍然找不到其他字符串.任何想法为什么不或如何找到?
顺便说一句,我不是因为黑客原因而这样做的.
swift ×6
binary-data ×2
stream ×2
string ×2
c ×1
casting ×1
character ×1
constructor ×1
data-class ×1
foundation ×1
git ×1
gitlab ×1
immutability ×1
implicits ×1
inout ×1
inputstream ×1
kotlin ×1
latex ×1
outputstream ×1
pdf ×1
ruby ×1
scala ×1
swift5 ×1
typechecking ×1