我不确定我是否正在使用字典或数据对象或两者都不正确.我试图习惯切换到快速但我有点麻烦.
var dictionaryExample : [String:AnyObject] =
["user":"UserName",
"pass":"password",
"token":"0123456789",
"image":0] // image should be either NSData or empty
let dataExample : NSData = dictionaryExample as NSData
Run Code Online (Sandbox Code Playgroud)
我需要NSDictionary编码到一个NSData对象以及获取该NSData对象并将其解码为NSDictionary.
非常感谢任何帮助,谢谢.
Swift 实现的Measurement类型可用于在所需的指标中保存速度、高度等值。
例如:
// Going to explicitly type here so the question is clear
var speed: Measurement<UnitSpeed> = Measurement(value: 10, unit: UnitSpeed.metersPerSecond)
var altitude: Measurement<UnitLength> = Measurement(value: 500, unit: UnitLength.meters)
altitude.convert(to: UnitLength.feet)
print("500 meters is roughly \(Int(altitude.value)) feet")
// Prints: 500 meters is roughly 1640 feet
Run Code Online (Sandbox Code Playgroud)
然而,即使UnitLength和UnitSpeed都是 的子类Unit,使用这些类型的度量不能多态地传递到 swift 中的函数中。
例如,如果您传入类型为Measurement<UnitLength>或的对象,则以下函数不起作用Measurement<UnitSpeed>。
func formatMeasurementAsString(measurement: Measurement<Unit>) -> String {
return "\(Int(measurement.value)) \(measurement.unit.symbol)"
}
Run Code Online (Sandbox Code Playgroud)
将上面定义的speed或传递altitude到此函数中会产生以下错误:Cannot convert …