optional

Refactor multiple If' statements in Java-8

假装没事ソ 提交于 2019-12-09 00:10:19
问题 I need to validate mandatory fields in my class For example, 9 fields must not be null . I need to check if they are all null but I am using multiple if statements for this now as below: StringBuilder mandatoryExcessFields = new StringBuilder(MANDATORY_EXCESS_FIELDS.length); if(Objects.isNull(excess.getAsOfDate())){ mandatoryExcessFields.append(MANDATORY_EXCESS_FIELDS[0]); } if(StringUtils.isEmpty(excess.getStatus())) { mandatoryExcessFields.append(MANDATORY_EXCESS_FIELDS[1]); } if(Objects

Make a function accepting an optional to accept a non-optional?

故事扮演 提交于 2019-12-08 22:49:16
问题 I'm trying to write syntactic sugar, in a monad-style, over std::optional . Please consider: template<class T> void f(std::optional<T>) {} As is, this function cannot be called with a non-optional T 1 (e.g. an int ), even though there exists a conversion from T to std::optional<T> 2 . Is there a way to make f accept an std::optional<T> or a T (converted to an optional at the caller site), without defining an overload 3 ? 1) f(0) : error: no matching function for call to 'f(int)' and note:

How to deal with non-optional values in NSUserDefaults in Swift

僤鯓⒐⒋嵵緔 提交于 2019-12-08 20:58:34
To get a value from NSUserDefaults I would do something like this: let userDefaults = NSUserDefaults.standardUserDefaults() if let value = userDefaults.objectForKey(key) { print(value) } However, these methods do not return optionals: boolForKey (defaults to false if not set) integerForKey (defaults to 0 if not set) floatForKey (defaults to 0.0 if not set) doubleForKey (defaults to 0.0 if not set) In my app I want to used the saved value of an integer if it has been previously set. And if it wasn't previously set, I want to use my own default value. The problem is that 0 is a valid integer

How can you check if a type is Optional in Swift?

守給你的承諾、 提交于 2019-12-08 19:53:19
问题 How can you check if a type is Optional in Swift? Say I have a variable of type PartialKeyPath where: struct Foo { let bar: String let baz: String? } typealias Property<Root> = (key: PartialKeyPath<Root>, value: Any?) typealias Properties<Root> = [Property<Root>] Now say I iterate thru an instance of Properties: properties.forEach { prop in let valueType1 = type(of: prop.key).valueType let valueType2 = type(of: value) ... How can I check here whether valueType1 is Optional<valueType2> , or

Guard not unwrapping optional

不羁的心 提交于 2019-12-08 17:07:51
问题 I'm trying to process a JSON object, using a guard statement to unwrap it and cast to the type I want, but the value is still being saved as an optional. guard let json = try? JSONSerialization.jsonObject(with: data) as? [String:Any] else { break } let result = json["Result"] // Error: Value of optional type '[String:Any]?' not unwrapped Am I missing something here? 回答1: try? JSONSerialization.jsonObject(with: data) as? [String:Any] is interpreted as try? (JSONSerialization.jsonObject(with:

Array Index Out Of Range - Error when optional unbinding

我们两清 提交于 2019-12-08 14:04:43
问题 I have an entity called Settings with an attribute called backgroundColor of type Int , if it is 1 then the view controller will have a background of white if 0 then a background of dark grey. But I am getting the following error when trying to open the view controller; fatal error: Array Index out of range For the following line in my function if settingsArray.count == 1 { setting = settingsArray[1] } else if settingsArray.count <= 0 { println("No settings in array") } View Controller var

how use Optional with cascaded objects not created with Optional

拥有回忆 提交于 2019-12-08 12:37:29
问题 Context: I have chained objects generated by wsdl2java so none of them contains java.util.Optional. There is an already created method that call the soap web services, receives the xml and unmarshalling it in cascaded objects. Desire: I want to use Optional in order to avoid null tests. I have already posted my dificults to accomplish it (how to use optional and filter along to retrieve an object from chained/cascaded objects generated by wsdl2java) but, after 3 days searching and reading I

IEnumerable of Options to Option of IEnumerable

笑着哭i 提交于 2019-12-08 09:38:11
问题 I have a 3rd party method that get IEnumerable of T and evaluates it. I would like to introduce Option with exceptional values(either) in my LINQ evaluation statements(select, where...) for the value that needs to get fed into the method. For each T element I would like to have a method that can return the transformed element or an error (leaving it as deferred execution) and If error is returned evaluation should stop there. (technically this can be achieved with throwing an exception, but

Optional wrapping in Swift, why does Swift add the “Optional” to the string

a 夏天 提交于 2019-12-08 09:37:38
问题 I'm saving an array into a model, when saving the data is not wrapped with Optional (...) however when the data is being read I get the Optional(...) wrapping around it. Appreciate your help and patience as I'm new to Swift. This is the println when adding the values to the model: saveOperativesInModel: Test Name This is the println when reading the values back from the model: getOperativesFromModel: Optional(Test Name) Why does Swift add the "Optional(xxx)" to the String? This is the reduced

Implicitly Unwrapped Optionals and println

半腔热情 提交于 2019-12-08 08:26:37
问题 I'm learning Swift and I have a doubt regarding Implicitly Unwrapped Optionals. I have a function returning a String optional: func findApt(aptNumber :String) -> String? { let aptNumbers = ["101", "202", "303", "404"] for element in aptNumbers { if (element == aptNumber) { return aptNumber } } // END OF for LOOP return nil } and an if statement to perform safe unwrapping, which uses an Implicitly Unwrapped Optional as its constant: if let apt :String! = findApt("404") { apt println(apt)