Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 目标c:如何按名称强制转换为类类型_Objective C - Fatal编程技术网

Objective c 目标c:如何按名称强制转换为类类型

Objective c 目标c:如何按名称强制转换为类类型,objective-c,Objective C,我尝试这样做: Class CustomClass = [MyClass class]; // _object is an instance variable typed with "id" CustomClass *obj = CustomClass *_object; // Error here 在Objective-C中是否可以按名称将对象强制转换为类 更新 我实际上在写一篇文章 - (id)photoFrame { if (NSClassFromString

我尝试这样做:

Class CustomClass = [MyClass class];

// _object is an instance variable typed with "id"
CustomClass *obj =  CustomClass *_object;         // Error here
在Objective-C中是否可以按名称将对象强制转换为类

更新 我实际上在写一篇文章

- (id)photoFrame
{
    if (NSClassFromString(@"CustomClass")) {

        return (CustomClass1 *) _object;
    } else {
        return (CustomClass2 *) _object;
    }
}

错误消息是
预期表达式

CustomClass*obj=(CustomClass*)\u对象

为什么??你期望发生什么?代码中哪里写着“期望表达式”?在
if(NSClassFromString(@“CustomClass”)
部分中,执行
返回(CustomClass1*)\u对象也没有意义除非您要返回其他类型的内容(即不是类型
id
),或者在返回之前实际使用
CustomClass1
中的内容。我想我们大多数人都不知道这应该做什么。你不需要在
id
之间转换,反正它会隐式转换。这个问题没有意义。铸造Objective-C对象不会做任何有意义的工作。虽然我写了一个小的库,但这可能有助于鼓励您正在寻找的类型检查。