optional

More about the weird behavior of ReferenceWritableKeyPath with an Optional property

随声附和 提交于 2020-01-23 02:52:27
问题 (I say "more" in my title because this question is an appendix to my How does Swift ReferenceWritableKeyPath work with an Optional property?.) In my code, self.iv is an outlet property: @IBOutlet weak var iv: UIImageView! Now, will this compile? let im = UIImage() let kp = \UIImageView.image self.iv[keyPath:kp] = im // error No! We are told that something here must be unwrapped, although it is not entirely obvious what it is: Value of optional type 'UIImage?' must be unwrapped to a value of

How to properly return Optional<> of a method?

偶尔善良 提交于 2020-01-22 11:40:06
问题 I have read a lot of Java 8 Optional and I do understand the concept, but still get difficulties when trying to implement it myself in my code. Although I have serached the web for good examples, I didn't found one with good explanation. I have the next method: public static String getFileMd5(String filePath) throws NoSuchAlgorithmException, IOException { AutomationLogger.getLog().info("Trying getting MD5 hash from file: " + filePath); MessageDigest md = MessageDigest.getInstance("MD5");

Optional.of(null)will throw NPE, I need to verify null before call the method?

你离开我真会死。 提交于 2020-01-20 22:05:42
问题 Java 8 introduces Optional to deal with NPE. In practical application,I can‘t understand a problem. I have the method A public void doSomethingA(String para) { Optional<String> name = Optional.of(para); if (name.isPresent()) { //do } } But if para = null, it will throw NPE. method B public void doSomethingB(String para) { if (para != null) { //do } } if I check para is not null, What's the difference between A and B. Where is the meaning of Optional. 回答1: Use Optional.ofNullable if you are

Java 8 support with Jersey framework [closed]

浪子不回头ぞ 提交于 2020-01-17 05:20:09
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 3 years ago . I have a Rest Application using Jersey framework and maven. My application works well. Now I have decided to re-factor my code and use Java 8 features wherever possible. I started with Optional class. But when I use Optional.ifPresent method in my code, tomcat fails to start with Exception. Note: There are no

Why does pattern matching on &Option<T> yield something of type Some(&T)?

时光总嘲笑我的痴心妄想 提交于 2020-01-16 05:25:07
问题 I have a tiny playground example here fn main() { let l = Some(3); match &l { None => {} Some(_x) => {} // x is of type &i32 } } I'm pattern matching on &Option and if I use Some(x) as a branch, why is x of type &i32 ? 回答1: The type of the expression &l you match against is &Option<i32> , so if we are strict the patterns should be &None and &Some(x) , and if we use these patterns, the type of x indeed is i32 . If we omit the ampersand in the patterns, as you did in your code, it first looks

Understanding optional global variables in swift

别等时光非礼了梦想. 提交于 2020-01-15 06:32:09
问题 I'm working through a book on Swift and I understand the idea of scope in functions so what I'd like to understand next is why we set global variables using optional types in classes. Honestly it looks like we don't set these variables per say but just let the class know that there will be a variable of a specific type somewhere throughout the code base: var sut: ItemManager! . From what I understand the variable sut is an unwrapped optional of the type ItemManger which definitely has a valid

Broken toString of Optional without value

只谈情不闲聊 提交于 2020-01-14 06:51:07
问题 I have an certificate based authentication: import java.security.cert.X509Certificate; import java.util.Optional; import java.util.logging.Level; import java.util.logging.Logger; import javax.inject.Inject; import javax.inject.Named; import javax.naming.InvalidNameException; import javax.naming.ldap.LdapName; import javax.naming.ldap.Rdn; @Named public class UsernameService { private static final String MISSING_NAME = null; private static final Logger LOG = Logger.getLogger(UsernameService

'(NSObject, AnyObject)' is not convertible to 'String'

落花浮王杯 提交于 2020-01-13 09:31:52
问题 How do I convert an object of type (NSObject, AnyObject) to the type String ? At the end of the first line of the method below, as String causes the compiler error: '(NSObject, AnyObject)' is not convertible to 'String' Casting street to NSString instead of String compiles, but I'm casting street to String because I want to compare it to placemark.name , which has the type String! , not NSString . I know name and street are optionals, but I'm assuming they're not nil for now because all the

How to avoid using Optional.get and Optional.isPresent

半腔热情 提交于 2020-01-13 02:14:22
问题 public ValueA map(ValueB valueB, Date date) { Optional<ValueC> valueCOpt = find(valueB); if (valueCOpt.isPresent()) { ValueC valueC = valueCOpt.get(); // call many getters on valueC and do a lot of logic with it. return map(/*some parameters*/); } return null; } This seems quite ugly. The advantage of optionals is completely gone in here. I read that one should rather use map or flatMap instead of get . But is it really a benefit if I replace every getter like valueC.getFieldA() with

How does Swift ReferenceWritableKeyPath work with an Optional property?

旧时模样 提交于 2020-01-12 07:39:09
问题 Ground of Being : It will help, before reading, to know that you cannot assign a UIImage to an image view outlet's image property through the keypath \UIImageView.image . Here's the property: @IBOutlet weak var iv: UIImageView! Now, will this compile? let im = UIImage() let kp = \UIImageView.image self.iv[keyPath:kp] = im // error No! Value of optional type 'UIImage?' must be unwrapped to a value of type 'UIImage' Okay, now we're ready for the actual use case. What I'm actually trying to