optional

Why doesn't redeclaration of 'optional binding' create an error?

你说的曾经没有我的故事 提交于 2019-12-13 07:15:52
问题 I am doing this in Playground, but I am not getting any error. Am I not recreating constant range ? Is it happening in 2 different scopes? What's happening in the background that makes this not an error? if let range = add1.rangeOfString(", ") { print(add1.substringToIndex(range.startIndex)) print (range) } if let range = add1.rangeOfString(", ") { print(add1.substringToIndex(range.startIndex)) print (range) } 回答1: Variables introduced with Optional binding of if-let is local after the let

Constructor with Optional<String>

大城市里の小女人 提交于 2019-12-13 05:58:12
问题 I have written the following code: void Test(A a) { B b = new B(a.getName()); } So, the constructor of B expects a String . It looks like the following: protected B (String name) { super(name, KIND); this.name = name; } But a.getName() gives me a name with Optional<String> as return value and I do not want to change that. Therefore, I try to change the parameter of the constructor B (I replace String name with Optional<String> ), but then Eclipse underlines super(name, KIND) and this.name =

How to check if nested optional is nil in generic class?

感情迁移 提交于 2019-12-13 03:38:50
问题 I have simple class: class Values<T> { let new: T let old: T? init(new: T, old: T? = nil) { self.new = new self.old = old } func changed<TProp: AnyObject>(_ getter: (T) -> TProp) -> Bool { return old == nil || !(getter(old!) === getter(new)) } func changed<TProp: Equatable>(_ getter: (T) -> TProp) -> Bool { return old == nil || !(getter(old!) == getter(new)) } } When using it as Values<ChartViewModelData?> where ChartViewModelData is a class i got problems when old is nested optional - it is

Swift Subscript Error

只谈情不闲聊 提交于 2019-12-12 21:43:46
问题 I believe it has something to do with optionals, but I'm safely unwrapping sourceURL so I'm still not sure where the error is! I'm trying to access a JSON object's array's dictionary value. However, I'm still getting the "could not find overload for 'subscript' that accepts the supplied arguments. It seems simple, but I just can't seem to figure it out! var dictTemp: NSDictionary! = NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers, error:

How to chain Optional#ifPresent() in lambda without nesting?

爱⌒轻易说出口 提交于 2019-12-12 14:42:59
问题 I have the following code block that requires that I check whether multiple nested variables are present. These were originally null checks that I replaced with Optional and ifPresent() . I would like to use ifPresent() instead of querying get() to mitigate potential runtime exceptions. But this is causing a lot of nesting. Can I leverage lambdas in this example to achieve the same flow without the nesting? void SomeMethod() { procA().ifPresent(a -> { procB(a).ifPresent(b -> { // Do something

Validating inputs using Optional

风格不统一 提交于 2019-12-12 11:02:33
问题 I have a CreateOrder instance which has some String, Integer and Double states in it. When I create an object for CreateOrder in my JUnit test and send it over, I am able to validate String attributes but not Integer using Optional API as follows - String aoid = Optional.ofNullable(createOrder.getAltorderid()).orElse(""); int quantity = Integer.parseInt(each.getQty()); double amount = Double.parseDouble(each.getPrice().getAmount()); Like for aoid , I also want to user ofNullable() for integer

Swift optional in label

对着背影说爱祢 提交于 2019-12-12 09:27:30
问题 I have this code right here let fundsreceived = String(stringInterpolationSegment: self.campaign?["CurrentFunds"]!) cell.FundsReceivedLabel.text = "$\(funds received)" It is printing out Optional(1000) I have already added ! to the variable but the optional isn't going away. Any idea what have i done wrong here? 回答1: This is happening because the parameter you are passing to String(stringInterpolationSegment:) is an Optional . Yes, you did a force unwrap and you still have an Optional ...

Whats the most elegant way to add two numbers that are Optional<BigDecimal>

吃可爱长大的小学妹 提交于 2019-12-12 07:37:55
问题 I need to perform an add operation on two big decimals that are wrapped optionals: Optional<BigDecimal> ordersTotal; Optional<BigDecimal> newOrder; I want to achieve ordersTotal += newOrder It's important to note that if both values are empty the result should likewise be empty (ie not zero). Here is what I came up with: ordersTotal = ordersTotal.flatMap( b -> Optional.of(b.add(newOrder.orElse(BigDecimal.ZERO)))); but I'm wondering if there's a more elegant solution. 回答1: Not sure if you'll

Allow a OneToOne relationship to be optional in Symfony2

喜你入骨 提交于 2019-12-12 05:37:24
问题 I have a form responsible of creating and updating users. A user can (or not) have an address (OneToOne unidirectional relation from user). When I create a user, no problem. When I update a user, usually no problem. Problems come up when i update a user which already has an address and try to unset all the address fields. There is then a validation error. The wanted behavior would be to have the user->address relation set to null (and delete the previously set address on the DB). There is a

Fortran Functions with optional arguments

会有一股神秘感。 提交于 2019-12-12 04:48:35
问题 I want to use an operator .ef. however the operator does not accept optional arguments. Is it possible to keep the my function and be able to have the operator working too? Module Core Implicit None Interface Operator (.ef.) Module Procedure fes End Interface Operator (.ef.) Contains Function fes & ( & nm, wn & ) & Result (located) Logical :: located Character (Len=*), Intent (In) :: nm Character (Len=*), Intent (In), Optional :: wn End Function Gfortran is returning the following problem lib