Initialising model object without optionals
The model object is populated using JSONDecoder method. Since it is needed to pass it on tableview i need to initialise a local variable of type KBArticle on UI side. Is there any possible method of initialising an object of the KBArticle without optionals or accessing the inner array directly ?
var kbArticle = KBArticle()
Missing argument for parameter 'from' in call. Insert 'from: <#Decoder#>'
/// UI Side
var kbArticle = KBArticle()
override func viewDidLoad() {
super.viewDidLoad()
/// Web services call method
SDKCore.getInstance.getKbService().fetchKbArticles(with: topicID) { (result) in
switch result {
case .success(let kbArticle):
self.kbArticle = kbArticle
DispatchQueue.main.async {
self.tableView.reloadData()
}
case .failed(let e):
print("Error=(e)")
}
}
}
// Model Class in SDK
public struct KBArticle: Codable {
public let id: Int
public let topic: String
public let articleCount: Int
public let articles: [Article]
}
public struct Article: Codable {
public let id: Int
public let subject: String
}
/// Method in SDK
public func fetchKbArticles(with topicId: Int, completionHandler: @escaping (ResultModel<KBArticle, Error>) -> Void) {
let request = GetKBArticles(topicId: topicId)
Networking.shared.performRequest(request) { (response) in
switch response {
case .success(let response):
do {
let decoder = JSONDecoder()
let result = try decoder.decode(KBArticle.self, from: response.data!)
completionHandler(.success(result))
} catch let error {
completionHandler(.failed(error))
}
case .failed(let error):
completionHandler(.failed(error))
}
}
}
I have to use this articles
inside kbArticle
struct to provide my tableview datasource. Is is possible to initialise kbArticle
object without a primary initialisation using nil values???
swift model initialization jsondecoder
add a comment |
The model object is populated using JSONDecoder method. Since it is needed to pass it on tableview i need to initialise a local variable of type KBArticle on UI side. Is there any possible method of initialising an object of the KBArticle without optionals or accessing the inner array directly ?
var kbArticle = KBArticle()
Missing argument for parameter 'from' in call. Insert 'from: <#Decoder#>'
/// UI Side
var kbArticle = KBArticle()
override func viewDidLoad() {
super.viewDidLoad()
/// Web services call method
SDKCore.getInstance.getKbService().fetchKbArticles(with: topicID) { (result) in
switch result {
case .success(let kbArticle):
self.kbArticle = kbArticle
DispatchQueue.main.async {
self.tableView.reloadData()
}
case .failed(let e):
print("Error=(e)")
}
}
}
// Model Class in SDK
public struct KBArticle: Codable {
public let id: Int
public let topic: String
public let articleCount: Int
public let articles: [Article]
}
public struct Article: Codable {
public let id: Int
public let subject: String
}
/// Method in SDK
public func fetchKbArticles(with topicId: Int, completionHandler: @escaping (ResultModel<KBArticle, Error>) -> Void) {
let request = GetKBArticles(topicId: topicId)
Networking.shared.performRequest(request) { (response) in
switch response {
case .success(let response):
do {
let decoder = JSONDecoder()
let result = try decoder.decode(KBArticle.self, from: response.data!)
completionHandler(.success(result))
} catch let error {
completionHandler(.failed(error))
}
case .failed(let error):
completionHandler(.failed(error))
}
}
}
I have to use this articles
inside kbArticle
struct to provide my tableview datasource. Is is possible to initialise kbArticle
object without a primary initialisation using nil values???
swift model initialization jsondecoder
see this stackoverflow.com/a/52116454/9315978
– a.masri
Jan 1 at 13:46
or you can set default value for all params
– a.masri
Jan 1 at 13:47
add a comment |
The model object is populated using JSONDecoder method. Since it is needed to pass it on tableview i need to initialise a local variable of type KBArticle on UI side. Is there any possible method of initialising an object of the KBArticle without optionals or accessing the inner array directly ?
var kbArticle = KBArticle()
Missing argument for parameter 'from' in call. Insert 'from: <#Decoder#>'
/// UI Side
var kbArticle = KBArticle()
override func viewDidLoad() {
super.viewDidLoad()
/// Web services call method
SDKCore.getInstance.getKbService().fetchKbArticles(with: topicID) { (result) in
switch result {
case .success(let kbArticle):
self.kbArticle = kbArticle
DispatchQueue.main.async {
self.tableView.reloadData()
}
case .failed(let e):
print("Error=(e)")
}
}
}
// Model Class in SDK
public struct KBArticle: Codable {
public let id: Int
public let topic: String
public let articleCount: Int
public let articles: [Article]
}
public struct Article: Codable {
public let id: Int
public let subject: String
}
/// Method in SDK
public func fetchKbArticles(with topicId: Int, completionHandler: @escaping (ResultModel<KBArticle, Error>) -> Void) {
let request = GetKBArticles(topicId: topicId)
Networking.shared.performRequest(request) { (response) in
switch response {
case .success(let response):
do {
let decoder = JSONDecoder()
let result = try decoder.decode(KBArticle.self, from: response.data!)
completionHandler(.success(result))
} catch let error {
completionHandler(.failed(error))
}
case .failed(let error):
completionHandler(.failed(error))
}
}
}
I have to use this articles
inside kbArticle
struct to provide my tableview datasource. Is is possible to initialise kbArticle
object without a primary initialisation using nil values???
swift model initialization jsondecoder
The model object is populated using JSONDecoder method. Since it is needed to pass it on tableview i need to initialise a local variable of type KBArticle on UI side. Is there any possible method of initialising an object of the KBArticle without optionals or accessing the inner array directly ?
var kbArticle = KBArticle()
Missing argument for parameter 'from' in call. Insert 'from: <#Decoder#>'
/// UI Side
var kbArticle = KBArticle()
override func viewDidLoad() {
super.viewDidLoad()
/// Web services call method
SDKCore.getInstance.getKbService().fetchKbArticles(with: topicID) { (result) in
switch result {
case .success(let kbArticle):
self.kbArticle = kbArticle
DispatchQueue.main.async {
self.tableView.reloadData()
}
case .failed(let e):
print("Error=(e)")
}
}
}
// Model Class in SDK
public struct KBArticle: Codable {
public let id: Int
public let topic: String
public let articleCount: Int
public let articles: [Article]
}
public struct Article: Codable {
public let id: Int
public let subject: String
}
/// Method in SDK
public func fetchKbArticles(with topicId: Int, completionHandler: @escaping (ResultModel<KBArticle, Error>) -> Void) {
let request = GetKBArticles(topicId: topicId)
Networking.shared.performRequest(request) { (response) in
switch response {
case .success(let response):
do {
let decoder = JSONDecoder()
let result = try decoder.decode(KBArticle.self, from: response.data!)
completionHandler(.success(result))
} catch let error {
completionHandler(.failed(error))
}
case .failed(let error):
completionHandler(.failed(error))
}
}
}
I have to use this articles
inside kbArticle
struct to provide my tableview datasource. Is is possible to initialise kbArticle
object without a primary initialisation using nil values???
swift model initialization jsondecoder
swift model initialization jsondecoder
edited Jan 24 at 7:05
Vaisakh KP
asked Jan 1 at 12:50
Vaisakh KPVaisakh KP
302318
302318
see this stackoverflow.com/a/52116454/9315978
– a.masri
Jan 1 at 13:46
or you can set default value for all params
– a.masri
Jan 1 at 13:47
add a comment |
see this stackoverflow.com/a/52116454/9315978
– a.masri
Jan 1 at 13:46
or you can set default value for all params
– a.masri
Jan 1 at 13:47
see this stackoverflow.com/a/52116454/9315978
– a.masri
Jan 1 at 13:46
see this stackoverflow.com/a/52116454/9315978
– a.masri
Jan 1 at 13:46
or you can set default value for all params
– a.masri
Jan 1 at 13:47
or you can set default value for all params
– a.masri
Jan 1 at 13:47
add a comment |
1 Answer
1
active
oldest
votes
If you need new object without optional , you can set a default value for all params
ex :
public struct KBArticle: Codable {
public var id: Int = 0
public var topic: String = ""
public var articleCount: Int = 0
public var articles: [Article]?
}
public struct Article: Codable {
public var id: Int = 0
public var subject: String = ""
}
Use: let myObj = KBArticle()
Please check this Answer
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53995580%2finitialising-model-object-without-optionals%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you need new object without optional , you can set a default value for all params
ex :
public struct KBArticle: Codable {
public var id: Int = 0
public var topic: String = ""
public var articleCount: Int = 0
public var articles: [Article]?
}
public struct Article: Codable {
public var id: Int = 0
public var subject: String = ""
}
Use: let myObj = KBArticle()
Please check this Answer
add a comment |
If you need new object without optional , you can set a default value for all params
ex :
public struct KBArticle: Codable {
public var id: Int = 0
public var topic: String = ""
public var articleCount: Int = 0
public var articles: [Article]?
}
public struct Article: Codable {
public var id: Int = 0
public var subject: String = ""
}
Use: let myObj = KBArticle()
Please check this Answer
add a comment |
If you need new object without optional , you can set a default value for all params
ex :
public struct KBArticle: Codable {
public var id: Int = 0
public var topic: String = ""
public var articleCount: Int = 0
public var articles: [Article]?
}
public struct Article: Codable {
public var id: Int = 0
public var subject: String = ""
}
Use: let myObj = KBArticle()
Please check this Answer
If you need new object without optional , you can set a default value for all params
ex :
public struct KBArticle: Codable {
public var id: Int = 0
public var topic: String = ""
public var articleCount: Int = 0
public var articles: [Article]?
}
public struct Article: Codable {
public var id: Int = 0
public var subject: String = ""
}
Use: let myObj = KBArticle()
Please check this Answer
answered Jan 1 at 13:52


a.masria.masri
1,7531721
1,7531721
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53995580%2finitialising-model-object-without-optionals%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
see this stackoverflow.com/a/52116454/9315978
– a.masri
Jan 1 at 13:46
or you can set default value for all params
– a.masri
Jan 1 at 13:47