Plist with different dictionaries to a struct
I have a plist
which is an array of dictionaries. (root is array).
Each dic have 2 values for sure : name
, and icon
as Strings
.
But some of them have more keys/values, some don't.
I am trying to read the array into a structure with :
struct Config: Decodable
private enum CodingKeys: String, CodingKey
case name, icon
let name: String
let icon: String
func functionsStruct() -> Config
let url = Bundle.main.url(forResource: "FunctionsList", withExtension: "plist")!
let data = try! Data(contentsOf: url)
let decoder = PropertyListDecoder()
return try! decoder.decode(Config.self, from: data)
I read a dictionary, how to read an array of dictionaries and how to set up the structure.
how to solve it that some dics have keys that others don't ?
swift dictionary struct plist
add a comment |
I have a plist
which is an array of dictionaries. (root is array).
Each dic have 2 values for sure : name
, and icon
as Strings
.
But some of them have more keys/values, some don't.
I am trying to read the array into a structure with :
struct Config: Decodable
private enum CodingKeys: String, CodingKey
case name, icon
let name: String
let icon: String
func functionsStruct() -> Config
let url = Bundle.main.url(forResource: "FunctionsList", withExtension: "plist")!
let data = try! Data(contentsOf: url)
let decoder = PropertyListDecoder()
return try! decoder.decode(Config.self, from: data)
I read a dictionary, how to read an array of dictionaries and how to set up the structure.
how to solve it that some dics have keys that others don't ?
swift dictionary struct plist
You could make those keys optionals. For examplelet someProperty: String?
.
– Mike Taverne
Nov 14 '18 at 6:25
thanks, the question is how to save array of dictionaries and not an array. can't figure it out.
– Curnelious
Nov 14 '18 at 6:31
2
Decoding[Config].self
won't work?
– Cristik
Nov 14 '18 at 6:47
add a comment |
I have a plist
which is an array of dictionaries. (root is array).
Each dic have 2 values for sure : name
, and icon
as Strings
.
But some of them have more keys/values, some don't.
I am trying to read the array into a structure with :
struct Config: Decodable
private enum CodingKeys: String, CodingKey
case name, icon
let name: String
let icon: String
func functionsStruct() -> Config
let url = Bundle.main.url(forResource: "FunctionsList", withExtension: "plist")!
let data = try! Data(contentsOf: url)
let decoder = PropertyListDecoder()
return try! decoder.decode(Config.self, from: data)
I read a dictionary, how to read an array of dictionaries and how to set up the structure.
how to solve it that some dics have keys that others don't ?
swift dictionary struct plist
I have a plist
which is an array of dictionaries. (root is array).
Each dic have 2 values for sure : name
, and icon
as Strings
.
But some of them have more keys/values, some don't.
I am trying to read the array into a structure with :
struct Config: Decodable
private enum CodingKeys: String, CodingKey
case name, icon
let name: String
let icon: String
func functionsStruct() -> Config
let url = Bundle.main.url(forResource: "FunctionsList", withExtension: "plist")!
let data = try! Data(contentsOf: url)
let decoder = PropertyListDecoder()
return try! decoder.decode(Config.self, from: data)
I read a dictionary, how to read an array of dictionaries and how to set up the structure.
how to solve it that some dics have keys that others don't ?
swift dictionary struct plist
swift dictionary struct plist
asked Nov 14 '18 at 5:50
CurneliousCurnelious
3,879751103
3,879751103
You could make those keys optionals. For examplelet someProperty: String?
.
– Mike Taverne
Nov 14 '18 at 6:25
thanks, the question is how to save array of dictionaries and not an array. can't figure it out.
– Curnelious
Nov 14 '18 at 6:31
2
Decoding[Config].self
won't work?
– Cristik
Nov 14 '18 at 6:47
add a comment |
You could make those keys optionals. For examplelet someProperty: String?
.
– Mike Taverne
Nov 14 '18 at 6:25
thanks, the question is how to save array of dictionaries and not an array. can't figure it out.
– Curnelious
Nov 14 '18 at 6:31
2
Decoding[Config].self
won't work?
– Cristik
Nov 14 '18 at 6:47
You could make those keys optionals. For example
let someProperty: String?
.– Mike Taverne
Nov 14 '18 at 6:25
You could make those keys optionals. For example
let someProperty: String?
.– Mike Taverne
Nov 14 '18 at 6:25
thanks, the question is how to save array of dictionaries and not an array. can't figure it out.
– Curnelious
Nov 14 '18 at 6:31
thanks, the question is how to save array of dictionaries and not an array. can't figure it out.
– Curnelious
Nov 14 '18 at 6:31
2
2
Decoding
[Config].self
won't work?– Cristik
Nov 14 '18 at 6:47
Decoding
[Config].self
won't work?– Cristik
Nov 14 '18 at 6:47
add a comment |
2 Answers
2
active
oldest
votes
For your first question :
I read a dictionary, how to read an array of dictionaries and how to set up the structure.
func functionsStruct() -> [Config]
let url = Bundle.main.url(forResource: "FunctionsList", withExtension: "plist")!
let data = try! Data(contentsOf: url)
let decoder = PropertyListDecoder()
return try! decoder.decode([Config].self, from: data)
As simple as that!!!
add a comment |
If you want an array of dictionaries, then you don't need to create your own Codable
class. Arrays and dictionaries already implements Codable
.
Just do:
func functionsStruct() -> [[String: String]]
let url = Bundle.main.url(forResource: "FunctionsList", withExtension: "plist")!
let data = try! Data(contentsOf: url)
let decoder = PropertyListDecoder()
return try! decoder.decode([[String: String]].self, from: data)
Or use [[String: Any]]
if the dictionary contains values other than strings.
And then you can access your name
and icon
like this:
let dicts = functionsStruct()
print(dicts[0]["name"])
However, I don't understand why you insist on using a dictionary. I strongly recommend you to stick with a Codable
struct. You can make optional keys optional:
struct Config: Decodable
let name: String
let icon: String
let optionalKey1: String?
let optionalKey2: String?
Their values will be nil if the key does not exist in the plist.
And you need to decode an array of Config
, instead of just Config
like you did:
return try! decoder.decode([Config].self, from: data)
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%2f53293915%2fplist-with-different-dictionaries-to-a-struct%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
For your first question :
I read a dictionary, how to read an array of dictionaries and how to set up the structure.
func functionsStruct() -> [Config]
let url = Bundle.main.url(forResource: "FunctionsList", withExtension: "plist")!
let data = try! Data(contentsOf: url)
let decoder = PropertyListDecoder()
return try! decoder.decode([Config].self, from: data)
As simple as that!!!
add a comment |
For your first question :
I read a dictionary, how to read an array of dictionaries and how to set up the structure.
func functionsStruct() -> [Config]
let url = Bundle.main.url(forResource: "FunctionsList", withExtension: "plist")!
let data = try! Data(contentsOf: url)
let decoder = PropertyListDecoder()
return try! decoder.decode([Config].self, from: data)
As simple as that!!!
add a comment |
For your first question :
I read a dictionary, how to read an array of dictionaries and how to set up the structure.
func functionsStruct() -> [Config]
let url = Bundle.main.url(forResource: "FunctionsList", withExtension: "plist")!
let data = try! Data(contentsOf: url)
let decoder = PropertyListDecoder()
return try! decoder.decode([Config].self, from: data)
As simple as that!!!
For your first question :
I read a dictionary, how to read an array of dictionaries and how to set up the structure.
func functionsStruct() -> [Config]
let url = Bundle.main.url(forResource: "FunctionsList", withExtension: "plist")!
let data = try! Data(contentsOf: url)
let decoder = PropertyListDecoder()
return try! decoder.decode([Config].self, from: data)
As simple as that!!!
edited Nov 14 '18 at 7:04
answered Nov 14 '18 at 6:53
guruguru
1,2291124
1,2291124
add a comment |
add a comment |
If you want an array of dictionaries, then you don't need to create your own Codable
class. Arrays and dictionaries already implements Codable
.
Just do:
func functionsStruct() -> [[String: String]]
let url = Bundle.main.url(forResource: "FunctionsList", withExtension: "plist")!
let data = try! Data(contentsOf: url)
let decoder = PropertyListDecoder()
return try! decoder.decode([[String: String]].self, from: data)
Or use [[String: Any]]
if the dictionary contains values other than strings.
And then you can access your name
and icon
like this:
let dicts = functionsStruct()
print(dicts[0]["name"])
However, I don't understand why you insist on using a dictionary. I strongly recommend you to stick with a Codable
struct. You can make optional keys optional:
struct Config: Decodable
let name: String
let icon: String
let optionalKey1: String?
let optionalKey2: String?
Their values will be nil if the key does not exist in the plist.
And you need to decode an array of Config
, instead of just Config
like you did:
return try! decoder.decode([Config].self, from: data)
add a comment |
If you want an array of dictionaries, then you don't need to create your own Codable
class. Arrays and dictionaries already implements Codable
.
Just do:
func functionsStruct() -> [[String: String]]
let url = Bundle.main.url(forResource: "FunctionsList", withExtension: "plist")!
let data = try! Data(contentsOf: url)
let decoder = PropertyListDecoder()
return try! decoder.decode([[String: String]].self, from: data)
Or use [[String: Any]]
if the dictionary contains values other than strings.
And then you can access your name
and icon
like this:
let dicts = functionsStruct()
print(dicts[0]["name"])
However, I don't understand why you insist on using a dictionary. I strongly recommend you to stick with a Codable
struct. You can make optional keys optional:
struct Config: Decodable
let name: String
let icon: String
let optionalKey1: String?
let optionalKey2: String?
Their values will be nil if the key does not exist in the plist.
And you need to decode an array of Config
, instead of just Config
like you did:
return try! decoder.decode([Config].self, from: data)
add a comment |
If you want an array of dictionaries, then you don't need to create your own Codable
class. Arrays and dictionaries already implements Codable
.
Just do:
func functionsStruct() -> [[String: String]]
let url = Bundle.main.url(forResource: "FunctionsList", withExtension: "plist")!
let data = try! Data(contentsOf: url)
let decoder = PropertyListDecoder()
return try! decoder.decode([[String: String]].self, from: data)
Or use [[String: Any]]
if the dictionary contains values other than strings.
And then you can access your name
and icon
like this:
let dicts = functionsStruct()
print(dicts[0]["name"])
However, I don't understand why you insist on using a dictionary. I strongly recommend you to stick with a Codable
struct. You can make optional keys optional:
struct Config: Decodable
let name: String
let icon: String
let optionalKey1: String?
let optionalKey2: String?
Their values will be nil if the key does not exist in the plist.
And you need to decode an array of Config
, instead of just Config
like you did:
return try! decoder.decode([Config].self, from: data)
If you want an array of dictionaries, then you don't need to create your own Codable
class. Arrays and dictionaries already implements Codable
.
Just do:
func functionsStruct() -> [[String: String]]
let url = Bundle.main.url(forResource: "FunctionsList", withExtension: "plist")!
let data = try! Data(contentsOf: url)
let decoder = PropertyListDecoder()
return try! decoder.decode([[String: String]].self, from: data)
Or use [[String: Any]]
if the dictionary contains values other than strings.
And then you can access your name
and icon
like this:
let dicts = functionsStruct()
print(dicts[0]["name"])
However, I don't understand why you insist on using a dictionary. I strongly recommend you to stick with a Codable
struct. You can make optional keys optional:
struct Config: Decodable
let name: String
let icon: String
let optionalKey1: String?
let optionalKey2: String?
Their values will be nil if the key does not exist in the plist.
And you need to decode an array of Config
, instead of just Config
like you did:
return try! decoder.decode([Config].self, from: data)
answered Nov 14 '18 at 6:46
SweeperSweeper
69.1k1074140
69.1k1074140
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%2f53293915%2fplist-with-different-dictionaries-to-a-struct%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
You could make those keys optionals. For example
let someProperty: String?
.– Mike Taverne
Nov 14 '18 at 6:25
thanks, the question is how to save array of dictionaries and not an array. can't figure it out.
– Curnelious
Nov 14 '18 at 6:31
2
Decoding
[Config].self
won't work?– Cristik
Nov 14 '18 at 6:47