optional

Swift Optional of Optional

。_饼干妹妹 提交于 2019-12-17 20:28:54
问题 For a project of mine, I have to create a proxy AppDelegate that will forward calls to a another AppDelegate. UIApplicationDelegate havs a var window: UIWindow? . My question is, why can't I do this: private lazy var realAppDelegate: UIApplicationDelegate = { return AppDelegate() }() var window: UIWindow? { get { return realAppDelegate.window } set { realAppDelegate.window = newValue } } The problem with that code is that realAppDelegate.window is a UIWindow?? . Does anybody know why? 回答1:

Optional Getting Field

筅森魡賤 提交于 2019-12-17 20:22:20
问题 I have a class structure like this: public class Foo { private FooB foob; public Optional<FooB> getFoob() { return Optional.ofNullable(foob); } } public class FooB { private int valA; public int getValA() { return valA; } } My objective is to call the get method for fooB and then check to see if it's present. If it is present then return the valA property, if it doesn't then just return null. So something like this: Integer valA = foo.getFoob().ifPresent(getValA()).orElse(null); Of course

JDK1.8重温

拈花ヽ惹草 提交于 2019-12-17 18:47:59
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 1.8带来最核心的特性: 1.lambda(匿名函数) 基本上就是一个没有名称的方法,但和匿名类一样,它可以作为参数传递给一个方法。 1.lambda表达式的三部分:参数列表,箭头->,方法体。 2.使用场景: 只有在接受函数式接口(Compartor<T>,Runnable,Callable<V>等 以及1.8中function包中引入的几个新的函数式接口 Predicate<T>,Consumer<T>和Function<T,R>,Supplier<T>)上使用Lambda表达式。函数式接口的定义:只定义一个抽象方法的接口。 允许直接使用内联的形式为函数式接口的抽象方法提供实现,把整个表达式作为函数式接口的实例 List<Apple> greenApples = filter(inventory, (Apple a) -> "green".equals(a.getColor())); 3.方法引用 方法引用可以重复使用现有的方法定义,并像lambda一样传递给它们。 inventory.sort((Apple a1, Apple a2) -> a1.getWeight().compareTo(a2.getWeight())); inventory.sort(comparing(Apple:

Avoid NoSuchElementException with Stream

主宰稳场 提交于 2019-12-17 18:23:30
问题 I have the following Stream : Stream<T> stream = stream(); T result = stream.filter(t -> { double x = getX(t); double y = getY(t); return (x == tx && y == ty); }).findFirst().get(); return result; However, there is not always a result which gives me the following error: NoSuchElementException: No value present So how can I return a null if there is no value present? 回答1: You can use Optional.orElse, it's much simpler than checking isPresent : T result = stream.filter(t -> { double x = getX(t)

Check string for nil & empty

社会主义新天地 提交于 2019-12-17 17:38:05
问题 Is there a way to check strings for nil and "" in Swift? In Rails, I can use blank() to check. I currently have this, but it seems overkill: if stringA? != nil { if !stringA!.isEmpty { ...blah blah } } 回答1: If you're dealing with optional Strings, this works: (string ?? "").isEmpty The ?? nil coalescing operator returns the left side if it's non-nil, otherwise it returns the right side. You can also use it like this to return a default value: (string ?? "").isEmpty ? "Default" : string! 回答2:

Using multiple let-as within a if-statement in Swift

大憨熊 提交于 2019-12-17 17:26:21
问题 I'm unwrapping two values from a dictionary and before using them I have to cast them and test for the right type. This is what I came up with: var latitude : AnyObject! = imageDictionary["latitude"] var longitude : AnyObject! = imageDictionary["longitude"] if let latitudeDouble = latitude as? Double { if let longitudeDouble = longitude as? Double { // do stuff here } } But I would like to pack the two if let queries into one. So that it would something like that: if let latitudeDouble =

Swift : non-nil optional value raising a nil exception

[亡魂溺海] 提交于 2019-12-17 17:17:43
问题 I'm having a dictionary, with values, i'm calling it to populate a field if let userdata: NSDictionary = self.fbdata { println(userdata["email"]) // print Optional(email@domain.com) vc.email.text = userdata["email"] as? String ?? "" // raise a nil error } As put in the code, the userdata["email"] exists and has a value, printed by println, anyway at the next line i have a nil optional exception raised (and even the default "" value isn't used) I don't see what i'm doing wrong here 回答1: it's

Is it a good practice to use Optional as an attribute in a class? [duplicate]

心已入冬 提交于 2019-12-17 15:42:06
问题 This question already has answers here : Uses for Optional (13 answers) Guava Optional as method argument for optional parameters (1 answer) Closed 4 years ago . I have read something about the purpose of Optional (unfortunately I don't remember where) in Java 8, and I was surprised the writer didn't mention the use of an Optional as an attribute in a class. Since I am using optionals pretty frequently in my classes, I was wondering if this is a good practice. Or could I better just use

Optional return in C#.Net

有些话、适合烂在心里 提交于 2019-12-17 10:36:09
问题 Java 1.8 is receiving the Optional class, that allows us to explicitly say when a method may return a null value and "force" its consumer to verify if it is not null ( isPresent() ) before using it. I see C# has Nullable, that does something similar, but with basic types. It seems to be used for DB queries, to distinguish when a value exists and is 0 from when it doesn't exist and is null. But it seems that C#'s Nullable doesn't work for objects, only for basic types, while Java's Optional

Why does findFirst() throw a NullPointerException if the first element it finds is null?

浪子不回头ぞ 提交于 2019-12-17 06:06:47
问题 Why does this throw a java.lang.NullPointerException ? List<String> strings = new ArrayList<>(); strings.add(null); strings.add("test"); String firstString = strings.stream() .findFirst() // Exception thrown here .orElse("StringWhenListIsEmpty"); //.orElse(null); // Changing the `orElse()` to avoid ambiguity The first item in strings is null , which is a perfectly acceptable value. Furthermore, findFirst() returns an Optional, which makes even more sense for findFirst() to be able to handle