optional

Sorting nil Dates to the end of an Array

让人想犯罪 __ 提交于 2019-12-07 01:00:47
问题 Trying to sort an array in Swift in descending order. This works well objectArray.sort{ $0.date!.compare($1.date!) == .orderedDescending} As you can see, I'm force unwrapping the date. I'm looking for another way so that if the date is nil , the object moves to the end of array. 回答1: Maybe not the cleanest solution, but you can do it in one step with nil-coalescing. objectArray.sort{ ($0.date ?? .distantPast) > ($1.date ?? .distantPast) } 来源: https://stackoverflow.com/questions/44144298

Return type std::optional<std::variant<…>>

白昼怎懂夜的黑 提交于 2019-12-06 23:33:31
问题 I have a situation where a function must return a value taken from a table. A cell in this table (let's assume the table just works...) may contain a value, or it might not. This value can also be one of several types: int, double, string, date (but no other type). What would such a function return? Is it a good idea to return std::optional<std::variant<std::string, int, double, std::chrono::time_point>> ? Would that be a good use of optional and variant ? 回答1: I would consider this to be a

What makes Swift's “Optional” safer than Objective-C's “nil”?

佐手、 提交于 2019-12-06 18:39:04
问题 Apple seems to claim that the Optional type in Swift is safer than nil in Objective-C, but I don't understand why this is so. What are the salient differences in implementation that make Optional s safer than nil , and how will this affect my code? 回答1: I always do check if a variable is nil before I use it to avoid problems. However, it still happens once in a while that a value might come in as nil , to my surprise -- or sometimes I just plain forget to check. After I code more and more in

F# Optional Record Field

ⅰ亾dé卋堺 提交于 2019-12-06 18:31:12
问题 I have a F# record type and want one of the fields to be optional: type legComponents = { shares : int<share> ; price : float<dollar / share> ; totalInvestment : float<dollar> ; } type tradeLeg = { id : int ; tradeId : int ; legActivity : LegActivityType ; actedOn : DateTime ; estimates : legComponents ; ?actuals : legComponents ; } in the tradeLeg type I would like the the actuals field to be optional. I can't seem to figure it out nor can I seem to find a reliable example on the web. It

Swift tuple to Optional assignment

老子叫甜甜 提交于 2019-12-06 18:28:13
问题 I am writing some code in Swift to learn the language. Here is my base class: import Foundation class BaseCommand:NSOperation { var status:Int? = nil var message:String? = nil func buildRequest() -> NSData? { return nil } func parseResponse(data:NSData?) -> (Status:Int, Error:String) { return (200, "Success") } override func main() { let requestBody = self.buildRequest() println("Sending body \(requestBody)") // do network op var networkResultBody = "test" var resultBody:NSData =

How to convert single element list to java 8 optional

十年热恋 提交于 2019-12-06 17:27:49
问题 How to nicely convert list containing one or zero elements to Optional? The ugly code: List<Integer> integers = new ArrayList<>(); Optional<Integer> optional = integers.size() == 0 ? Optional.empty() : Optional.of(integers.get(0)); 回答1: You can use the Stream#findFirst() method, which: Returns an Optional describing the first element of this stream, or an empty Optional if the stream is empty. List<Integer> list = ... Optional<Integer> optional = list.stream().findFirst(); Alternatively, with

What does Swift's optional binding do to the type it's arguments?

纵饮孤独 提交于 2019-12-06 16:27:09
问题 Why is if let y: Int? = nil { ... } the same as if let y: Int? = nil as Int?? { ... } (and thus an invalid assignment) especially when, on its own let y: Int? = nil is not the same as let y: Int? = nil as Int?? (since let y: Int? = nil is a valid assignment)? 回答1: OK, I will answer, with my poor English skills ;-) Let's start with this: if let lvalue:T = rvalue { ... } At first the compiler tries to convert rvalue to T? by wrapping with Optional . For example: typealias T = Int let rvalue:Int

Make a dictionary value non-optional as extension

被刻印的时光 ゝ 提交于 2019-12-06 16:04:15
The below playground outlines my issue. The extension will remove nil values from my dictionary, but leave the other values as Optional(Value). What I need is a dictionary that has no nil values and make the optional value type non-optional. Ex: I have a dictionary of [String:Int?] . I want the jsonSantize() of that called on that dictionary to return a [String:Int] . //: Playground - noun: a place where people can play import UIKit import Foundation protocol OptionalType { associatedtype Wrapped var asOptional : Wrapped? { get } } extension Optional : OptionalType { var asOptional : Wrapped?

How to use high order functions to filter and match on options fields in a record

空扰寡人 提交于 2019-12-06 14:31:03
问题 So a bit of context is required just to understand my issue. In an RPG, I consider the equipment as a record with optional fields, which means, while they're None that nothing was attributed yet. Equipment is constructed by the game items of the game that represents either weaponry or character protection (helmets and etc) which will be seen in the snippet below. The functionalities are removed and the domain model reduced to make it easier to read. type ConsummableItem = | HealthPotion |

Optional include in PHP

﹥>﹥吖頭↗ 提交于 2019-12-06 14:27:47
I have a config file with the general configuration (in a git repo), and a local config file that overwrites configuration properties (ignored in the repo). Right now the local config file is included at the beginning of the config file: include_once 'local_config.php'; But I would like the include to be conditional: only do it if the file local_config.php actually exists. I can do a enter link description here without problems, but first I would need to check if the file exists. So I tried get_include_path() but it returns a list of paths, and I would have to parse this list and check for