简体   繁体   English

如何使用 Swift iOS 中的 Struct Modal class 中的虚拟值创建数组

[英]How to make an array using dummy values from Struct Modal class in Swift iOS

I am getting a JSON from my server.我从我的服务器获得了 JSON。 To parse the JSON, I am using Decoder and converting the data in my Modal Class object.为了解析 JSON,我使用解码器并转换模态 Class object 中的数据。

This is my Modal class for Decoder:这是我的解码器模态 class:

struct SyncModelRecord : Codable {

    var date : String
    var shakeState : Int
}

class SyncModel {

    var date : Date
    var shakeState : Int

    init?(record: SyncModelRecord) {

        guard let secondsFrom1970 = Double(record.date) else {
            return nil
        }

        date = Date(timeIntervalSince1970: secondsFrom1970)

        shakeState = record.shakeState
    }
}

My parsing is working fine.我的解析工作正常。

My problem is that now I have to make a 'SyncModelRecord' dummy array like:我的问题是现在我必须制作一个“SyncModelRecord”虚拟数组,例如:

var dummySyncModelRecordArray = [SyncModelRecord]()

var syncModelRecord : SyncModelRecord?
syncModelRecord?.shakeState = 0
syncModelRecord?.date = String(Int64(syncTimestamp!))

dummySyncModelRecordArray.append(syncModelRecord!)

But this is not working.但这不起作用。 Its crashing with Error (Please see the below attached error screenshot).它因错误而崩溃(请参阅下面附加的错误屏幕截图)。

Please advise me.请给我提意见。

在此处输入图像描述

You're force unwrapping a value that is nil syncModelRecord .强制解开一个值为 nil syncModelRecord的值。

You need to instantiate it when declaring it.声明时需要实例化它。

var syncModelRecord: SyncModelRecord? = SyncModelRecord()

Or, even better, don't declare it as an optional, since you know at that point that it will not be nil (if instantiated correctly, of course):或者,更好的是,不要将其声明为可选项,因为此时您知道它不会为nil (当然,如果实例化正确):

var syncModelRecord = SyncModelRecord()

You need to actually initialize syncModelRecord .您需要实际初始化syncModelRecord Try this:尝试这个:

let syncModelRecord = SyncModelRecord(date: String(Int64(syncTimestamp!)), shakeState: 0)

dummySyncModelRecordArray.append(syncModelRecord!)

Another tip maybe worth exploring, you can directly decode a date and specify the decoder's strategy for it (in your case secondsSince1970 )另一个提示可能值得探索,您可以直接解码日期并为其指定解码器的策略(在您的情况下secondsSince1970

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不使用情节提要的情况下在iOS Swift 3中以编程方式创建UICollectionView类? - How to make UICollectionView class programmatically in iOS Swift 3 without using storyboard? How to create a model class and access the values from JSON response using Codable in ios swift? - How to create a model class and access the values from JSON response using Codable in ios swift? 如何快速从结构检索值 - How to retrieve values from struct in swift iOS如何从数组Swift获取更多然后100个值 - iOS How to get more then 100 values from array Swift Append 使用 Swift 将结构转换为数组(在 iOS 中失败,但在 Playgrounds 中失败) - Append a Struct to an Array using Swift (fails in iOS but not in Playgrounds) "如何在单独的类中在 iOS Swift 中制作计时器?" - How to make a timer in iOS Swift in a separate class? Swift:结构数组与类数组 - Swift: Struct Array vs Class Array 如何在Swift的类级别创建一个UIImageViews数组? - How to make an array of UIImageViews at class level in Swift? 使用 struct Array 实现 Swift/iOS searchBar 时遇到问题 - Trouble implementing Swift/iOS searchBar with struct Array Create a model class and access the values from JSON response using Codable in ios swift? - Create a model class and access the values from JSON response using Codable in ios swift?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM