optional

Implicitly Unwrapped Optionals in Swift does not seem to work

女生的网名这么多〃 提交于 2020-01-05 08:22:11
问题 I am following the The Swift Programming Language book to investigate strong reference cycle. One of the examples that should be working cannot be compiled in Xcode. I don't understand why the code is broken. On this chapter, there is an example that looks like this: When I try to compile this code in Xcode the this error was thrown: 'self' used before all stored properties are initialized . However, I think it should have been able to compile because I set capitalCity to be Implicitly

How to execute with optional argument in Spyder? [duplicate]

半城伤御伤魂 提交于 2020-01-05 07:12:31
问题 This question already has answers here : How to use argv with Spyder (4 answers) Closed last year . I don't know how to execute a program with optional arguments on Spyder. I know how to pass variables to it, but my program uses argparse, and I want to execute it with the "-h" or "--help" option, the code is the following one import argparse parser = argparse.ArgumentParser() parser.parse_args() For now, it only has the default optional argument of "-h"/"--help", I tried putting it on

How to execute with optional argument in Spyder? [duplicate]

偶尔善良 提交于 2020-01-05 07:12:10
问题 This question already has answers here : How to use argv with Spyder (4 answers) Closed last year . I don't know how to execute a program with optional arguments on Spyder. I know how to pass variables to it, but my program uses argparse, and I want to execute it with the "-h" or "--help" option, the code is the following one import argparse parser = argparse.ArgumentParser() parser.parse_args() For now, it only has the default optional argument of "-h"/"--help", I tried putting it on

How to check for availability for a nested variable without checking all the preceding variable availability, in ReactNative?

跟風遠走 提交于 2020-01-05 06:49:12
问题 For example, in iOS Swift, I can do something like this: if (self.user?.company?.pic?.phoneNumber != null) { doSomething() } Without the need to: if (self.user != null && self.user!.company != null && self.user!.company!.pic != null && self.user!.company!.pic!.phoneNumber != null) { doSomething() } In ReactNative (or Javascript), I found out that if an object is undefined, I can't check for the existence of the variable inside of it, so I have to check first whether the object is undefined or

Swift: Can't unwrap an optional inside a loop

送分小仙女□ 提交于 2020-01-05 04:37:12
问题 I can't understand why unwrapping an optional value is possible in this case: let name: String? = "Toto" guard let name = name else { fatalError() } print(name) but not when this snippet is wrapped in a for-loop: for _ in 0..<100 { let name: String? = "Toto" guard let name = name else { fatalError() } print(name) } I got the error "Definition conflicts with previous value". Using Swift 5 in Xcode 11.0. 回答1: As explained in Why isn't guard let foo = foo valid?, let name: String? = "Toto" guard

Why using implicit unwrap or force unwrap letting the app crash at some stage not beneficial?

青春壹個敷衍的年華 提交于 2020-01-05 04:22:06
问题 My point is at some places we know that the variable won't have nil at all but due to some reason we can't instantiate it in the class's init function so we have to make it optional. I also know that we can use optional binding or guard techniques to get rid of it easily. But letting the app crash at some really stupid mistakes because of the implicit unwrap/force unwrap is beneficial for the developers at the developing phase in my opinion . My example would be: class

Why Swift disallows weak reference for non-optional type?

巧了我就是萌 提交于 2020-01-04 15:56:08
问题 This is not pure curiosity, there is a feeling that I may misunderstand something about weak references in Swift. Suppose I create a class from a View Controller and pass its reference to the initialiser: class = MyClass(vc: self) Since the storyboard and window already keep a reference to this View Controller, it seems logical for MyClass to have a weak reference to it (for the similar reason all references created within IB are weak by default): class MyClass: NSObject { private weak var

Swift - C API bridge - how to handle null pointers

大城市里の小女人 提交于 2020-01-04 01:59:29
问题 In Swift I am using C API that returns struct with char array (containing UTF8 null terminated string or null). struct TextStruct { char * text; //other data } I use: let text: String = String(cString: data.text) This works, however, when data.text is nullptr , this fails with fatal error: unexpectedly found nil while unwrapping an Optional value Is there any workaround, or I have to check data.text manually before using cString ctor? 回答1: In addition to Gwendal Roué's solution: You can

Optional get value if present

戏子无情 提交于 2020-01-03 17:24:06
问题 As an example I have an optional like this: Optional<Desktop> opt = Optional.ofNullable(status).map(Status::getDesktop); I want to have the desktop and work with it outside of lambda expressions. I do it like this: if (opt.isPresent()){ Desktop desktop = opt.get(); ... } Is there a better solution to get the desktop; something like this ? Desktop desktop = Optional.ofNullable(status).map(Status::getDesktop).ifPresent(get()); EDIT: OrElse was the method I was looking for :) 回答1: If you have a

Mockito.when().thenReturn() doesn't work or returns null

混江龙づ霸主 提交于 2020-01-03 16:57:28
问题 During the test there is a NullPointerException thrown. I tried to debug it and the only thing I worked out was that eventOptional is always null. Just as if Mockito.when().thenReturn() didn't work. Can anybody help? Here's my code for a tested service and for the test itself: @Service public class EventService { @Autowired public EventService(EventRepository eventRepository) { this.eventRepository = eventRepository; } //... public void updateEvent(EventDTO eventDTO) { Optional<Event>