gpt4 book ai didi

objective-c - 使用@[array, of, items] vs [NSArray arrayWithObjects :]

转载 作者:太空狗 更新时间:2023-10-30 03:23:00 25 4
gpt4 key购买 nike

有区别吗

NSArray *myArray = @[objectOne, objectTwo, objectThree];

NSArray *myArray = [NSArray arrayWithObjects:objectOne, objectTwo, objectThree, nil];

一个比另一个更受欢迎吗?

最佳答案

它们几乎相同,但不完全相同。 Objective-C Literals 上的 Clang 文档状态:

Array literal expressions expand to calls to +[NSArray arrayWithObjects:count:], which validates that all objects are non-nil. The variadic form, +[NSArray arrayWithObjects:] uses nil as an argument list terminator, which can lead to malformed array objects.

所以

NSArray *myArray = @[objectOne, objectTwo, objectThree];

如果 objectTwo == nil 会抛出运行时异常,但是

NSArray *myArray = [NSArray arrayWithObjects:objectOne, objectTwo, objectThree, nil];

在这种情况下会创建一个只有一个元素的数组。

关于objective-c - 使用@[array, of, items] vs [NSArray arrayWithObjects :],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14527489/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com