Swift Codable protocol with Strings and UIImages
I am trying to save data via a codable protocol. It is an array of structures with structs inside. There are strings, images and bool values and I am thinking it is one of these data types that does not conform to the protocol.
Here is a pic of all the data I must save:
original
struct Checklist
var name: String
var image: UIImage?
var items: [Item] =
struct Item
var nameOfNote: String
var remind: Bool
//var date: Date
struct alldata: Codable
var checklists: [Checklist] =
I have tried to include the protocol in all the structs but it also produced an error. Here's a pic:
tried a solution picture
swift protocols decode encode codable
add a comment |
I am trying to save data via a codable protocol. It is an array of structures with structs inside. There are strings, images and bool values and I am thinking it is one of these data types that does not conform to the protocol.
Here is a pic of all the data I must save:
original
struct Checklist
var name: String
var image: UIImage?
var items: [Item] =
struct Item
var nameOfNote: String
var remind: Bool
//var date: Date
struct alldata: Codable
var checklists: [Checklist] =
I have tried to include the protocol in all the structs but it also produced an error. Here's a pic:
tried a solution picture
swift protocols decode encode codable
Not tried, but I doubtUIImage
isCodable
. What I've done instead, is customised the coding and decoding process so that theUIImage
is converted to a base64String
instead
– MadProgrammer
Nov 11 '18 at 21:34
I might also point out that neitherCheckList
orItem
areCodable
. You should also post text/code rather then images ;)
– MadProgrammer
Nov 11 '18 at 21:35
MadProgrammer is right.UIImage
is notCodable
compliant.
– vadian
Nov 11 '18 at 21:50
how do you think i can make UIImage codable?
– Antony D
Nov 11 '18 at 21:53
@AntonyD JSON is a text based representation of the data, it doesn't not support binary data. However, this is neither a new problem or an uncommon one. The most common solution is to convert the binary data to a base 64 (text) representation. There are plenty of examples floating around. You then need to customise the encoding and decoding process for yourstruct
so you can take control over how the image is encoded and decoded, again, not a uncommon or new problem - and well supported in Swift
– MadProgrammer
Nov 11 '18 at 21:58
add a comment |
I am trying to save data via a codable protocol. It is an array of structures with structs inside. There are strings, images and bool values and I am thinking it is one of these data types that does not conform to the protocol.
Here is a pic of all the data I must save:
original
struct Checklist
var name: String
var image: UIImage?
var items: [Item] =
struct Item
var nameOfNote: String
var remind: Bool
//var date: Date
struct alldata: Codable
var checklists: [Checklist] =
I have tried to include the protocol in all the structs but it also produced an error. Here's a pic:
tried a solution picture
swift protocols decode encode codable
I am trying to save data via a codable protocol. It is an array of structures with structs inside. There are strings, images and bool values and I am thinking it is one of these data types that does not conform to the protocol.
Here is a pic of all the data I must save:
original
struct Checklist
var name: String
var image: UIImage?
var items: [Item] =
struct Item
var nameOfNote: String
var remind: Bool
//var date: Date
struct alldata: Codable
var checklists: [Checklist] =
I have tried to include the protocol in all the structs but it also produced an error. Here's a pic:
tried a solution picture
swift protocols decode encode codable
swift protocols decode encode codable
edited Nov 11 '18 at 21:47
vadian
148k14159178
148k14159178
asked Nov 11 '18 at 21:32
Antony DAntony D
185
185
Not tried, but I doubtUIImage
isCodable
. What I've done instead, is customised the coding and decoding process so that theUIImage
is converted to a base64String
instead
– MadProgrammer
Nov 11 '18 at 21:34
I might also point out that neitherCheckList
orItem
areCodable
. You should also post text/code rather then images ;)
– MadProgrammer
Nov 11 '18 at 21:35
MadProgrammer is right.UIImage
is notCodable
compliant.
– vadian
Nov 11 '18 at 21:50
how do you think i can make UIImage codable?
– Antony D
Nov 11 '18 at 21:53
@AntonyD JSON is a text based representation of the data, it doesn't not support binary data. However, this is neither a new problem or an uncommon one. The most common solution is to convert the binary data to a base 64 (text) representation. There are plenty of examples floating around. You then need to customise the encoding and decoding process for yourstruct
so you can take control over how the image is encoded and decoded, again, not a uncommon or new problem - and well supported in Swift
– MadProgrammer
Nov 11 '18 at 21:58
add a comment |
Not tried, but I doubtUIImage
isCodable
. What I've done instead, is customised the coding and decoding process so that theUIImage
is converted to a base64String
instead
– MadProgrammer
Nov 11 '18 at 21:34
I might also point out that neitherCheckList
orItem
areCodable
. You should also post text/code rather then images ;)
– MadProgrammer
Nov 11 '18 at 21:35
MadProgrammer is right.UIImage
is notCodable
compliant.
– vadian
Nov 11 '18 at 21:50
how do you think i can make UIImage codable?
– Antony D
Nov 11 '18 at 21:53
@AntonyD JSON is a text based representation of the data, it doesn't not support binary data. However, this is neither a new problem or an uncommon one. The most common solution is to convert the binary data to a base 64 (text) representation. There are plenty of examples floating around. You then need to customise the encoding and decoding process for yourstruct
so you can take control over how the image is encoded and decoded, again, not a uncommon or new problem - and well supported in Swift
– MadProgrammer
Nov 11 '18 at 21:58
Not tried, but I doubt
UIImage
is Codable
. What I've done instead, is customised the coding and decoding process so that the UIImage
is converted to a base64 String
instead– MadProgrammer
Nov 11 '18 at 21:34
Not tried, but I doubt
UIImage
is Codable
. What I've done instead, is customised the coding and decoding process so that the UIImage
is converted to a base64 String
instead– MadProgrammer
Nov 11 '18 at 21:34
I might also point out that neither
CheckList
or Item
are Codable
. You should also post text/code rather then images ;)– MadProgrammer
Nov 11 '18 at 21:35
I might also point out that neither
CheckList
or Item
are Codable
. You should also post text/code rather then images ;)– MadProgrammer
Nov 11 '18 at 21:35
MadProgrammer is right.
UIImage
is not Codable
compliant.– vadian
Nov 11 '18 at 21:50
MadProgrammer is right.
UIImage
is not Codable
compliant.– vadian
Nov 11 '18 at 21:50
how do you think i can make UIImage codable?
– Antony D
Nov 11 '18 at 21:53
how do you think i can make UIImage codable?
– Antony D
Nov 11 '18 at 21:53
@AntonyD JSON is a text based representation of the data, it doesn't not support binary data. However, this is neither a new problem or an uncommon one. The most common solution is to convert the binary data to a base 64 (text) representation. There are plenty of examples floating around. You then need to customise the encoding and decoding process for your
struct
so you can take control over how the image is encoded and decoded, again, not a uncommon or new problem - and well supported in Swift– MadProgrammer
Nov 11 '18 at 21:58
@AntonyD JSON is a text based representation of the data, it doesn't not support binary data. However, this is neither a new problem or an uncommon one. The most common solution is to convert the binary data to a base 64 (text) representation. There are plenty of examples floating around. You then need to customise the encoding and decoding process for your
struct
so you can take control over how the image is encoded and decoded, again, not a uncommon or new problem - and well supported in Swift– MadProgrammer
Nov 11 '18 at 21:58
add a comment |
1 Answer
1
active
oldest
votes
JSON in Swift 4 is fun (repeat this at 2am in the morning ;)).
One of the first things I always do is consult a good blog, like Ultimate Guide to JSON Parsing with Swift 4. While it might not answer the "direct" question, it will provide a lot of useful information.
Another thing to do, is consult the Apple documentation. In this, a quick look at the documentation for UIImage
will point out that it does not conform to Codable
, so that's a problem.
Another issue is, JSON doesn't support binary data, it's a text based solution.
This means that you need to figure out away to convert the binary image data to text. Lucky for us, people have already thought about this and the most common mechanism is to use Base 64 encoding.
A quick google search will turn up any number of solutions for encoding/decoding a UIImage
to/from base 64, for example Convert between UIImage and Base64 string
Now, we just need to customise the encoding and decoding process.
The first thing is to make sure all the other fields/objects/data we use are also conform to Codable
...
struct Item: Codable
var nameOfNote: String
var remind: Bool
//var date: Date
Then we can set out customising the encoding and decoding process for the image, maybe something like...
struct Checklist: Codable
enum CodingKeys: String, CodingKey
case image
case name
case items
var name: String
var image: UIImage?
var items: [Item] =
init(from decoder: Decoder) throws
let container = try decoder.container(keyedBy: CodingKeys.self)
name = try container.decode(String.self, forKey: .name)
items = try container.decode([Item].self, forKey: .items)
if let text = try container.decodeIfPresent(String.self, forKey: .image)
if let data = Data(base64Encoded: text)
image = UIImage(data: data)
func encode(to encoder: Encoder) throws
var container = encoder.container(keyedBy: CodingKeys.self)
if let image = image, let data = image.pngData()
try container.encode(data, forKey: .image)
try container.encode(name, forKey: .name)
try container.encode(items, forKey: .items)
nb: This is the basic process I've used for a couple of projects, I've not tested the above myself, but it should present the basic idea you need to get started
Thanks for the help
– Antony D
Nov 11 '18 at 22:02
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%2f53253438%2fswift-codable-protocol-with-strings-and-uiimages%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
JSON in Swift 4 is fun (repeat this at 2am in the morning ;)).
One of the first things I always do is consult a good blog, like Ultimate Guide to JSON Parsing with Swift 4. While it might not answer the "direct" question, it will provide a lot of useful information.
Another thing to do, is consult the Apple documentation. In this, a quick look at the documentation for UIImage
will point out that it does not conform to Codable
, so that's a problem.
Another issue is, JSON doesn't support binary data, it's a text based solution.
This means that you need to figure out away to convert the binary image data to text. Lucky for us, people have already thought about this and the most common mechanism is to use Base 64 encoding.
A quick google search will turn up any number of solutions for encoding/decoding a UIImage
to/from base 64, for example Convert between UIImage and Base64 string
Now, we just need to customise the encoding and decoding process.
The first thing is to make sure all the other fields/objects/data we use are also conform to Codable
...
struct Item: Codable
var nameOfNote: String
var remind: Bool
//var date: Date
Then we can set out customising the encoding and decoding process for the image, maybe something like...
struct Checklist: Codable
enum CodingKeys: String, CodingKey
case image
case name
case items
var name: String
var image: UIImage?
var items: [Item] =
init(from decoder: Decoder) throws
let container = try decoder.container(keyedBy: CodingKeys.self)
name = try container.decode(String.self, forKey: .name)
items = try container.decode([Item].self, forKey: .items)
if let text = try container.decodeIfPresent(String.self, forKey: .image)
if let data = Data(base64Encoded: text)
image = UIImage(data: data)
func encode(to encoder: Encoder) throws
var container = encoder.container(keyedBy: CodingKeys.self)
if let image = image, let data = image.pngData()
try container.encode(data, forKey: .image)
try container.encode(name, forKey: .name)
try container.encode(items, forKey: .items)
nb: This is the basic process I've used for a couple of projects, I've not tested the above myself, but it should present the basic idea you need to get started
Thanks for the help
– Antony D
Nov 11 '18 at 22:02
add a comment |
JSON in Swift 4 is fun (repeat this at 2am in the morning ;)).
One of the first things I always do is consult a good blog, like Ultimate Guide to JSON Parsing with Swift 4. While it might not answer the "direct" question, it will provide a lot of useful information.
Another thing to do, is consult the Apple documentation. In this, a quick look at the documentation for UIImage
will point out that it does not conform to Codable
, so that's a problem.
Another issue is, JSON doesn't support binary data, it's a text based solution.
This means that you need to figure out away to convert the binary image data to text. Lucky for us, people have already thought about this and the most common mechanism is to use Base 64 encoding.
A quick google search will turn up any number of solutions for encoding/decoding a UIImage
to/from base 64, for example Convert between UIImage and Base64 string
Now, we just need to customise the encoding and decoding process.
The first thing is to make sure all the other fields/objects/data we use are also conform to Codable
...
struct Item: Codable
var nameOfNote: String
var remind: Bool
//var date: Date
Then we can set out customising the encoding and decoding process for the image, maybe something like...
struct Checklist: Codable
enum CodingKeys: String, CodingKey
case image
case name
case items
var name: String
var image: UIImage?
var items: [Item] =
init(from decoder: Decoder) throws
let container = try decoder.container(keyedBy: CodingKeys.self)
name = try container.decode(String.self, forKey: .name)
items = try container.decode([Item].self, forKey: .items)
if let text = try container.decodeIfPresent(String.self, forKey: .image)
if let data = Data(base64Encoded: text)
image = UIImage(data: data)
func encode(to encoder: Encoder) throws
var container = encoder.container(keyedBy: CodingKeys.self)
if let image = image, let data = image.pngData()
try container.encode(data, forKey: .image)
try container.encode(name, forKey: .name)
try container.encode(items, forKey: .items)
nb: This is the basic process I've used for a couple of projects, I've not tested the above myself, but it should present the basic idea you need to get started
Thanks for the help
– Antony D
Nov 11 '18 at 22:02
add a comment |
JSON in Swift 4 is fun (repeat this at 2am in the morning ;)).
One of the first things I always do is consult a good blog, like Ultimate Guide to JSON Parsing with Swift 4. While it might not answer the "direct" question, it will provide a lot of useful information.
Another thing to do, is consult the Apple documentation. In this, a quick look at the documentation for UIImage
will point out that it does not conform to Codable
, so that's a problem.
Another issue is, JSON doesn't support binary data, it's a text based solution.
This means that you need to figure out away to convert the binary image data to text. Lucky for us, people have already thought about this and the most common mechanism is to use Base 64 encoding.
A quick google search will turn up any number of solutions for encoding/decoding a UIImage
to/from base 64, for example Convert between UIImage and Base64 string
Now, we just need to customise the encoding and decoding process.
The first thing is to make sure all the other fields/objects/data we use are also conform to Codable
...
struct Item: Codable
var nameOfNote: String
var remind: Bool
//var date: Date
Then we can set out customising the encoding and decoding process for the image, maybe something like...
struct Checklist: Codable
enum CodingKeys: String, CodingKey
case image
case name
case items
var name: String
var image: UIImage?
var items: [Item] =
init(from decoder: Decoder) throws
let container = try decoder.container(keyedBy: CodingKeys.self)
name = try container.decode(String.self, forKey: .name)
items = try container.decode([Item].self, forKey: .items)
if let text = try container.decodeIfPresent(String.self, forKey: .image)
if let data = Data(base64Encoded: text)
image = UIImage(data: data)
func encode(to encoder: Encoder) throws
var container = encoder.container(keyedBy: CodingKeys.self)
if let image = image, let data = image.pngData()
try container.encode(data, forKey: .image)
try container.encode(name, forKey: .name)
try container.encode(items, forKey: .items)
nb: This is the basic process I've used for a couple of projects, I've not tested the above myself, but it should present the basic idea you need to get started
JSON in Swift 4 is fun (repeat this at 2am in the morning ;)).
One of the first things I always do is consult a good blog, like Ultimate Guide to JSON Parsing with Swift 4. While it might not answer the "direct" question, it will provide a lot of useful information.
Another thing to do, is consult the Apple documentation. In this, a quick look at the documentation for UIImage
will point out that it does not conform to Codable
, so that's a problem.
Another issue is, JSON doesn't support binary data, it's a text based solution.
This means that you need to figure out away to convert the binary image data to text. Lucky for us, people have already thought about this and the most common mechanism is to use Base 64 encoding.
A quick google search will turn up any number of solutions for encoding/decoding a UIImage
to/from base 64, for example Convert between UIImage and Base64 string
Now, we just need to customise the encoding and decoding process.
The first thing is to make sure all the other fields/objects/data we use are also conform to Codable
...
struct Item: Codable
var nameOfNote: String
var remind: Bool
//var date: Date
Then we can set out customising the encoding and decoding process for the image, maybe something like...
struct Checklist: Codable
enum CodingKeys: String, CodingKey
case image
case name
case items
var name: String
var image: UIImage?
var items: [Item] =
init(from decoder: Decoder) throws
let container = try decoder.container(keyedBy: CodingKeys.self)
name = try container.decode(String.self, forKey: .name)
items = try container.decode([Item].self, forKey: .items)
if let text = try container.decodeIfPresent(String.self, forKey: .image)
if let data = Data(base64Encoded: text)
image = UIImage(data: data)
func encode(to encoder: Encoder) throws
var container = encoder.container(keyedBy: CodingKeys.self)
if let image = image, let data = image.pngData()
try container.encode(data, forKey: .image)
try container.encode(name, forKey: .name)
try container.encode(items, forKey: .items)
nb: This is the basic process I've used for a couple of projects, I've not tested the above myself, but it should present the basic idea you need to get started
edited Jan 11 at 22:10
answered Nov 11 '18 at 21:56
MadProgrammerMadProgrammer
300k17154268
300k17154268
Thanks for the help
– Antony D
Nov 11 '18 at 22:02
add a comment |
Thanks for the help
– Antony D
Nov 11 '18 at 22:02
Thanks for the help
– Antony D
Nov 11 '18 at 22:02
Thanks for the help
– Antony D
Nov 11 '18 at 22:02
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%2f53253438%2fswift-codable-protocol-with-strings-and-uiimages%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
Not tried, but I doubt
UIImage
isCodable
. What I've done instead, is customised the coding and decoding process so that theUIImage
is converted to a base64String
instead– MadProgrammer
Nov 11 '18 at 21:34
I might also point out that neither
CheckList
orItem
areCodable
. You should also post text/code rather then images ;)– MadProgrammer
Nov 11 '18 at 21:35
MadProgrammer is right.
UIImage
is notCodable
compliant.– vadian
Nov 11 '18 at 21:50
how do you think i can make UIImage codable?
– Antony D
Nov 11 '18 at 21:53
@AntonyD JSON is a text based representation of the data, it doesn't not support binary data. However, this is neither a new problem or an uncommon one. The most common solution is to convert the binary data to a base 64 (text) representation. There are plenty of examples floating around. You then need to customise the encoding and decoding process for your
struct
so you can take control over how the image is encoded and decoded, again, not a uncommon or new problem - and well supported in Swift– MadProgrammer
Nov 11 '18 at 21:58