相关疑难解决方法(0)

String.Index如何在Swift中工作

我一直在使用Swift 3更新我的一些旧代码和答案,但是当我使用Swift Strings和Indexing时,理解事情是一件很痛苦的事情.

具体来说,我尝试以下方法:

let str = "Hello, playground"
let prefixRange = str.startIndex..<str.startIndex.advancedBy(5) // error
Run Code Online (Sandbox Code Playgroud)

第二行给我以下错误

'advancedBy'不可用:要将索引推进n步,请在生成索引的CharacterView实例上调用'index(_:offsetBy :)'.

我看到它String有以下方法.

str.index(after: String.Index)
str.index(before: String.Index)
str.index(String.Index, offsetBy: String.IndexDistance)
str.index(String.Index, offsetBy: String.IndexDistance, limitedBy: String.Index)
Run Code Online (Sandbox Code Playgroud)

起初这些让我很困惑所以我开始玩它们直到我理解它们.我在下面添加一个答案来说明它们是如何使用的.

string indexing swift

92
推荐指数
1
解决办法
7万
查看次数

快速对象的安全内存

我正在编写一个需要在内存中处理私钥的快速应用程序.由于这些对象的敏感性,当对象被释放时,需要清除密钥(也称为全零),并且内存不能被分页到磁盘(通常使用mlock()完成).

在Objective-C中,您可以提供自定义CFAllocator对象,该对象允许您使用自己的函数来分配/释放/重新分配对象使用的内存.

因此,一种解决方案是在objective-c中实现"SecureData"对象,该对象使用自定义CFAllocator(也在objective-c中)内部创建NSMutableData对象.

但是,有没有办法为纯swift对象提供自己的自定义内存分配函数(例如,struct或[UInt8])?或者是否有更好的"适当"方式在swift中实现这样的安全内存?

security macos memory-management ios swift

27
推荐指数
1
解决办法
2797
查看次数

标签 统计

swift ×2

indexing ×1

ios ×1

macos ×1

memory-management ×1

security ×1

string ×1