optional

Java Lambda - check if an ArrayList to Stream is empty

不羁岁月 提交于 2019-12-05 04:29:25
I have the following lambda expression and if works fine when bonusScheduleDurationContainers is not empty. If it is empty, I get a NoSuchElementException . How do I check this in the lambda expression? final List<ScheduleDurationContainer> bonusScheduleDurationContainers = scheduleDurationContainersOfWeek.stream() .filter(s -> s.getContainerType() == ScheduleIntervalContainerTypeEnum.BONUS) .collect(Collectors.toList()); final ScheduleDurationContainer bonusScheduleDurationContainer = bonusScheduleDurationContainers.stream() .filter(s -> s.getDayOfWeekStartingWithZero() == dayOfWeekTmp)

Sorting nil Dates to the end of an Array

 ̄綄美尐妖づ 提交于 2019-12-05 04:25:05
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. 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/sorting-nil-dates-to-the-end-of-an-array

Why do we need to explicitly cast the optional to Any?

南楼画角 提交于 2019-12-05 03:59:36
问题 According to Apple Doc, The Any type represents values of any type, including optional types. Swift gives you a warning if you use an optional value where a value of type Any is expected. If you really do need to use an optional value as an Any value, you can use the as operator to explicitly cast the optional to Any, as shown below. var things = [Any]() things.append(3) // No warning let optionalNumber: Int? = 3 things.append(optionalNumber) // Warning, even though Any also represents

Convert optional string to int in Swift

不羁的心 提交于 2019-12-05 02:49:32
I am having troubles while converting optional string to int. println("str_VAR = \(str_VAR)") println(str_VAR.toInt()) Result is str_VAR = Optional(100) nil And i want it to be str_VAR = Optional(100) 100 Dharmesh You can unwrap it this way: if let yourStr = str_VAR?.toInt() { println("str_VAR = \(yourStr)") //"str_VAR = 100" println(yourStr) //"100" } Refer THIS for more info. When to use “if let”? if let is a special structure in Swift that allows you to check if an Optional holds a value, and in case it does – do something with the unwrapped value. Let’s have a look: if let yourStr = str

Swift tuple to Optional assignment

一笑奈何 提交于 2019-12-04 23:58:24
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 = networkResultBody.dataUsingEncoding(NSUTF8StringEncoding)! (self.status, self.message) = self.parseResponse

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

青春壹個敷衍的年華 提交于 2019-12-04 23:50:08
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? Joe Huang 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 Swift, I have found that using optional variables indeed provides a lot of convenience to avoid

Swift optional in label

倖福魔咒の 提交于 2019-12-04 22:26:07
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? 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 ... Infact if you decompose your line... let fundsreceived = String(stringInterpolationSegment: self.campaign?[

Mockito error with method that returns Optional<T>

半腔热情 提交于 2019-12-04 22:22:13
I have an interface with the following method public interface IRemoteStore { <T> Optional<T> get(String cacheName, String key, String ... rest); } The instance of the class implementing the interface is called remoteStore. When I mock this with mockito and use the method when: Mockito.when(remoteStore.get("a", "b")).thenReturn("lol"); I get the error: Cannot resolved the method 'thenReturn(java.lang.String)' I thought it has to do with the fact that get returns an instance of the Optional class so I tried this: Mockito.<Optional<String>>when(remoteStore.get("cache-name", "cache-key"))

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

天大地大妈咪最大 提交于 2019-12-04 22:08:43
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)? 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? = 1 if let lvalue:T = rvalue { ... } // do nothing because `rvalue` is already `T?` //--- typealias T =

How to know where Optional Chaining is breaking?

試著忘記壹切 提交于 2019-12-04 21:17:45
问题 So in iOS Swift we can do optional chaining to simplify the nil checking like in the official documentation let johnsAddress = Address() johnsAddress.buildingName = "The Larches" johnsAddress.street = "Laurel Street" john.residence!.address = johnsAddress if let johnsStreet = john.residence?.address?.street { println("John's street name is \(johnsStreet).") } else { println("Unable to retrieve the address.") } // prints "John's street name is Laurel Street." I understand about the usage of