optional

Swift double unwrapping of Optionals

烂漫一生 提交于 2019-12-21 04:07:03
问题 I understand what optional are in Swift but I just encountered a ”Double Wrapped Optional’ , where if I don’t use two '!' Xcode gives an complier error Value of optional type 'String?' not unwrapped; did you mean to use '!' or ‘?'? I have the following code, where app is of type NSRunningApplication . let name: String = app.localizedName! Why should I have to use two ! ? Isn’t one enough to unwrap the variable because it is of type var localizedName: String? . Context: Xcode want me to use

optional array in avro schema

南楼画角 提交于 2019-12-21 03:52:52
问题 I'm wondering whether or not it is possible to have an optional array. Let's assume a schema like this: { "type": "record", "name": "test_avro", "fields" : [ {"name": "test_field_1", "type": "long"}, {"name": "subrecord", "type": [{ "type": "record", "name": "subrecord_type", "fields":[{"name":"field_1", "type":"long"}] },"null"] }, {"name": "simple_array", "type":{ "type": "array", "items": "string" } } ] } Trying to write an avro record without "simple_array" would result in a NPE in the

Best alternative to std::optional to return an optional value from a method? (using C++98/C++11/C++14)

こ雲淡風輕ζ 提交于 2019-12-21 03:14:07
问题 Obviously, std::optional is the best choice to return an optional value from a function if one uses C++17 or boost (see also GOTW #90) std::optional<double> possiblyFailingCalculation() But what and why would be the best alternative if one is stuck with an older version (and can't use boost)? I see a few options: STL smart pointers (C++11 only) std::unique_ptr<double> possiblyFailingCalculation(); (+) virtually the same usage as optional (−) confusing to have smart pointers to non-polymorphic

How does @RequestParam in Spring handle Guava's Optional?

二次信任 提交于 2019-12-20 17:36:48
问题 @RequestMapping(value = "/contact.html", method = RequestMethod.POST) public final ModelAndView contact( @RequestParam(value = "name", required = false) Optional<String> name) { How does Spring's @RequestMapping handle an Optional from Guava library if the parameter value is not required and nothing is sent? Will it be: Set to null Set to Optional.absent() Can Optional.fromNullable(T) be used to accept the request? 回答1: EDIT (October 2015): Spring 4 handles java.util.Optional (from Java 8)

How does @RequestParam in Spring handle Guava's Optional?

本小妞迷上赌 提交于 2019-12-20 17:36:25
问题 @RequestMapping(value = "/contact.html", method = RequestMethod.POST) public final ModelAndView contact( @RequestParam(value = "name", required = false) Optional<String> name) { How does Spring's @RequestMapping handle an Optional from Guava library if the parameter value is not required and nothing is sent? Will it be: Set to null Set to Optional.absent() Can Optional.fromNullable(T) be used to accept the request? 回答1: EDIT (October 2015): Spring 4 handles java.util.Optional (from Java 8)

Difference between Force Unwrapping Optionals and Implicitly Unwrapped Optionals

大兔子大兔子 提交于 2019-12-20 08:49:29
问题 I was very confused about forced unwrapping and implicit unwrapping at first. Now, the following understanding comes from my self-study: There is no action available for implicit unwrapping, but there is something called implicitly unwrapped Optionals. Implicitly unwrapped Optionals and normal Optionals are both Optionals, the difference being when accessing an implicitly unwrapped Optional, you confidently know that there is a valid value under the hood and ready for use. Normal Optionals

Checking the value of an Optional Bool

半世苍凉 提交于 2019-12-20 08:26:51
问题 When I want to check if an Optional Bool is true, doing this doesn't work: var boolean : Bool? = false if boolean{ } It results in this error: Optional type '@IvalueBool?' cannot be used as a boolean; test for '!= nil' instead I don't want to check for nil; I want to check if the value returned is true. Do I always have to do if boolean == true if I'm working with an Optional Bool? Since Optionals don't conform to BooleanType anymore, shouldn't the compiler know that I want to check the value

Unable to remove “Optional” from String

六眼飞鱼酱① 提交于 2019-12-20 07:43:07
问题 Below is my snippet // MARK: - Location Functions func getCurrentLocation() -> (String!, String!) { let location = LocationManager.sharedInstance.currentLocation?.coordinate return (String(location?.latitude), String(location?.longitude)) } func setCurrentLocation() { let (latitude, longitude) = getCurrentLocation() let location = "\(latitude!),\(longitude!)" print(location) } Though I unwrap optional using latitude! and longitude! , it prints me Optional(37.33233141),Optional(-122.0312186) I

How to use Optionals to stop a compiler error in Swift [duplicate]

旧时模样 提交于 2019-12-20 06:07:41
问题 This question already has answers here : can I split a numeric string using multiple separators in a Swift closure? (3 answers) Closed 2 years ago . I want a way to detect input errors in a string and notify the user. Take the following example: let fraction = "15/8" let fractionArray = fraction.components(separatedBy: "/") let numerator = Double(fractionArray[0]) let denominator = Double(fractionArray[1]) var linearFactor = numerator! / denominator! print(numerator!, "/", denominator!, " = "

Is there some way to specify a custom deserializer for a type that is nested inside an Optional for Jackson?

谁说胖子不能爱 提交于 2019-12-20 04:51:10
问题 I have some types which need to be handled with their own special deserializers and serializers, but how do I instruct Jackson to use them when the type is nested inside an Optional? I am using the JDK8Module which works great for any type not needing any special handling. The @JsonDeserialize and @JsonSerialize annotations don't seem to have any way to apply to the value inside an Optional when they are used on an Optional field: @JsonDeserialize(?????) Optional<SpecialType> myField 回答1: The