optional-chaining

Is Swift optional chaining always done with the if let construction, or is it just done using a question mark with an optional?

試著忘記壹切 提交于 2021-02-07 03:55:45
问题 As per Apple docs, optional chaining is the following: You specify optional chaining by placing a question mark (?) after the optional value on which you wish to call a property, method or subscript if the optional is non-nil. ... optional chaining fails gracefully when the optional is nil ... My interpretation of this is that a construction as the following is optional chaining: someMasterObject.possiblyNilHandler?.handleTheSituation() ...and that the above line would call the

Is Swift optional chaining always done with the if let construction, or is it just done using a question mark with an optional?

怎甘沉沦 提交于 2021-02-07 03:54:11
问题 As per Apple docs, optional chaining is the following: You specify optional chaining by placing a question mark (?) after the optional value on which you wish to call a property, method or subscript if the optional is non-nil. ... optional chaining fails gracefully when the optional is nil ... My interpretation of this is that a construction as the following is optional chaining: someMasterObject.possiblyNilHandler?.handleTheSituation() ...and that the above line would call the

Is Swift optional chaining always done with the if let construction, or is it just done using a question mark with an optional?

主宰稳场 提交于 2021-02-07 03:54:08
问题 As per Apple docs, optional chaining is the following: You specify optional chaining by placing a question mark (?) after the optional value on which you wish to call a property, method or subscript if the optional is non-nil. ... optional chaining fails gracefully when the optional is nil ... My interpretation of this is that a construction as the following is optional chaining: someMasterObject.possiblyNilHandler?.handleTheSituation() ...and that the above line would call the

How to get optional chaining working in TypeScript?

纵然是瞬间 提交于 2021-02-06 10:48:21
问题 Looks like optional chaining has landed. Here's an example What I can't figure out is how to get TS to compile it properly. I'm not getting any syntax errors in my project, but this: let imageFileId = (await db.query(sql`select id from image_files where sha256=${sha256}`))[0]?.id; Is being output as: let imageFileId = (await db.query(mysql3_1.sql `select id from image_files where sha256=${sha256}`))[0]?.id; Which won't run until we get native support in Node. Here's my tsconfig: {

Using optional chaining operator for object property access

那年仲夏 提交于 2020-12-25 01:36:23
问题 TypeScript 3.7 now supports the optional chaining operator. Hence, you can write code such as: const value = a?.b?.c; I.e., you can use this operator to access properties of an object, where the object itself may be null or undefined . Now what I would like to do is basically the same, but the property names are dynamic: const value = a?[b]?.c; However, there I get a syntax error: error TS1005: ':' expected. What am I doing wrong here? Is this even possible? PS: The proposal seems to imply

How to make webpack accept optional chaining without babel

感情迁移 提交于 2020-06-27 15:01:13
问题 Scenario: We're using webpack 4 to create a bundle from our Javascript sources. We're not using Babel because we're authoring only for a single platform (latest Chrome), and we're only using features directly available in Chrome, thus no transpiling is required. The plus side of this is a smaller bundle , and much faster turnaround times while developing. Now we would like to start using the stage 4 optional chaining feature which can be enabled in Chrome using a flag. I've tried to google

What are '!' and '?' marks used for in Swift

人走茶凉 提交于 2019-12-24 13:35:43
问题 When I changed from Objective-C to Swift programming, I have come across ' ! ' (exclamation mark) and ' ? ' (question mark) that often have to be put right after a property , a method call etc. What are these marks used for, and what happens if we did not use them? I searched for a thorough answer on the web but could not find any. So I decided to put here my answer in case for anyone is in search of a clear answer for that. I also included in my answer below a link to a question that is

Optional Chaining not enabled ReactNative

帅比萌擦擦* 提交于 2019-12-23 12:51:05
问题 I am getting this error when running the android project in react react native. This is fresh install of react native version "react": "^16.3.1","react-native": "^0.57.1", It gives error of optional chaining. Can anyone please help me how to enable optional chaining in react native. Loading dependency graph, done. BUNDLE [android, dev] ....../index.js ▓▓▓▓▓▓▓▓▓▓░░░░░░ 64.3% (667/832)::ffff:127.0.0.1 - - [02/Oct/2018:04:30:46 +0000] "GET /index.delta?platform=android&dev=true&minify=false HTTP

What is the difference when change to use optional chaining replace forced unwrapping in swift?

巧了我就是萌 提交于 2019-12-20 05:23:42
问题 When call a function of an object instance, the object may be not exist(optional type), it seems like you can always put an question mark behind the object name, instead of put an exclamation mark behind the object name, and not crash. window!.rootViewController = containerViewController // forced unwrapping // Can change to question mark and not crash. window?.rootViewController = containerViewController // Optional chaining Is that in the place of use forced unwrapping, you can always

Get the negative of an Optional Chain

匆匆过客 提交于 2019-12-11 04:06:07
问题 This is not allowed (bad expression) if !(let nsDictionaryObject = swiftObject as? NSDictionary) { "Error could not make NSDictionary in \(self)" return } Is it possible to check for the negative of an Optional Chain expression in 1 line? 回答1: In Swift 2.0, you can use guard let nsDictionaryObject = swiftObject as? NSDictionary else { "Error could not make NSDictionary in \(self)" return } This will also bind nsDictionaryObject to the scope outside the guard statement. 回答2: If you are using