How to compare [String]? and String in Swift? [closed]
up vote
-4
down vote
favorite
When I try to compare [String]? and String I get the error:
Binary operator '!=' cannot be applied to operands of type '[String]?' and 'String'
Could anyone tell me how to compare them?
swift string compare
closed as unclear what you're asking by ColGraff, user6655984, TDG, greg-449, Rob Nov 10 at 12:42
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
up vote
-4
down vote
favorite
When I try to compare [String]? and String I get the error:
Binary operator '!=' cannot be applied to operands of type '[String]?' and 'String'
Could anyone tell me how to compare them?
swift string compare
closed as unclear what you're asking by ColGraff, user6655984, TDG, greg-449, Rob Nov 10 at 12:42
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
2
You'll have to be more clear on how you want to compare a collection ofStringto a singleString. Do you want to know if any item in the collection is not equal to the single item?
– ColGraff
Nov 10 at 4:42
Just give us an example of what you are trying to do and show the code that gave you this error.
– Rakesha Shastri
Nov 10 at 4:44
Thanks for all you guys effort. Im new in Swift and just got confuse with the "Question Mark". And not realised the one Im comparing actually is an array. Finally understand just need to do likearray![0]=="anystring"
– Payne Chu
Nov 11 at 1:53
add a comment |
up vote
-4
down vote
favorite
up vote
-4
down vote
favorite
When I try to compare [String]? and String I get the error:
Binary operator '!=' cannot be applied to operands of type '[String]?' and 'String'
Could anyone tell me how to compare them?
swift string compare
When I try to compare [String]? and String I get the error:
Binary operator '!=' cannot be applied to operands of type '[String]?' and 'String'
Could anyone tell me how to compare them?
swift string compare
swift string compare
edited Nov 10 at 5:12
rmaddy
236k27306373
236k27306373
asked Nov 10 at 4:30
Payne Chu
146313
146313
closed as unclear what you're asking by ColGraff, user6655984, TDG, greg-449, Rob Nov 10 at 12:42
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
closed as unclear what you're asking by ColGraff, user6655984, TDG, greg-449, Rob Nov 10 at 12:42
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
2
You'll have to be more clear on how you want to compare a collection ofStringto a singleString. Do you want to know if any item in the collection is not equal to the single item?
– ColGraff
Nov 10 at 4:42
Just give us an example of what you are trying to do and show the code that gave you this error.
– Rakesha Shastri
Nov 10 at 4:44
Thanks for all you guys effort. Im new in Swift and just got confuse with the "Question Mark". And not realised the one Im comparing actually is an array. Finally understand just need to do likearray![0]=="anystring"
– Payne Chu
Nov 11 at 1:53
add a comment |
2
You'll have to be more clear on how you want to compare a collection ofStringto a singleString. Do you want to know if any item in the collection is not equal to the single item?
– ColGraff
Nov 10 at 4:42
Just give us an example of what you are trying to do and show the code that gave you this error.
– Rakesha Shastri
Nov 10 at 4:44
Thanks for all you guys effort. Im new in Swift and just got confuse with the "Question Mark". And not realised the one Im comparing actually is an array. Finally understand just need to do likearray![0]=="anystring"
– Payne Chu
Nov 11 at 1:53
2
2
You'll have to be more clear on how you want to compare a collection of
String to a single String. Do you want to know if any item in the collection is not equal to the single item?– ColGraff
Nov 10 at 4:42
You'll have to be more clear on how you want to compare a collection of
String to a single String. Do you want to know if any item in the collection is not equal to the single item?– ColGraff
Nov 10 at 4:42
Just give us an example of what you are trying to do and show the code that gave you this error.
– Rakesha Shastri
Nov 10 at 4:44
Just give us an example of what you are trying to do and show the code that gave you this error.
– Rakesha Shastri
Nov 10 at 4:44
Thanks for all you guys effort. Im new in Swift and just got confuse with the "Question Mark". And not realised the one Im comparing actually is an array. Finally understand just need to do like
array![0]=="anystring"– Payne Chu
Nov 11 at 1:53
Thanks for all you guys effort. Im new in Swift and just got confuse with the "Question Mark". And not realised the one Im comparing actually is an array. Finally understand just need to do like
array![0]=="anystring"– Payne Chu
Nov 11 at 1:53
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
You can't compare a string with an optional array of strings, they have two different types: The equality operator ==, as defined in the standard library, can't compare a string to an optional array of strings.
If you want to check if an optional array contains a string, then use the following:
let array: [String]? = ["hello", "world", "✋🏻"]
let result = array?.contains("hello")
result would be an optional boolean that you may unwrap later.
add a comment |
up vote
0
down vote
var array = ["First", "Second"]
let loneString = "Second"
for k in 0..<array.count
if array[k] != loneString
print("they are not same")
2
This doesn't even compile (you can't unwrap a string that isn't optional) +arrayis optional in the question
– Carpsen90
Nov 10 at 11:04
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
You can't compare a string with an optional array of strings, they have two different types: The equality operator ==, as defined in the standard library, can't compare a string to an optional array of strings.
If you want to check if an optional array contains a string, then use the following:
let array: [String]? = ["hello", "world", "✋🏻"]
let result = array?.contains("hello")
result would be an optional boolean that you may unwrap later.
add a comment |
up vote
1
down vote
accepted
You can't compare a string with an optional array of strings, they have two different types: The equality operator ==, as defined in the standard library, can't compare a string to an optional array of strings.
If you want to check if an optional array contains a string, then use the following:
let array: [String]? = ["hello", "world", "✋🏻"]
let result = array?.contains("hello")
result would be an optional boolean that you may unwrap later.
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
You can't compare a string with an optional array of strings, they have two different types: The equality operator ==, as defined in the standard library, can't compare a string to an optional array of strings.
If you want to check if an optional array contains a string, then use the following:
let array: [String]? = ["hello", "world", "✋🏻"]
let result = array?.contains("hello")
result would be an optional boolean that you may unwrap later.
You can't compare a string with an optional array of strings, they have two different types: The equality operator ==, as defined in the standard library, can't compare a string to an optional array of strings.
If you want to check if an optional array contains a string, then use the following:
let array: [String]? = ["hello", "world", "✋🏻"]
let result = array?.contains("hello")
result would be an optional boolean that you may unwrap later.
answered Nov 10 at 11:26
Carpsen90
6,59062557
6,59062557
add a comment |
add a comment |
up vote
0
down vote
var array = ["First", "Second"]
let loneString = "Second"
for k in 0..<array.count
if array[k] != loneString
print("they are not same")
2
This doesn't even compile (you can't unwrap a string that isn't optional) +arrayis optional in the question
– Carpsen90
Nov 10 at 11:04
add a comment |
up vote
0
down vote
var array = ["First", "Second"]
let loneString = "Second"
for k in 0..<array.count
if array[k] != loneString
print("they are not same")
2
This doesn't even compile (you can't unwrap a string that isn't optional) +arrayis optional in the question
– Carpsen90
Nov 10 at 11:04
add a comment |
up vote
0
down vote
up vote
0
down vote
var array = ["First", "Second"]
let loneString = "Second"
for k in 0..<array.count
if array[k] != loneString
print("they are not same")
var array = ["First", "Second"]
let loneString = "Second"
for k in 0..<array.count
if array[k] != loneString
print("they are not same")
edited Nov 11 at 7:42
mrvincenzo
3,39832142
3,39832142
answered Nov 10 at 8:29
Hope
5493828
5493828
2
This doesn't even compile (you can't unwrap a string that isn't optional) +arrayis optional in the question
– Carpsen90
Nov 10 at 11:04
add a comment |
2
This doesn't even compile (you can't unwrap a string that isn't optional) +arrayis optional in the question
– Carpsen90
Nov 10 at 11:04
2
2
This doesn't even compile (you can't unwrap a string that isn't optional) +
array is optional in the question– Carpsen90
Nov 10 at 11:04
This doesn't even compile (you can't unwrap a string that isn't optional) +
array is optional in the question– Carpsen90
Nov 10 at 11:04
add a comment |
2
You'll have to be more clear on how you want to compare a collection of
Stringto a singleString. Do you want to know if any item in the collection is not equal to the single item?– ColGraff
Nov 10 at 4:42
Just give us an example of what you are trying to do and show the code that gave you this error.
– Rakesha Shastri
Nov 10 at 4:44
Thanks for all you guys effort. Im new in Swift and just got confuse with the "Question Mark". And not realised the one Im comparing actually is an array. Finally understand just need to do like
array![0]=="anystring"– Payne Chu
Nov 11 at 1:53