我正在将我的一个项目从Obj-C切换到Swift,我在从NSDictionary / NSMutableArray切换到Dictionary / Array时遇到了问题。因为我抓住了" key"的价值。我假设它会像Obj-C那样返回它指向的相同数组,但事实并非如此。我写了一个小样本来复制这个bug。一个正确方向的提示将非常感激。提前致谢
var test:[ [String:[String]] ]
init() {
test = [ [String:[String]] ]()
var first:[String] = _arrayForKey("key")
first.append("first")
var second:[String] = _arrayForKey("key")
second.append("second")
var third:[String] = _arrayForKey("key")
third.append("third")
var fourth:[String] = _arrayForKey("key")
fourth.append("fourth")
println(work)
}
private func _arrayForKey(key:String) -> [String] {
for dict in test {
if dict.keys.first! == key {
return dict[key]!
}
}
test.append([key:[String]()])
var dict:[String:[String]] = test.last!
println("MAKING A NEW DICTIONARY \(test)")
return dict[key]!
}
当它运行时它退出["第四个"],我正在寻找的是["第一个","第二个",&#34 ;第三","第四"]
答案 0 :(得分:0)
这不是一个错误。
与Objective-C不同,NSArray和NSDictionary是引用类型,Swift中的Array和Dictionary是值类型,例如
if directory.endswith('.zip'):
print('Currently downloading:', url)
self.set_header('Content-Type', 'application/octet-stream')
self.set_header("Content-Description", "File Transfer")
self.set_header('Content-Disposition', 'attachment; filename {}'.format(directory))
with open(url, 'r') as f:
try:
while True:
data = f.read(4096)
if not data:
break
self.write(data)
self.finish()
except Exception as exc:
self.write(json_encode({'data': exc}))