我有一个FileHelper类,我已经实现了3个方法,其工作是将Dictionary内容写入文件.那些方法是:
func storeDictionary(_ dictionary: Dictionary<String, String>, inFile fileName: String, atDirectory directory: String) -> Bool {
let ext = "txt"
let filePath = createFile(fileName, withExtension: ext, atDirectory: directory)
/**** //If I use this method, file is created and dictionary is saved
guard (dictionary as NSDictionary).write(to: filePath!, atomically: true) else {
return false
}
*/
guard NSKeyedArchiver.archiveRootObject(dictionary, toFile: (filePath?.absoluteString)!) else {
return false
}
return true
}
func createFile(_ file: String, withExtension ext: String, atDirectory directory: String) -> URL? {
let directoryPath …Run Code Online (Sandbox Code Playgroud) 我的变量有问题n。n我不能在 for 循环中使用。为什么?n在 for 循环之前初始化。请帮忙。
import Foundation
var n: Int
var t: Int
while(true){
var tt = readLine()
t = Int(tt!)!
if (t==0){
break
}
else if ( t < 0){
n = t*(-1)
}
else if(t > 0){
n = t
}
var arr : [[String]] = []
for i in 0..<n*2{
for y in 0..<n*2{
arr[i][y] = "."
}
}
}
Run Code Online (Sandbox Code Playgroud) 我有这个代码:
NSDateFormatter *dateFormatter = [NSDateFormatter new];
dateFormatter.dateStyle = NSDateFormatterLongStyle;
dateFormatter.timeStyle = NSDateFormatterLongStyle;
NSDate *myDate = [dateFormatter dateFromString:@"2016-03-01 13:42:17"];
Run Code Online (Sandbox Code Playgroud)
对于<iOS 11,找到预期日期,但对于iOS 11(在iOS 11.1的iPhone中测试),这将返回nil.
我没有看到dateFromString的任何API更改:在Apple的doc中.怎么了?
我正在尝试更新我的应用程序以使用 Swift 4 Decodable - 并且正在从具有子值的 JSON api 中提取数据,这些子值可能是:
这是 Json Api 响应:
var jsonoutput =
"""
{
"id": "124549",
"key": "TEST-32",
"fields": {
"lastViewed": "2018-02-17T21:40:38.864+0000",
"timeestimate": 26640
}
}
""".data(using: .utf8)
Run Code Online (Sandbox Code Playgroud)
我尝试使用以下方法解析它:如果我只引用都是字符串的 id 和 key 属性,这会起作用。
struct SupportDeskResponse: Decodable{
var id: String
var key: String
//var fields: [String: Any] //this is commented out as this approach doesn't work - just generated a decodable protocol error.
}
var myStruct: Any!
do {
myStruct = try JSONDecoder().decode(SupportDeskResponse.self, from: …Run Code Online (Sandbox Code Playgroud) 我希望在我的 WiFi 路由器颜色从绿色变为红色时收到通知
我正在制作一个应用程序,它可以通过 Swift 的菜单栏告诉您是否在线或离线,该应用程序是开源的,可以在https://github.com/deadcoder0904/net-alert找到
我想知道当 Swift 中 WiFi 颜色发生变化时是否可以收到通知。
我无法不断地 ping 我的服务器来知道颜色发生了变化,因为这会浪费互联网资源。
那么在 Swift 中是否可以知道这一点呢?
如何在Swift 3/4中替换或重复数组的特定项目(v):
["A","s","B","v","C","s","D","v","E","s"]
得到这个:
["A","s","B","v","v","C","s","D","v","v","E","s"]
或这个:
["A","s","B","v","v","v","C","s","D","v","v","v","E","s"]
["A","s","B","v","v","v","v","C","s","D","v","v","v","v","E","s"]
Run Code Online (Sandbox Code Playgroud)
原因是元素v在音频文件(A,B,C,...)之间插入暂停(秒).项目v的重复次数应通过SegmentedControl(1,2,...,6)设置.
这就是我生成公钥/私钥对的方式
var statusCode: OSStatus
var publicKey: SecKey?
var privateKey: SecKey?
let publicKeyAttribute: [NSObject : NSObject] = [kSecAttrIsPermanent: true as NSObject, kSecAttrApplicationTag: "publictag".data(using: String.Encoding.utf8)! as NSObject]
let privateKeyAtrribute: [NSObject: NSObject] = [kSecAttrIsPermanent: true as NSObject, kSecAttrApplicationTag: "privatetag".data(using: String.Encoding.utf8)! as NSObject]
var keyPairAttr = [NSObject: Any]()
keyPairAttr[kSecAttrType] = kSecAttrKeyTypeRSA
keyPairAttr[kSecAttrKeySizeInBits] = 2048
keyPairAttr[kSecReturnData] = true
keyPairAttr[kSecPublicKeyAttrs] = publicKeyAttribute
keyPairAttr[kSecPrivateKeyAttrs] = privateKeyAtrribute
statusCode = SecKeyGeneratePair(keyPairAttr as CFDictionary, &publicKey, &privateKey)
Run Code Online (Sandbox Code Playgroud)
这会生成如下所示的密钥对,
公钥:
Optional(<SecKeyRef algorithm id: 1, key type: RSAPublicKey, version: 4, block size: 2048 …Run Code Online (Sandbox Code Playgroud)