How to iterate over array of Any
![Multi tool use Multi tool use](http://sgv.ssvwv.com/sg/ssvwvcomimagb.png)
Multi tool use
I want to know how I would iterate over an array of objects of different types. The array looks like this:
var messages: [Any] = [SentMessage(sent_text: "Halla", date_sent: "24.des", isSent: true, sending: false), RecievedMessage(profile_image: UIImage(named: "baseline_account_box_black_18pt")!, recieved_text: "Hei hva skjer?", date_recieved: "25.des", isRecieved: true)]
I tried to convert iterator like this:
for i in messages{
guard let received = ReceivedMessage(i) else{
return
}
}
ReceivedMessages and SentMessages are both structs, if it is necessary to see more code, just ask.
swift iteration
add a comment |
I want to know how I would iterate over an array of objects of different types. The array looks like this:
var messages: [Any] = [SentMessage(sent_text: "Halla", date_sent: "24.des", isSent: true, sending: false), RecievedMessage(profile_image: UIImage(named: "baseline_account_box_black_18pt")!, recieved_text: "Hei hva skjer?", date_recieved: "25.des", isRecieved: true)]
I tried to convert iterator like this:
for i in messages{
guard let received = ReceivedMessage(i) else{
return
}
}
ReceivedMessages and SentMessages are both structs, if it is necessary to see more code, just ask.
swift iteration
2
"I want to know how I would iterate over an array of objects of different types" Best way: don't.
– matt
Dec 28 '18 at 21:58
1
See stackoverflow.com/a/26076910/1630618
– vacawama
Dec 28 '18 at 22:04
add a comment |
I want to know how I would iterate over an array of objects of different types. The array looks like this:
var messages: [Any] = [SentMessage(sent_text: "Halla", date_sent: "24.des", isSent: true, sending: false), RecievedMessage(profile_image: UIImage(named: "baseline_account_box_black_18pt")!, recieved_text: "Hei hva skjer?", date_recieved: "25.des", isRecieved: true)]
I tried to convert iterator like this:
for i in messages{
guard let received = ReceivedMessage(i) else{
return
}
}
ReceivedMessages and SentMessages are both structs, if it is necessary to see more code, just ask.
swift iteration
I want to know how I would iterate over an array of objects of different types. The array looks like this:
var messages: [Any] = [SentMessage(sent_text: "Halla", date_sent: "24.des", isSent: true, sending: false), RecievedMessage(profile_image: UIImage(named: "baseline_account_box_black_18pt")!, recieved_text: "Hei hva skjer?", date_recieved: "25.des", isRecieved: true)]
I tried to convert iterator like this:
for i in messages{
guard let received = ReceivedMessage(i) else{
return
}
}
ReceivedMessages and SentMessages are both structs, if it is necessary to see more code, just ask.
swift iteration
swift iteration
asked Dec 28 '18 at 21:45
Elias KnudsenElias Knudsen
83
83
2
"I want to know how I would iterate over an array of objects of different types" Best way: don't.
– matt
Dec 28 '18 at 21:58
1
See stackoverflow.com/a/26076910/1630618
– vacawama
Dec 28 '18 at 22:04
add a comment |
2
"I want to know how I would iterate over an array of objects of different types" Best way: don't.
– matt
Dec 28 '18 at 21:58
1
See stackoverflow.com/a/26076910/1630618
– vacawama
Dec 28 '18 at 22:04
2
2
"I want to know how I would iterate over an array of objects of different types" Best way: don't.
– matt
Dec 28 '18 at 21:58
"I want to know how I would iterate over an array of objects of different types" Best way: don't.
– matt
Dec 28 '18 at 21:58
1
1
See stackoverflow.com/a/26076910/1630618
– vacawama
Dec 28 '18 at 22:04
See stackoverflow.com/a/26076910/1630618
– vacawama
Dec 28 '18 at 22:04
add a comment |
1 Answer
1
active
oldest
votes
Use optional binding :
guard let received = i as? ReceivedMessage
Instead of declaring messages
as [Any]
, make ReceivedMessage
and SentMessage
adopt a common protocol and then messages
would be an array of objects adopting that protocol:
protocol Message {
}
struct SentMessage: Message {
}
struct ReceivedMessage: Message {
}
var messages: [Message]
Would you mind showing an example?
– Elias Knudsen
Dec 28 '18 at 21:55
I understand that, but if I am adopting everything from a protocol, I could just create one struct as well?
– Elias Knudsen
Dec 28 '18 at 22:02
@EliasKnudsen Of course, preferably, with a boolean to differentiate between received and sent messages
– Carpsen90
Dec 28 '18 at 22:04
Thanks, it was dumb to create to different structs, a message share many similar properites, only difference is whether it was sent or received.
– Elias Knudsen
Dec 28 '18 at 22:06
Maybe introduce an enum for the status of the message? You'll probably discover over time that a message can probably be in more states than just sent and received
– Ben Kane
Dec 28 '18 at 22:09
|
show 1 more 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%2f53964596%2fhow-to-iterate-over-array-of-any%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
Use optional binding :
guard let received = i as? ReceivedMessage
Instead of declaring messages
as [Any]
, make ReceivedMessage
and SentMessage
adopt a common protocol and then messages
would be an array of objects adopting that protocol:
protocol Message {
}
struct SentMessage: Message {
}
struct ReceivedMessage: Message {
}
var messages: [Message]
Would you mind showing an example?
– Elias Knudsen
Dec 28 '18 at 21:55
I understand that, but if I am adopting everything from a protocol, I could just create one struct as well?
– Elias Knudsen
Dec 28 '18 at 22:02
@EliasKnudsen Of course, preferably, with a boolean to differentiate between received and sent messages
– Carpsen90
Dec 28 '18 at 22:04
Thanks, it was dumb to create to different structs, a message share many similar properites, only difference is whether it was sent or received.
– Elias Knudsen
Dec 28 '18 at 22:06
Maybe introduce an enum for the status of the message? You'll probably discover over time that a message can probably be in more states than just sent and received
– Ben Kane
Dec 28 '18 at 22:09
|
show 1 more comment
Use optional binding :
guard let received = i as? ReceivedMessage
Instead of declaring messages
as [Any]
, make ReceivedMessage
and SentMessage
adopt a common protocol and then messages
would be an array of objects adopting that protocol:
protocol Message {
}
struct SentMessage: Message {
}
struct ReceivedMessage: Message {
}
var messages: [Message]
Would you mind showing an example?
– Elias Knudsen
Dec 28 '18 at 21:55
I understand that, but if I am adopting everything from a protocol, I could just create one struct as well?
– Elias Knudsen
Dec 28 '18 at 22:02
@EliasKnudsen Of course, preferably, with a boolean to differentiate between received and sent messages
– Carpsen90
Dec 28 '18 at 22:04
Thanks, it was dumb to create to different structs, a message share many similar properites, only difference is whether it was sent or received.
– Elias Knudsen
Dec 28 '18 at 22:06
Maybe introduce an enum for the status of the message? You'll probably discover over time that a message can probably be in more states than just sent and received
– Ben Kane
Dec 28 '18 at 22:09
|
show 1 more comment
Use optional binding :
guard let received = i as? ReceivedMessage
Instead of declaring messages
as [Any]
, make ReceivedMessage
and SentMessage
adopt a common protocol and then messages
would be an array of objects adopting that protocol:
protocol Message {
}
struct SentMessage: Message {
}
struct ReceivedMessage: Message {
}
var messages: [Message]
Use optional binding :
guard let received = i as? ReceivedMessage
Instead of declaring messages
as [Any]
, make ReceivedMessage
and SentMessage
adopt a common protocol and then messages
would be an array of objects adopting that protocol:
protocol Message {
}
struct SentMessage: Message {
}
struct ReceivedMessage: Message {
}
var messages: [Message]
edited Dec 29 '18 at 23:29
answered Dec 28 '18 at 21:49
![](https://i.stack.imgur.com/W2LSH.png?s=32&g=1)
![](https://i.stack.imgur.com/W2LSH.png?s=32&g=1)
Carpsen90Carpsen90
7,27262559
7,27262559
Would you mind showing an example?
– Elias Knudsen
Dec 28 '18 at 21:55
I understand that, but if I am adopting everything from a protocol, I could just create one struct as well?
– Elias Knudsen
Dec 28 '18 at 22:02
@EliasKnudsen Of course, preferably, with a boolean to differentiate between received and sent messages
– Carpsen90
Dec 28 '18 at 22:04
Thanks, it was dumb to create to different structs, a message share many similar properites, only difference is whether it was sent or received.
– Elias Knudsen
Dec 28 '18 at 22:06
Maybe introduce an enum for the status of the message? You'll probably discover over time that a message can probably be in more states than just sent and received
– Ben Kane
Dec 28 '18 at 22:09
|
show 1 more comment
Would you mind showing an example?
– Elias Knudsen
Dec 28 '18 at 21:55
I understand that, but if I am adopting everything from a protocol, I could just create one struct as well?
– Elias Knudsen
Dec 28 '18 at 22:02
@EliasKnudsen Of course, preferably, with a boolean to differentiate between received and sent messages
– Carpsen90
Dec 28 '18 at 22:04
Thanks, it was dumb to create to different structs, a message share many similar properites, only difference is whether it was sent or received.
– Elias Knudsen
Dec 28 '18 at 22:06
Maybe introduce an enum for the status of the message? You'll probably discover over time that a message can probably be in more states than just sent and received
– Ben Kane
Dec 28 '18 at 22:09
Would you mind showing an example?
– Elias Knudsen
Dec 28 '18 at 21:55
Would you mind showing an example?
– Elias Knudsen
Dec 28 '18 at 21:55
I understand that, but if I am adopting everything from a protocol, I could just create one struct as well?
– Elias Knudsen
Dec 28 '18 at 22:02
I understand that, but if I am adopting everything from a protocol, I could just create one struct as well?
– Elias Knudsen
Dec 28 '18 at 22:02
@EliasKnudsen Of course, preferably, with a boolean to differentiate between received and sent messages
– Carpsen90
Dec 28 '18 at 22:04
@EliasKnudsen Of course, preferably, with a boolean to differentiate between received and sent messages
– Carpsen90
Dec 28 '18 at 22:04
Thanks, it was dumb to create to different structs, a message share many similar properites, only difference is whether it was sent or received.
– Elias Knudsen
Dec 28 '18 at 22:06
Thanks, it was dumb to create to different structs, a message share many similar properites, only difference is whether it was sent or received.
– Elias Knudsen
Dec 28 '18 at 22:06
Maybe introduce an enum for the status of the message? You'll probably discover over time that a message can probably be in more states than just sent and received
– Ben Kane
Dec 28 '18 at 22:09
Maybe introduce an enum for the status of the message? You'll probably discover over time that a message can probably be in more states than just sent and received
– Ben Kane
Dec 28 '18 at 22:09
|
show 1 more 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%2f53964596%2fhow-to-iterate-over-array-of-any%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
IVQ,3bVWvFJQAjad3xx8ddmOoHbotvSA,n,XILmQyZSX8jlW4ytnz,l7hHUKIZHxIQv4ZTpunN M KZBWfKj4wsei253,I,JiNL ToKk9
2
"I want to know how I would iterate over an array of objects of different types" Best way: don't.
– matt
Dec 28 '18 at 21:58
1
See stackoverflow.com/a/26076910/1630618
– vacawama
Dec 28 '18 at 22:04