[英]Swift 5 How can I hash two arrays
How can I make this code SWIFT accepting?如何使此代码 SWIFT 接受? I've got two arrays of type ANY one array's value should act as the key, the other one as the appropriate value:
我有两个 arrays 类型的任何一个数组的值都应该作为键,另一个作为适当的值:
let it_tt_ar = db.pair(keys: "int_test", values: "text_test");
func _pair<K : Hashable, V>(keys: [K], values: [V]) -> Dictionary<K,V> {
var result = Dictionary<K, V>();
for i in 0...(keys.count - 1) {
result[keys[i]] = values[i];
}
return result;
}
func pair (keys: String?, values: String?) -> Dictionary<Int32,Any> {
if let _keys = keys, let _values = values {
let result = _pair(keys: hashtable[_keys] as! [Int32], values: hashtable[_values]!);
return result;
} else {
return [:];
}
}
I can't get it working if the type of the key is unknown.如果密钥的类型未知,我将无法正常工作。 I want to write it like this:
我想这样写:
let it_tt_ar = db.pair<Int32,String>(keys: "int_test", values: "text_test");
or
let it_tt_ar = db.pair(keys: "int_test", values: "text_test", kt:(Int32.self,String.self));
... in the last case by catching kt: in the function
But there's seems no chance to win against SWIFT:但似乎没有机会赢得 SWIFT:
It's terrible!它是可怕的! You want to write application logic but 80% of the development time is wasted by got to have fulfill such rules!
您想编写应用程序逻辑,但 80% 的开发时间都浪费在必须满足这些规则上!
It looks like you're trying to turn a pair of arrays into a dictionary, regardless of the type of the array (provided, of course, that the type of the key array element is hashable).看起来您正在尝试将一对 arrays 转换为字典,而不管数组的类型如何(当然,前提是键数组元素的类型是可散列的)。 Here is one way:
这是一种方法:
let k : [Int] = [1,2,3]
let v : [String] = ["one", "two", "three"]
func pair<Key, Value>(keyArray keys:[Key], valueArray values:[Value]) -> Dictionary<Key,Value> where Key:Hashable {
zip(keys,values).reduce(into: Dictionary<Key,Value>()) {
(dict, tuple) in dict[tuple.0] = tuple.1
}
}
let result = pair(keyArray: k, valueArray: v)
print(result) // [1: "one", 2: "two", 3: "three"], in some order
Found a solution that works for me:找到了一个适合我的解决方案:
var db = try DataBaseSqlite(dbfile: "test.db");
try db.select(sql: "int_test, real_test, text_test from stest");
var it = db.valueByKey(key: "int_test");
var rt = db.valueByKey(key: "real_test");
var tt = db.valueByKey(key: "text_test");
let it_tt_ar = db.pair(keys: "int_test", values: "text_test", kt: Int32.self);
let tt_it_ar = db.pair(keys: "text_test", values: "int_test", kt: String.self);
try db.close();
func _pair<K : Hashable, V>(keys: [K], values: [V]) -> Dictionary<K,V> {
var result = Dictionary<K, V>();
for i in 0...(keys.count - 1) {
result[keys[i]] = values[i];
}
return result;
}
func pair<T>(keys: String?, values: String?, kt: T.Type) -> Dictionary<T,Any> {
if let _keys = keys, let _values = values {
let result = _pair(keys: hashtable[_keys] as! [T], values: hashtable[_values]!);
return result;
} else {
return [:];
}
}
Due to lack of supporting a real hashtable in Swift (like c# does), my hashtable is just an Dictionary of <String,Array> which is automatically built up by the select method.由于 Swift 中缺乏真正的哈希表支持(就像 c# 一样),我的哈希表只是 <String,Array> 的字典,它是由 select 方法自动构建的。 So from an application point of view I can write a more efficient and generic code to query sqlite databases.
因此,从应用程序的角度来看,我可以编写更高效和通用的代码来查询 sqlite 数据库。
dbValueByKey() returns a typed (requested) Array of the column values and pair() returns just a combination of two columns. dbValueByKey() 返回列值的类型化(请求)数组,而 pair() 仅返回两列的组合。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.