optional

Swift: handling an unexpected nil value, when variable is not optional [duplicate]

房东的猫 提交于 2019-12-06 12:01:18
This question already has answers here : Check if property is set in Core Data? (2 answers) Closed 4 years ago . I have a UITableViewController loading its entries from Core Data via a NSFetchedResultsController . Like this: let historyItem = fetchedResults.objectAtIndexPath(indexPath) as HistoryItem historyItem has a title property defined like this: @NSManaged var title: String So in cellForRowAtIndexPath the code says cell?.textLabel?.text = historyItem.title and that should all be fine. title is not an optional and does not need unwrapping. However, in the past, the stored Core Data has

Optional Binding on Implicitly Unwrapped Optional

你说的曾经没有我的故事 提交于 2019-12-06 11:27:40
问题 Swift Programming Guide says "You can also use an implicitly unwrapped optional with optional binding, to check and unwrap its value in a single statement". Why do you need to use optional binding when the value is already unwrapped? Does option binding unwrap it again? 回答1: Calling an implicitly unwrapped is the same as calling a regular optional with ! after it. It can still hold a nil value and calling it when it's nil will result in a runtime error, so you use the if let optional binding

Java8 lambda for checking two conditions

删除回忆录丶 提交于 2019-12-06 10:53:17
问题 I have the following code snippet which I would try to change to the lambda function. if(catList != null && catList.size() > 0) { animalType = AnimalType.CAT; HttpEntity<List<?>> request = new HttpEntity<>(catList, headers); response = restTemplate.postForObject(apiUrl, request, String.class); } else if(dogList != null && dogList.size() > 0) { animalType = AnimalType.DOG; } else { return; } Somehow I have written like as shown below, but don't know to incorporate the dogList checking the

iOS/Swift: Can't assign optional String to UILabel text property

徘徊边缘 提交于 2019-12-06 10:01:43
问题 UILabel has a text property, which is an optional String, but it seems to be behaving like an implicitly unwrapped optional. Why can't I assign it another optional String? Thanks. @IBOutlet weak var tweetContent: UILabel! ... var unopt: String = "foo" var opt: String? = "bar" var opt2: String? opt2 = opt //Works fine cell.tweetContent.text? = unopt //Works fine cell.tweetContent.text? = opt //Compile error: Value of optional type 'String?' not unwrapped 回答1: You don't need to unwrap text .

accessing nested dictionary from api in swift

你说的曾经没有我的故事 提交于 2019-12-06 09:47:40
问题 Holy cow...there MUST be a better way to access formatted in floorplan_summary: { bedrooms: { low: 1, high: 2, formatted: "1 - 2 Beds" } } than doing this: if data["floorplan_summary"]?["bedrooms"] != nil { let bedrooms = data["floorplan_summary"]?["bedrooms"] as NSDictionary if bedrooms["formatted"] != nil{ self.beds = bedrooms["formatted"] as String } } I want to just do this: self.beds = data["floorplan_summary"]?["bedrooms"]?["formatted"] as String ..but at each level the object seems to

Java 是如何优雅地处理NPE问题的

喜夏-厌秋 提交于 2019-12-06 08:43:58
1. 前言 对于 Java 开发者来说, null 是一个令人头疼的类型,一不小心就会发生 NPE (空指针) 问题。也是 Java 语言为人诟病的一个重要原因之一。在我们消除可恶的 NPE 问题之前我们要回顾一下 Java 中 null 的概念。 2. Java 中的 null 翻译自 Oracle Java 文档 Java语言中有两种类型,一种是 基本类型 ,另一种是 引用类型 。还有一种没有名字的特殊类型,即表达式 null 。 由于 null 类型没有名称,所以不可能声明为 null 类型的变量或者转换为 null 类型。 null 引用是 null 类型表达式唯一可能的值。 null 引用可以转换为任意引用类型。 事实上,程序员可以忽略 null 类型,可以认为 null 仅仅是一个可以成为任何引用类型的特殊符号。 从上面的描述我们可以了解到, 其实 null 仅仅是一个关键字标识量,既不是一种类型也不算对象,无法直接声明 null 和被转换为 null,仅仅只能被引用,null 可以转换为任何引用类型。当一个 Java 引用类型对象被引用为 null 时代表当前对象不引用对象,并没有为其分配内存。 这也是我们在没有引用的对象上调用方法出现空指针的根本原因。 大多数情况下 Java 开发者使用 null 是为了表示某种不存在的意思。 3. NPE 问题的解决

Linux 查看磁盘使用情况

扶醉桌前 提交于 2019-12-06 06:47:10
通过 DU/DF 可以查看磁盘使用情况 DU 通过搜索文件来计算每个文件的大小然后累加,du能看到的文件只是一些当前存在的,没有被删除的。他计算的大小就是当前他认为存在的所有文件大小的累加和。 用法:du [选项]... [文件]...  或:du [选项]... --files0-from=F Summarize disk usage of the set of FILEs, recursively for directories. 必选参数对长短选项同时适用。 -0, --null end each output line with NUL, not newline -a, --all write counts for all files, not just directories --apparent-size print apparent sizes, rather than disk usage; although the apparent size is usually smaller, it may be larger due to holes in ('sparse') files, internal fragmentation, indirect blocks, and the like -B, --block-size=SIZE scale sizes by

Is adapting a no-arg method into a Consumer bad form?

好久不见. 提交于 2019-12-06 06:30:35
Someone raised a question on another SO Answer about whether it is bad practice or inefficient to do this: Optional<User> user = ... user.ifPresent(u -> doSomethingWithoutUser()); instead of if (user.isPresent()) doSomethingWithoutUser(); Specifically, the fact that we're adapting a zero-arg method into a Consumer<User> which ignores its parameter u . As this isn't a Stream non-terminal operation, the fact doSomethingWithoutUser() likely has side-effects isn't a concern. I'm not bothered about the specifics of this one-line Optional example, it could the result of a long chain of functional

What's the difference between Optional<T> and optional types in Swift? Extending Optional to carry error information?

 ̄綄美尐妖づ 提交于 2019-12-06 06:11:45
Update - there is no difference between Optional and optional types in Swift - they are the same thing. So in Swift they introduced the Type? construct, that creates an optional type which "forces" the programmer to check if the value actually exists. Also, in Swift there is no exception handling. But there is this built-in optionality mechanism. This optional feature ? is just an Optional<T> enum behind the scenes inspired from Haskell's Maybe . I was wondering if there was any way of passing error information through the optional type. Can "abc123".toInt() return error information through

Swift - Optional Void

自作多情 提交于 2019-12-06 05:43:40
问题 I was busy using NSURLProtocolClient 's URLProtocol function: welf?.client?.URLProtocol(welf!, didReceiveResponse: operation.response, cacheStoragePolicy: NSURLCacheStoragePolicy.NotAllowed) I was expecting it to return Void . But to my surprise it returns Void? Why is it necessary to make a distinction between Void and Void? I have read that Void is a type alias for the empty tuple type. So, does this have something to do with a distinction between the empty tuple type vs nil? 回答1: This is