小编nay*_*yem的帖子

如何将Dictionary写入文件?

我有一个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)

nskeyedarchiver nsfilemanager swift-dictionary swift3

2
推荐指数
1
解决办法
3302
查看次数

swift 3 在开始初始化之前使用变量

我的变量有问题nn我不能在 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)

swift

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

NSDateFormatter的dateFromString:在iOS 11中返回nil

我有这个代码:

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中.怎么了?

iphone objective-c ios ios11

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

使用 Swift 4 Decodable 解析具有混合属性值的 JSON

我正在尝试更新我的应用程序以使用 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)

json swift swift4 decodable

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

Swift 中如何知道实际的互联网是否可用?

我希望在我的 WiFi 路由器颜色从绿色变为红色时收到通知

我正在制作一个应用程序,它可以通过 Swift 的菜单栏告诉您是否在线或离线,该应用程序是开源的,可以在https://github.com/deadcoder0904/net-alert找到

我想知道当 Swift 中 WiFi 颜色发生变化时是否可以收到通知。

我无法不断地 ping 我的服务器来知道颜色发生了变化,因为这会浪费互联网资源。

那么在 Swift 中是否可以知道这一点呢?

notifications reachability ios swift

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

重复一个数组的特定项

如何在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)设置.

swift

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

如何在ios中快速生成公钥/私钥对,如下所示

这就是我生成公钥/私钥对的方式

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)

rsa pem der ios swift

0
推荐指数
1
解决办法
2719
查看次数