Says type is non-optional but prints optional
up vote
-2
down vote
favorite
I have this code:
let result_string = "(String(describing: value))"
because I need to convert this from type Any to String. However when I print result string, it prints:
optional(abcd)
How do I make it print just
abcd
?
When I tried the other fixes for optionals it told me that result_string is not optional and didn't let me force unwrap.
EDIT:
Value was an optional, so force unwrapping it fixed my problem.
let result_string = "(String(describing: value!))"
swift xcode
New contributor
|
show 1 more comment
up vote
-2
down vote
favorite
I have this code:
let result_string = "(String(describing: value))"
because I need to convert this from type Any to String. However when I print result string, it prints:
optional(abcd)
How do I make it print just
abcd
?
When I tried the other fixes for optionals it told me that result_string is not optional and didn't let me force unwrap.
EDIT:
Value was an optional, so force unwrapping it fixed my problem.
let result_string = "(String(describing: value!))"
swift xcode
New contributor
My guess:value
is an optional ...
– Martin R
yesterday
@Martin R So should I try force unwrapping value?
– Athreya Daniel
yesterday
@MartinR Thank You, force unwrapping this fixed my problem.
– Athreya Daniel
yesterday
2
force unwrap may crash if the value is nil
– Sh_Khan
yesterday
@AthreyaDaniel your result_string is optional but value which is type Any is not optional and you are trying to force unwrap non-optional value.
– Prabhat
10 hours ago
|
show 1 more comment
up vote
-2
down vote
favorite
up vote
-2
down vote
favorite
I have this code:
let result_string = "(String(describing: value))"
because I need to convert this from type Any to String. However when I print result string, it prints:
optional(abcd)
How do I make it print just
abcd
?
When I tried the other fixes for optionals it told me that result_string is not optional and didn't let me force unwrap.
EDIT:
Value was an optional, so force unwrapping it fixed my problem.
let result_string = "(String(describing: value!))"
swift xcode
New contributor
I have this code:
let result_string = "(String(describing: value))"
because I need to convert this from type Any to String. However when I print result string, it prints:
optional(abcd)
How do I make it print just
abcd
?
When I tried the other fixes for optionals it told me that result_string is not optional and didn't let me force unwrap.
EDIT:
Value was an optional, so force unwrapping it fixed my problem.
let result_string = "(String(describing: value!))"
swift xcode
swift xcode
New contributor
New contributor
edited yesterday
New contributor
asked yesterday
Athreya Daniel
11
11
New contributor
New contributor
My guess:value
is an optional ...
– Martin R
yesterday
@Martin R So should I try force unwrapping value?
– Athreya Daniel
yesterday
@MartinR Thank You, force unwrapping this fixed my problem.
– Athreya Daniel
yesterday
2
force unwrap may crash if the value is nil
– Sh_Khan
yesterday
@AthreyaDaniel your result_string is optional but value which is type Any is not optional and you are trying to force unwrap non-optional value.
– Prabhat
10 hours ago
|
show 1 more comment
My guess:value
is an optional ...
– Martin R
yesterday
@Martin R So should I try force unwrapping value?
– Athreya Daniel
yesterday
@MartinR Thank You, force unwrapping this fixed my problem.
– Athreya Daniel
yesterday
2
force unwrap may crash if the value is nil
– Sh_Khan
yesterday
@AthreyaDaniel your result_string is optional but value which is type Any is not optional and you are trying to force unwrap non-optional value.
– Prabhat
10 hours ago
My guess:
value
is an optional ...– Martin R
yesterday
My guess:
value
is an optional ...– Martin R
yesterday
@Martin R So should I try force unwrapping value?
– Athreya Daniel
yesterday
@Martin R So should I try force unwrapping value?
– Athreya Daniel
yesterday
@MartinR Thank You, force unwrapping this fixed my problem.
– Athreya Daniel
yesterday
@MartinR Thank You, force unwrapping this fixed my problem.
– Athreya Daniel
yesterday
2
2
force unwrap may crash if the value is nil
– Sh_Khan
yesterday
force unwrap may crash if the value is nil
– Sh_Khan
yesterday
@AthreyaDaniel your result_string is optional but value which is type Any is not optional and you are trying to force unwrap non-optional value.
– Prabhat
10 hours ago
@AthreyaDaniel your result_string is optional but value which is type Any is not optional and you are trying to force unwrap non-optional value.
– Prabhat
10 hours ago
|
show 1 more comment
2 Answers
2
active
oldest
votes
up vote
0
down vote
You can try
if let res = value {
print(res)
}
OR
guard let res = value { return }
print(res)
add a comment |
up vote
-1
down vote
you can try like below:
var value : Any? = "abcd"
let result_string = value as! String
print(result_string)
We have to downcast Any to string.
Worked in playground.
Sorry Prabhat but this is wrong. In your example,result_string
is a non-optional string containing the literal string "Optional(value)". Same mistake as OP's.
– Moritz
9 hours ago
@Moritz If value is type Any with optional then it will produce Optional(value) and I was consi
– Prabhat
8 hours ago
Please test your code in a Playground, Prabhat. You will see that you get "initializer for conditional binding must have Optional type, not 'String'", because as I said, in your exampleresult_string
is not an optional. Hint: "interpolation"... ;)
– Moritz
8 hours ago
Thanks got it. We have to downcast value of type Any . I have edited the answer and verified it.
– Prabhat
8 hours ago
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
You can try
if let res = value {
print(res)
}
OR
guard let res = value { return }
print(res)
add a comment |
up vote
0
down vote
You can try
if let res = value {
print(res)
}
OR
guard let res = value { return }
print(res)
add a comment |
up vote
0
down vote
up vote
0
down vote
You can try
if let res = value {
print(res)
}
OR
guard let res = value { return }
print(res)
You can try
if let res = value {
print(res)
}
OR
guard let res = value { return }
print(res)
answered yesterday
Sh_Khan
34.2k41125
34.2k41125
add a comment |
add a comment |
up vote
-1
down vote
you can try like below:
var value : Any? = "abcd"
let result_string = value as! String
print(result_string)
We have to downcast Any to string.
Worked in playground.
Sorry Prabhat but this is wrong. In your example,result_string
is a non-optional string containing the literal string "Optional(value)". Same mistake as OP's.
– Moritz
9 hours ago
@Moritz If value is type Any with optional then it will produce Optional(value) and I was consi
– Prabhat
8 hours ago
Please test your code in a Playground, Prabhat. You will see that you get "initializer for conditional binding must have Optional type, not 'String'", because as I said, in your exampleresult_string
is not an optional. Hint: "interpolation"... ;)
– Moritz
8 hours ago
Thanks got it. We have to downcast value of type Any . I have edited the answer and verified it.
– Prabhat
8 hours ago
add a comment |
up vote
-1
down vote
you can try like below:
var value : Any? = "abcd"
let result_string = value as! String
print(result_string)
We have to downcast Any to string.
Worked in playground.
Sorry Prabhat but this is wrong. In your example,result_string
is a non-optional string containing the literal string "Optional(value)". Same mistake as OP's.
– Moritz
9 hours ago
@Moritz If value is type Any with optional then it will produce Optional(value) and I was consi
– Prabhat
8 hours ago
Please test your code in a Playground, Prabhat. You will see that you get "initializer for conditional binding must have Optional type, not 'String'", because as I said, in your exampleresult_string
is not an optional. Hint: "interpolation"... ;)
– Moritz
8 hours ago
Thanks got it. We have to downcast value of type Any . I have edited the answer and verified it.
– Prabhat
8 hours ago
add a comment |
up vote
-1
down vote
up vote
-1
down vote
you can try like below:
var value : Any? = "abcd"
let result_string = value as! String
print(result_string)
We have to downcast Any to string.
Worked in playground.
you can try like below:
var value : Any? = "abcd"
let result_string = value as! String
print(result_string)
We have to downcast Any to string.
Worked in playground.
edited 8 hours ago
answered 10 hours ago
Prabhat
493
493
Sorry Prabhat but this is wrong. In your example,result_string
is a non-optional string containing the literal string "Optional(value)". Same mistake as OP's.
– Moritz
9 hours ago
@Moritz If value is type Any with optional then it will produce Optional(value) and I was consi
– Prabhat
8 hours ago
Please test your code in a Playground, Prabhat. You will see that you get "initializer for conditional binding must have Optional type, not 'String'", because as I said, in your exampleresult_string
is not an optional. Hint: "interpolation"... ;)
– Moritz
8 hours ago
Thanks got it. We have to downcast value of type Any . I have edited the answer and verified it.
– Prabhat
8 hours ago
add a comment |
Sorry Prabhat but this is wrong. In your example,result_string
is a non-optional string containing the literal string "Optional(value)". Same mistake as OP's.
– Moritz
9 hours ago
@Moritz If value is type Any with optional then it will produce Optional(value) and I was consi
– Prabhat
8 hours ago
Please test your code in a Playground, Prabhat. You will see that you get "initializer for conditional binding must have Optional type, not 'String'", because as I said, in your exampleresult_string
is not an optional. Hint: "interpolation"... ;)
– Moritz
8 hours ago
Thanks got it. We have to downcast value of type Any . I have edited the answer and verified it.
– Prabhat
8 hours ago
Sorry Prabhat but this is wrong. In your example,
result_string
is a non-optional string containing the literal string "Optional(value)". Same mistake as OP's.– Moritz
9 hours ago
Sorry Prabhat but this is wrong. In your example,
result_string
is a non-optional string containing the literal string "Optional(value)". Same mistake as OP's.– Moritz
9 hours ago
@Moritz If value is type Any with optional then it will produce Optional(value) and I was consi
– Prabhat
8 hours ago
@Moritz If value is type Any with optional then it will produce Optional(value) and I was consi
– Prabhat
8 hours ago
Please test your code in a Playground, Prabhat. You will see that you get "initializer for conditional binding must have Optional type, not 'String'", because as I said, in your example
result_string
is not an optional. Hint: "interpolation"... ;)– Moritz
8 hours ago
Please test your code in a Playground, Prabhat. You will see that you get "initializer for conditional binding must have Optional type, not 'String'", because as I said, in your example
result_string
is not an optional. Hint: "interpolation"... ;)– Moritz
8 hours ago
Thanks got it. We have to downcast value of type Any . I have edited the answer and verified it.
– Prabhat
8 hours ago
Thanks got it. We have to downcast value of type Any . I have edited the answer and verified it.
– Prabhat
8 hours ago
add a comment |
Athreya Daniel is a new contributor. Be nice, and check out our Code of Conduct.
Athreya Daniel is a new contributor. Be nice, and check out our Code of Conduct.
Athreya Daniel is a new contributor. Be nice, and check out our Code of Conduct.
Athreya Daniel is a new contributor. Be nice, and check out our Code of Conduct.
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%2f53401637%2fsays-type-is-non-optional-but-prints-optional%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
My guess:
value
is an optional ...– Martin R
yesterday
@Martin R So should I try force unwrapping value?
– Athreya Daniel
yesterday
@MartinR Thank You, force unwrapping this fixed my problem.
– Athreya Daniel
yesterday
2
force unwrap may crash if the value is nil
– Sh_Khan
yesterday
@AthreyaDaniel your result_string is optional but value which is type Any is not optional and you are trying to force unwrap non-optional value.
– Prabhat
10 hours ago