是否可以像这样同时使用可选链接和零合并?
\n\nprint("Meeting host: " + meeting.host?.email ?? \xe2\x80\x9cNo host\xe2\x80\x9d)\nRun Code Online (Sandbox Code Playgroud)\n\n我想这样做,但我收到一条错误消息,说我的字符串?没有打开。email是一个非可选字符串。
这是否可以在无需host事先打开包装的情况下实现?如果不是,为什么我的尝试不起作用?
当我发现可选链( ?.) 时,我正在编写一些 Javascript。我决定在我正在编写的一些代码中需要它。当我输入完代码后,我注意到 JSHint 给了我一个错误,指出Expected an identifier and instead saw '.'. (E030) jshint(E030). 下面的代码运行没有任何错误(MDN兼容性表),但JSHint仍然给出警告。
var x = {
y: {
z: 123
}
};
console.log(x.y?.z)Run Code Online (Sandbox Code Playgroud)
我发现了另一个与此相关的StackOverflow 问题,但该问题专门询问了 ESLint,而该问题是关于 JSHint。我还搜索了 JSHint GitHub 存储库的问题选项卡,但没有找到任何内容。有什么办法可以抑制这种错误吗?我正在使用 Visual Studio Code Insider。
编辑信息取自Code - Insiders > About Visual Studio Code - Insiders:
Version: 1.48.0-insider
Commit: d13d2fc56da7a2f8bcad4256212db0661fcbba45
Date: 2020-08-05T05:26:44.946Z (20 hrs ago)
Electron: 7.3.2
Chrome: 78.0.3904.130
Node.js: 12.8.1
V8: 7.8.279.23-electron.0
OS: Darwin x64 19.5.0
Run Code Online (Sandbox Code Playgroud) 这是我如何返回表视图的行数:
public func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if let dataCount = self.data?.count {
return dataCount
} else {
return 0
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我想使用可选链接使其更简洁......但编译器对我的代码不满意.我试试这个:
return self.data?.count
Run Code Online (Sandbox Code Playgroud)
并且它抱怨计数是Int类型?我应该强行打开它,所以我这样做:
return self.data?.count!
Run Code Online (Sandbox Code Playgroud)
并且它抱怨count是Int类型.使用可选链接,如果数据不是nil,它应该只得到计数,如果数组不是nil,那么我知道count将返回ok.
我错过了什么?谢谢
这是不允许的(坏表达)
if !(let nsDictionaryObject = swiftObject as? NSDictionary)
{
"Error could not make NSDictionary in \(self)"
return
}
Run Code Online (Sandbox Code Playgroud)
是否可以在1行中检查可选链表达式的否定值?
像下面的示例一样,使用可选链进行 null 检查是一种好的做法吗?
document.querySelector('.foo')?.classList.add('bar');
Run Code Online (Sandbox Code Playgroud)
在许多代码库中我看到了这一点:
let el = document.querySelector('.foo');
if(el){
el.classList.add('bar');
}
Run Code Online (Sandbox Code Playgroud)
我认为链接更加干净,并且在这两种情况下都会发生无声的失败。我知道浏览器支持。
我去那儿:
buyTicketData?.pricingOptions
Run Code Online (Sandbox Code Playgroud)
这个错误:
[tsl] ERROR in /Applications/MAMP/htdocs/wp-content/plugins/tikex/tikexModule/components/BuyTicket/PricingOptionInvoiceItemsFormFieldsCheckboxes.tsx(280,25)
TS2532: Object is possibly 'undefined'.
Run Code Online (Sandbox Code Playgroud)
如果左侧?未定义,?则将其包裹起来,为什么这很重要?
以下是类型:
buyTicketData?: BuyTicketData;
export type BuyTicketData = {
pricingOptions?: PricingOptions;
}
export type PricingOptions = {
[optionId: string]: PricingOptionType;
};
export type PricingOptionType = {
invoiceItems?: InvoiceItems;
};
export type InvoiceItems = {
[invoiceItemId: string]: InvoiceItemData;
};
export type InvoiceItemData = {
defaultValue?: number;
};
Run Code Online (Sandbox Code Playgroud)
无论如何,这就是整个表达式
buyTicketData?.pricingOptions
Run Code Online (Sandbox Code Playgroud)
好的,找到解决方案:
[tsl] ERROR in /Applications/MAMP/htdocs/wp-content/plugins/tikex/tikexModule/components/BuyTicket/PricingOptionInvoiceItemsFormFieldsCheckboxes.tsx(280,25)
TS2532: Object is possibly 'undefined'.
Run Code Online (Sandbox Code Playgroud)
我只是不知道为什么?? ""需要这种丑陋的条件。
我想使用可选的更改运算符 (?.) 来增加对象值。在我的示例中,仅当为当前人设置了年龄时,我才想在名为birthday()的函数中将人的年龄增加一。带有 if 子句的行可以正常工作(也可以明显地使用十进制 if )。不过我想知道类似下面的行是否可能。给定的语法会导致语法错误:无效的递增/递减操作数。也许这是一个基于运算符优先级的问题,但我现在知道如何解决它。
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
}
let peter = new Person("Peter", 27);
let paul = new Person("Paul", undefined);
Person.prototype.birthday = function() {
if (this.age) this.age++;
};
//Person.prototype.birthday = function(){this?.age++};
peter.birthday();
paul.birthday();
console.log(peter, paul);Run Code Online (Sandbox Code Playgroud)
我无法使用流和 flatMap 使我的方法空安全。
我的输入是List<RequestSlot>
对象是这样嵌套的:
Request
OuterItem
List<InnerItem>
Run Code Online (Sandbox Code Playgroud)
这正在努力展平列表并创建一个列表,但我不确定如何在此处添加选项以使此 null 对于每个映射/流步骤都是安全的。
final List<InnerItem> innerItem = request.stream()
.map(RequestSlot::getOuterItem)
.flatMap(outerItem -> outerItem.getInnerItem().stream()).collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
我尝试过使用 Stream.ofNullable 但最终创建了一个我不确定如何收集的流。有什么指点吗?
我从MDN中逐字复制粘贴的示例代码,但可选链接在我的节点(v12.13.0)REPL 中不起作用。抛出语法错误,指出问号后面的点无效。这是怎么回事 ?我已经在 React 应用程序中使用了这个表达式,它似乎工作得很好。
const adventurer = {
name: 'Alice',
cat: {
name: 'Dinah'
}
};
const dogName = adventurer.dog?.name;
console.log(dogName);
// expected output: undefined
console.log(adventurer.someNonExistentMethod?.());
// expected output: undefined
Run Code Online (Sandbox Code Playgroud) 在多个 null 条件中使用Java 8 可选功能
条件是
Optional<Payee> payee = Optional.ofNullable(paymtTrans.getPayee());
if (payee.isPresent()
&& payee.get().getPayeeAcc() != null
&& payee.get().getPayeeAcc().size() > 0
&& payee.get().getPayeeAcc().get(0).getBnk() != null
&& payee.get().getPayeeAcc().get(0).getBnk().getBnkCtryCode() != null) {
String ctryCode = payee.get().getPayeeAcc().get(0).getBnk().getBnkCtryCode();
}
Run Code Online (Sandbox Code Playgroud)
要求是使用 Java 8 功能而不检查空值!!
需要获取ctryCode吗?
javascript ×5
swift ×3
ios ×2
java ×2
java-8 ×1
java-stream ×1
jshint ×1
node.js ×1
operators ×1
option-type ×1
optional ×1
syntax ×1
typescript ×1