gpt4 book ai didi

objective-c - Swift 在 Objective C 中的 "if let"等价物

转载 作者:搜寻专家 更新时间:2023-10-31 08:32:45 25 4
gpt4 key购买 nike

Objective C 中的“if let”等价物是什么?下面是我要转换为 Objective C 的示例片段;

if let pfobjects = images as? [PFObject] {
if pfobjects.count > 0 {
var imageView: PFImageView = PFImageView()
imageView.file = pfobjects[0] as! PFFile
imageView.loadInBackground()
}
}

最佳答案

在 Objective-C 中没有直接等同于 if let,因为 if let 做 Swift 特定的事情(解包可选和重新绑定(bind)标识符)没有直接Objective-C 中的等价物。

下面是几乎等同于您的 Swift 代码:

if (images != nil) {
NSArray<PFObject *> *pfobjects = (id)images;
if (pfobjects.count > 0) {
PFImageView *imageView = [[PFImageView alloc] init];
assert([pfobjects[0] isKindOfClass:[PFFile class]]);
imageView.file = (PFFile *)pfobjects[0];
[imageView loadInBackground];
}
}

但是这个 Objective-C 代码不会验证 images 只包含 PFObject 的实例,并且只要 pfobjects[ 0] 是一个 PFFile。如果 images 包含任何非 PFObject 元素,您的 Swift 代码将不执行任何操作(不创建 ImageView )。

关于objective-c - Swift 在 Objective C 中的 "if let"等价物,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34689834/

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