Error: value of type 'Optional<NSMutableArray>' has no subscripts Array[0] in Swift iOS
Error: value of type 'Optional<NSMutableArray>' has no subscripts Array[0] in Swift iOS
我正在从我的 Objective C classes 中获取一些数据。这是我的 'dataArray' 数据:
Optional(<__NSArrayM 0x2818cff60>(
{
date = 1574164423;
shakeState = 1;
},
{
date = 1574164431;
shakeState = 1;
}
)
)
我创建了一个包含日期和 shakeState 值的模态 class 'ShakeInfo'。
我只想将此数组转换为 'ShakeInfo' 对象的数组。我的问题是当我尝试打印 'dataArray[0]' 时出现错误:
error: <EXPR>:3:1: error: value of type 'Optional<NSMutableArray>' has no subscripts
dataArray[0]
我怎样才能明智地读取这个数组值索引。请多多指教。
已编辑:
这是我获取 dataArray 后的代码:
do {
let data = try JSONSerialization.data(withJSONObject: dataArray!)
let responseStr = String(data: data, encoding: .utf8)!
print(responseStr)//[{"date":"1574164424","shakeState":1},{"date":"1574164430","shakeState":1}]
var shakeInfoDetails = [ShakeInfo]()
//how to add 'responseStr' value in the shakeInfoDetails Modal Array
} catch {
print("JSON serialization failed: ", error)
}
let data = #"[{"date":"1574164424","shakeState":1},{"date":"1574164430","shakeState":1}]"#.data(using: .utf8)!
//Model
struct ModelRecord : Codable {
var date : String
var shakeState : Int
}
class Model {
var date : Date
var shakeState : Int
init?(record: ModelRecord) {
guard let secondsFrom1970 = Double(record.date) else {
return nil
}
date = Date(timeIntervalSince1970: secondsFrom1970)
shakeState = record.shakeState
}
}
//Decoding
let decoder = JSONDecoder()
do {
let records = try decoder.decode([ModelRecord].self, from: data)
let models = records.compactMap { Model(record: [=10=]) }
}
catch {
print("Decoding Error: \(error)")
}
原问题的答案:
let a1 : NSMutableArray? = NSMutableArray(array: ["A", "B", "C"])
a1?.object(at: 2)
请阅读以下内容:
- Swift
中的可选项
- 可编码 - https://developer.apple.com/documentation/foundation/archives_and_serialization/encoding_and_decoding_custom_types
- Swift 标准库中的数组
- 在 Foundation 中定义的 NSMutableArray
我正在从我的 Objective C classes 中获取一些数据。这是我的 'dataArray' 数据:
Optional(<__NSArrayM 0x2818cff60>(
{
date = 1574164423;
shakeState = 1;
},
{
date = 1574164431;
shakeState = 1;
}
)
)
我创建了一个包含日期和 shakeState 值的模态 class 'ShakeInfo'。
我只想将此数组转换为 'ShakeInfo' 对象的数组。我的问题是当我尝试打印 'dataArray[0]' 时出现错误:
error: <EXPR>:3:1: error: value of type 'Optional<NSMutableArray>' has no subscripts
dataArray[0]
我怎样才能明智地读取这个数组值索引。请多多指教。
已编辑:
这是我获取 dataArray 后的代码:
do {
let data = try JSONSerialization.data(withJSONObject: dataArray!)
let responseStr = String(data: data, encoding: .utf8)!
print(responseStr)//[{"date":"1574164424","shakeState":1},{"date":"1574164430","shakeState":1}]
var shakeInfoDetails = [ShakeInfo]()
//how to add 'responseStr' value in the shakeInfoDetails Modal Array
} catch {
print("JSON serialization failed: ", error)
}
let data = #"[{"date":"1574164424","shakeState":1},{"date":"1574164430","shakeState":1}]"#.data(using: .utf8)!
//Model
struct ModelRecord : Codable {
var date : String
var shakeState : Int
}
class Model {
var date : Date
var shakeState : Int
init?(record: ModelRecord) {
guard let secondsFrom1970 = Double(record.date) else {
return nil
}
date = Date(timeIntervalSince1970: secondsFrom1970)
shakeState = record.shakeState
}
}
//Decoding
let decoder = JSONDecoder()
do {
let records = try decoder.decode([ModelRecord].self, from: data)
let models = records.compactMap { Model(record: [=10=]) }
}
catch {
print("Decoding Error: \(error)")
}
原问题的答案:
let a1 : NSMutableArray? = NSMutableArray(array: ["A", "B", "C"])
a1?.object(at: 2)
请阅读以下内容:
- Swift 中的可选项
- 可编码 - https://developer.apple.com/documentation/foundation/archives_and_serialization/encoding_and_decoding_custom_types
- Swift 标准库中的数组
- 在 Foundation 中定义的 NSMutableArray