optional

Swift 4: Printing out Bool optionals as either unwrapped true or false or as wrapped nil?

时间秒杀一切 提交于 2019-12-12 04:24:36
问题 Basically I want to print the below value as either "true", "false" or "nil". If I try just using the wrapped value I get the "nil" I want but gain the unwanted "Optional(true)" or "Optional(false). If I force unwrap the value and the value is nil I get a fatal error. I tried the below code as I've seen it work for Strings but because "nil" is not of type Bool it's not accepted. Any solutions to this? var isReal: Bool? String("Value is \(isReal ?? "nil")") I'm exporting to a csv file and it's

Swift dictionary with mix types (optional and non-optional)

偶尔善良 提交于 2019-12-11 18:06:30
问题 I have a struct that has a method to return a dictionary representation. The member variables were a combination of different types (String and Double?) With the following code example, there would be a warning from Xcode (Expression implicitly coerced from 'Double?' to Any) struct Record { let name: String let frequency: Double? init(name: String, frequency: Double?) { self.name = name self.frequency = frequency } func toDictionary() -> [String: Any] { return [ "name": name, "frequency":

Post request always wrapped by optional text

冷暖自知 提交于 2019-12-11 11:17:21
问题 I have a very similar problem like in Why the http post request body always wrapped by Optional text in the Swift app but I can´t apply the solution from this thread to my code, because I don´t have a request.setValue. Does anyone know what I need to do to get rid of the Optional? My Code: @IBAction func LoginButtonTapped(sender: UIButton) { let username = UsernameTextField.text let password = PasswordTextField.text if(username!.isEmpty || password!.isEmpty) {return; } let request =

Optional unwrapping SKPhysics error

让人想犯罪 __ 提交于 2019-12-11 09:49:54
问题 Before I'm crucified with downvotes let me say I have done research into this and I still cannot understand why I am getting this error. I have a core that the player is trying to defend and you can shoot little lasers out from it to defend against incoming meteors. Well, I have everything set up and working (most of the time anyways), but every once and while when a laser hits a meteor and my collision handling function tries to remove the shot node and meteor node, it throws this error:

flatMap does not filter out nil when ElementOfResult is inferred to be Optional

馋奶兔 提交于 2019-12-11 08:47:58
问题 Swift documentation of flatMap reads: Returns an array containing the non-nil results of calling the given transformation with each element of this sequence. In the following examples when return type of ElementOfResult is left to the compiler to infer flatMap works as documented, yet on line 5 when ElementOfResult is specified, thus inferred to an Optional<String> type it seems that flatMap stops filtering out nil 's. Why is it doing that? ~ swift Welcome to Apple Swift version 3.0.2

Return value if passes predicate, else default

我是研究僧i 提交于 2019-12-11 07:34:41
问题 How can I replace a value if it fails a predicate? To illustrate: assert_eq!((3-5).but_if(|v| v < 0).then(0), 0) I thought there would be something on Option or Result to allow this, but I cannot find it. 回答1: I thought there would be something on Option or Result But neither of these types appear here. Subtracting two numbers yields another number. It appears you just want a traditional if-else statement: fn main() { let a = 3 - 5; assert_eq!(if a < 0 { 0 } else { a }, 0); } Since you have

why swift compiler behaves differently with equality operator with/without Equatable protocol

痴心易碎 提交于 2019-12-11 06:54:27
问题 I have a very simple class in a Playground in Swift 4.0 that overrides the == operator. I'm not understanding why the Swift complier doesn't behave the same when the class inherits/doesn't inherit Equatable protocol. Here the class when inheriting Equatable protocol class Test: Equatable { var value = 0 init(_ initialValue:Int) { value = initialValue } static func == (lhs:Test, rhs:Test) -> Bool { return lhs.value == rhs.value ? true : false } } let test1 = Test(0) var test4:Test? = nil if

How to understand `!` and `?` in swift? [duplicate]

▼魔方 西西 提交于 2019-12-11 05:39:34
问题 This question already has answers here : What is the difference between String? and String! (two ways of creating an optional variable)? (7 answers) Closed 2 years ago . I'm new in swift.When i declare a variable,or when i get a property of an instance,I find "!" and "?" is everywhere.Doing such things in Objective-C is quite easy,you can even not know the type of class with the "id" type.Why do we need the ! and the ? ? I want to know the reason of designing ! and ? ,instead of how to use

Cached Optional<Boolean> values

£可爱£侵袭症+ 提交于 2019-12-11 04:46:14
问题 There doesn't seem to be any cached objects of type Optional<Boolean> for the true and false values available in the standard library. Am I missing them somewhere? It would surprise me if there were no such objects because it seems to me like this would be so useful, for both clarity and performance. If there really are no such objects, why is that? 回答1: There is no reason to fix a particular optimization strategy into the API. Optional instances are acquired via a factory method and it’s

C2143 syntax error when including boost/optional.hpp

痞子三分冷 提交于 2019-12-11 03:53:00
问题 I'm stuck with a compile-time error which I cannot understand. I try to use boost::optional in my code, and as soon as I include boost/optional.hpp I cannot build my project any longer. If I comment this include statement out, it works. I don't even have any actual usage of boost::optional in my code yet, just the include statement in the class header (see full header below). The compiler error is C2143 syntax error: missing ',' before '<' which happens in another Boost header boost/utility