Objective c 斯威夫特';s";如果让;目标C中的等价物
我试图通过观看斯威夫特的教程来学习目标C。在学习过程中,我尽可能多地将斯威夫特的代码转换成目标C。最近我偶然发现了“如果让”的结构。我无法转换成目标C。我想知道它的目标C等价物。下面是我想转换为Objective C的示例代码Objective c 斯威夫特';s";如果让;目标C中的等价物,objective-c,swift,Objective C,Swift,我试图通过观看斯威夫特的教程来学习目标C。在学习过程中,我尽可能多地将斯威夫特的代码转换成目标C。最近我偶然发现了“如果让”的结构。我无法转换成目标C。我想知道它的目标C等价物。下面是我想转换为Objective C的示例代码 if let pfobjects = images as? [PFObject] { if pfobjects.count > 0 { var imageView: PFImageView = PFImageView() im
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做的是在Objective-C中没有直接等价物的Swift特定的事情(展开选项和重新绑定标识符) 这里有一个几乎相当于您的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];
}
}
if(图像!=nil){
NSArray*pfobjects=(id)图像;
如果(pfobjects.count>0){
PFImageView*imageView=[[PFImageView alloc]init];
断言([pfobjects[0]iskindof类:[PFFile类]]);
imageView.file=(PFFile*)pfobjects[0];
[图像视图加载背景];
}
}
但此Objective-C代码不会验证
图像是否仅包含PFObject
的实例,只要pfobjects[0]
是PFFile
,就应该成功创建图像视图。如果images
包含任何非PFObject
元素,您的Swift代码将不会执行任何操作(不创建图像视图)。您可以使用NSPredicate验证数组仅包含PFObject
的实例:
NSPredicate *p = [NSPredicate predicateWithFormat:@"self isKindOfClass: %@", [PFObject class]];
NSInteger numberThatArePFObjects = [images filteredArrayUsingPredicate:p].count;
if(numberThatArePFObjects && numberThatArePFObjects == images.count){
// certain that images only contains instances of PFObject.
}
但是,如果您使用的不是阵列而是单个对象,则更简单:
if([image isKindOfClass:[PFObject class]]){
// certain that image is a valid PFObject.
}
或者,如果您想要一个新变量:
PFObject* obj = nil;
if([image isKindOfClass:[PFObject class]] && (obj = image)){
// certain that obj is a valid PFObject.
}
您可以使用Objective-C++代替Objective-C。在这里,您可以使用下一个定义:
#define let const auto
注意:它并不完全相同(Swift有包装值,…),但它使工作更容易
通过这个定义,您可以这样使用它:
if (let pfobjects = images) {
if (pfobjects.count > 0 {
let imageView = [[PFImageView alloc] init];
imageView.file = pfobjects[0];
imageView loadInBackground();
}
}
要在Objective-C++类中转换Objective-C类,只需将实现文件的扩展名.m更改为.mm您可以使用如下内容:
NSArray<PFObject *> *pfobjects;
if ([images isKindOfClass: [NSArray<PFObject> class]] && (pfobjects = images)) {
// your code here
}
NSArray*pfobjects;
if([images iskindof类:[NSArray类]]&&(pfobjects=images)){
//你的代码在这里
}
您同时需要三件事。让我们把它们分开:
变量为?OtherType
是可能的,但会删除类型,因为它返回id
。实现就像NSObject
类上的一个类别一样简单,因此它变成NSArray*数组=[jsonDict[@“objects”]ifkindof类:NSArray.class]
实施
@interface NSObject (OptionalDowncast)
- (id)ifKindOfClass:(__unsafe_unretained Class)clazz;
@end
@implementation NSObject (OptionalDowncast)
- (id)ifKindOfClass:(__unsafe_unretained Class)clazz {
return [self isKindOfClass:clazz] ? self : nil;
}
@end
如果类型已知,则在Objective-C中也可以使用if let
,因此它不能与前面的内容组合。最简单的方法是:for(NSArray*array=[self-getItems];array!=nil;array=nil){…}
,但是如果您想使用else
分支,它会变得更复杂一些。我已经为那个做了豆荚,请看一看
NSArray
,但不能转换到NSArray
for(id object in images) {
for(PFFile *file = [object ifKindOfClass:PFFile.class]; file != nil; file = nil) {
//operate on file
}
}
欢迎来到stackoverflow。如果您正确地格式化代码,帮助您会容易得多,这样我们就可以理解它。这次我帮你修好了,你什么意思<代码>如果让打开可选项,则Objective-C中没有可选项。在Objective-C[insert Boromir]中不能简单地声明可选项,顺便问一下,为什么要使用Swift教程学习Objective-C?我的意思是你可以通过Objective-C教程来学习,对吗?我是@lascort的。如果你想学习Objective-C,Swift不是最好的方法。观看Swift视频不是学习Objective-C的最佳方法,但对于已经了解Swift并正在学习Objective-C的人来说,这是一个合理、明确的问题。没有理由否决或关闭它。这被称为“轻量级通用”并且是Objective-C最近添加的一个数组。在去年6月之前,您不必费心声明另一个数组,因为
pfobjects
和images
都只是类型NSArray*
。区别在于Objective-C中的图像!=无
是不需要的。
for(id object in images) {
for(PFFile *file = [object ifKindOfClass:PFFile.class]; file != nil; file = nil) {
//operate on file
}
}